edge detection and comparison

17 ビュー (過去 30 日間)
Jishnoop JAYAPRAKASH
Jishnoop JAYAPRAKASH 2020 年 3 月 9 日
回答済み: Image Analyst 2025 年 2 月 15 日 3:59
How to compare these two images to find the length and area of distortion in the final image
  2 件のコメント
Image Analyst
Image Analyst 2020 年 3 月 9 日
Can you circle the distorted region?
Jishnoop JAYAPRAKASH
Jishnoop JAYAPRAKASH 2020 年 3 月 9 日
The diameter of 11.2.bmp has reduced due to machining, so I want to calculate the reduced area and radius of the second image based on first image (1.bmp)

サインインしてコメントする。

回答 (2 件)

Satyam
Satyam 2025 年 2 月 14 日 11:48
In order to calculate the reduced area and radius, you can proceed with the following steps:
Here is a MATLAB Script implementing these steps.
img1 = imread('1.BMP');
img2 = imread('11.2.BMP');
% Convert to grayscale if necessary
if size(img1, 3) == 3
img1 = rgb2gray(img1);
end
if size(img2, 3) == 3
img2 = rgb2gray(img2);
end
% Define crop region (example: cropping to the central region)
% Adjust these values based on your image size and the region of interest
cropRegion = [size(img1, 2)/8, size(img1, 1)/20, 3*size(img1, 2)/4, 3*size(img1, 1)/3];
% Crop the images
img1_cropped = imcrop(img1, cropRegion);
img2_cropped = imcrop(img2, cropRegion);
% Apply adaptive histogram equalization to enhance contrast
img1_eq = adapthisteq(img1_cropped);
img2_eq = adapthisteq(img2_cropped);
% Apply Gaussian smoothing to reduce noise
img1_smooth = imgaussfilt(img1_eq, 2);
img2_smooth = imgaussfilt(img2_eq, 2);
% Detect edges using Canny method with adjusted parameters
edges1 = edge(img1_smooth, 'Canny', [0.1, 0.2]);
edges2 = edge(img2_smooth, 'Canny', [0.1, 0.2]);
% Perform morphological operations to clean up edges
se = strel('disk', 2);
edges1_clean = imclose(edges1, se);
edges2_clean = imclose(edges2, se);
% Find contours and measure properties
stats1 = regionprops(edges1_clean, 'Area', 'EquivDiameter');
stats2 = regionprops(edges2_clean, 'Area', 'EquivDiameter');
[~, idx1] = max([stats1.Area]);
[~, idx2] = max([stats2.Area]);
area1 = stats1(idx1).Area;
diameter1 = stats1(idx1).EquivDiameter;
area2 = stats2(idx2).Area;
diameter2 = stats2(idx2).EquivDiameter;
% Calculate the reduced area and radius
reducedArea = area1 - area2;
reducedRadius = (diameter1 - diameter2) / 2;
% Display results
fprintf('Original Area: %.2f\n', area1);
Original Area: 1664.00
fprintf('Machined Area: %.2f\n', area2);
Machined Area: 1755.00
fprintf('Reduced Area: %.2f\n', reducedArea);
Reduced Area: -91.00
fprintf('Original Diameter: %.2f\n', diameter1);
Original Diameter: 46.03
fprintf('Machined Diameter: %.2f\n', diameter2);
Machined Diameter: 47.27
fprintf('Reduced Radius: %.2f\n', reducedRadius);
Reduced Radius: -0.62
% Visualize the results
figure;
subplot(2, 2, 1);
imshow(img1_smooth); title('Smoothed Cropped Image 1');
subplot(2, 2, 2);
imshow(edges1_clean); title('Edges of Cropped Image 1');
subplot(2, 2, 3);
imshow(img2_smooth); title('Smoothed Cropped Image 2');
subplot(2, 2, 4);
imshow(edges2_clean); title('Edges of Cropped Image 2');
Please note that, since the second image is a bit more zoomed, therefore, the result is not as per expectation.
I hope it would be useful for you!
  1 件のコメント
