edge detection and comparison
17 ビュー (過去 30 日間)
古いコメントを表示
How to compare these two images to find the length and area of distortion in the final image
2 件のコメント
回答 (2 件)
Satyam
2025 年 2 月 14 日 11:48
In order to calculate the reduced area and radius, you can proceed with the following steps:
- Preprocess the image by converting it to greyscale and cropping them to focus on region of interest.
- Enhance the image contrast by using adaptive histogram equalization to improve the image and make it easier to detect edges. You can use 'adapthisteq' function in order to do that. You can find more about 'adapthisteq' https://www.mathworks.com/help/images/ref/adapthisteq.html
- You can apply Gaussian smoothing using 'imgaussfilt' to reduce noise in the image. More details on 'imgaussfilt' can be found https://www.mathworks.com/help/images/ref/imgaussfilt.html
- Use the Canny edge detection method to identify the boundaries of the objects in the images. You can refer to the documentation of 'edge' function https://www.mathworks.com/help/images/ref/edge.html
- You can then calculate the area and equivalent diameter using 'regionprops' function. Refer to the documentation of 'regionprops' in order to learn more https://www.mathworks.com/help/images/ref/regionprops.html
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);
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(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
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
2025 年 2 月 15 日 3:59
What I would do is this:
- First use a telecentric lens - not a regular lens.
- Then make sure the backgrounds are the same (like both are white) and improve the lighting to get rid of the shadows.
- Then segment out the part and
- rotate it to align with a perfect, defect-free version of the part.
- Then, if your lighting is not good (like the parts have different brightnesses) then match the brightnesses.
- 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.
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!