Image Analyst
Image Analyst 2025 年 2 月 15 日 3:57
Not sure why you think the equivalent diameter of the largest edge segment is the same as the diameter of the whole circular machined part.
Let's take a look at what blob you're actually taking the diameter of by displaying it.
% Convert to grayscale if necessary
if size(img1, 3) == 3
img1 = rgb2gray(img1);
end
if size(img2, 3) == 3
img2 = rgb2gray(img2);
end
% Define crop region (example: cropping to the central region)
% Adjust these values based on your image size and the region of interest
cropRegion = [size(img1, 2)/8, size(img1, 1)/20, 3*size(img1, 2)/4, 3*size(img1, 1)/3];
% Crop the images
img1_cropped = imcrop(img1, cropRegion);
img2_cropped = imcrop(img2, cropRegion);
% Apply adaptive histogram equalization to enhance contrast
img1_eq = adapthisteq(img1_cropped);
img2_eq = adapthisteq(img2_cropped);
% Apply Gaussian smoothing to reduce noise
img1_smooth = imgaussfilt(img1_eq, 2);
img2_smooth = imgaussfilt(img2_eq, 2);
% Detect edges using Canny method with adjusted parameters
edges1 = edge(img1_smooth, 'Canny', [0.1, 0.2]);
edges2 = edge(img2_smooth, 'Canny', [0.1, 0.2]);
% Perform morphological operations to clean up edges
se = strel('disk', 2);
edges1_clean = imclose(edges1, se);
edges2_clean = imclose(edges2, se);
% Find contours and measure properties
stats1 = regionprops(edges1_clean, 'Area', 'EquivDiameter');
stats2 = regionprops(edges2_clean, 'Area', 'EquivDiameter');
[~, idx1] = max([stats1.Area]);
[~, idx2] = max([stats2.Area]);
area1 = stats1(idx1).Area;
diameter1 = stats1(idx1).EquivDiameter;
area2 = stats2(idx2).Area;
diameter2 = stats2(idx2).EquivDiameter;
% Calculate the reduced area and radius
reducedArea = area1 - area2;
reducedRadius = (diameter1 - diameter2) / 2;
% Display results
fprintf('Original Area: %.2f\n', area1);
fprintf('Machined Area: %.2f\n', area2);
fprintf('Reduced Area: %.2f\n', reducedArea);
fprintf('Original Diameter: %.2f\n', diameter1);
fprintf('Machined Diameter: %.2f\n', diameter2);
fprintf('Reduced Radius: %.2f\n', reducedRadius);
% Visualize the results
figure;
subplot(3, 2, 1);
imshow(img1_smooth); title('Smoothed Cropped Image 1');
subplot(3, 2, 2);
imshow(edges1_clean); title('Edges of Cropped Image 1');
subplot(3, 2, 3);
imshow(img2_smooth); title('Smoothed Cropped Image 2');
subplot(3, 2, 4);
imshow(edges2_clean); title('Edges of Cropped Image 2');
% Get the blob we measured and put it in row 3.
blob1 = ismember(bwlabel(edges1_clean), idx1);
% It's so tiny, let's crop it out so it will display bigger so we can see it.
props = regionprops(blob1, 'BoundingBox')
blob1 = imcrop(blob1, props.BoundingBox);
subplot(3, 2, 5);
imshow(blob1); title('What we measure in Image 1');
blob2 = ismember(bwlabel(edges2_clean), idx2);
subplot(3, 2, 6);
imshow(blob2); title('What we measure in Image 2');
As you can see, what you're measuring the equivalent circular diameter of is nothing like the diameter of the whole circular part. You're not even getting the bounding circle. You're getting a much smaller diameter because it's the diameter of a circle that has the same number of pixels as those edge shapes in the last row of images above. And it's certainly not finding the defective parts that are sticking out of the thing.

サインインしてコメントする。


Image Analyst
Image Analyst 2025 年 2 月 15 日 3:59
What I would do is this:
  1. First use a telecentric lens - not a regular lens.
  2. Then make sure the backgrounds are the same (like both are white) and improve the lighting to get rid of the shadows.
  3. Then segment out the part and
  4. rotate it to align with a perfect, defect-free version of the part.
  5. Then, if your lighting is not good (like the parts have different brightnesses) then match the brightnesses.
  6. Then you can use imabsdiff to compute the difference image.
If the test part is perfect then the difference image should be all black (0). If there are some non-zero pixels than that represents potential defects.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by