Hello,
I'm trying to detect the thin edge of my parallel rolls that can be seen from left to right. I've tried using canny, Prewitt and others to detect, but it didn't work....
My purpose is to automatically get the number of "rolls", the thickness, the area ....
image = im2gray(im2double(imread("2024-03-24_01-30-21.423-f.tiff")));
BX1 = edge(image, 'Canny');
imshow(BX1);
Screenshot of what I would like :
my image in tiff or png format : https://amubox.univ-amu.fr/s/ZDZWPpjeocZk8pN
Do you have any advices ?
Thank you !

 Réponse acceptée

Alexandre
Alexandre le 5 Avr 2024

0 votes

So I have made 3 differents codes :
The first one is the most simplier :
% 1. load image
%image = imread('votre_image.jpg'); % Remplacez 'votre_image.jpg' par le chemin de votre image
if size(image, 3) == 3
grayImage = rgb2gray(image);
else
grayImage = image;
end
% 2. Remove salt & Paper
med_grayImage = medfilt2(double(grayImage), [20 20]);
% 3. clean and smooth image using gaussian filter
sigma = 3; % STD gaussian filter
filteredImage = imgaussfilt(med_grayImage, sigma);
% 4. apply laplacian to highlight edges
laplacian = fspecial('laplacian');
laplacianFiltered = imfilter(filteredImage, laplacian);
% 5. display image
figure(1)
imshow(laplacianFiltered);
% 6. Dectect edges and binarize
BW1 = edge(laplacianFiltered,'canny', [0.0010 0.0018]);
figure(2)
imshow(BW1);
% 7. Apply binary operation to get edges
BW1 = bwmorph(BW1, "clean", Inf);
se = strel('disk',6, 8);
BW2 = imdilate(BW1, se);
BW3 = bwmorph(BW2,'bridge', Inf);
figure(3)
imshow(BW3)
2nd :
% 1. Load the image
%image = imread('your_image.jpg'); % Replace 'your_image.jpg' with the path to your image
% 2. Convert to grayscale if necessary
if size(image, 3) == 3
grayImage = rgb2gray(image);
else
grayImage = image;
end
% Apply median filter to reduce noise
med_grayImage = medfilt2(grayImage, [32 32]);
% 3. Define Gaussian filter parameters
sigma1 = 0.5; % Standard deviation of the first Gaussian filter
sigma2 = 50; % Standard deviation of the second Gaussian filter
% 4. Apply Gaussian filters
gaussian1 = imgaussfilt(double(med_grayImage), sigma1);
gaussian2 = imgaussfilt(double(med_grayImage), sigma2);
% 5. Compute the Difference of Gaussians
DoG = gaussian1 - gaussian2;
% 6. Display the image of the Difference of Gaussians
imshow(DoG, []);
% edge(DoG, 'Canny');
% 7. Apply a Gaussian filter
sigma = 3; % Standard deviation of the Gaussian filter
filteredImage = imgaussfilt(DoG, sigma);
% 8. Apply a Laplacian operator
laplacian = fspecial('laplacian');
laplacianFiltered = imfilter(filteredImage, laplacian);
% 9. Display the image with detected contours
figure(1)
imshow(laplacianFiltered);
% 10. Apply Canny edge detection
BW1 = edge(laplacianFiltered,'canny', [0.0010 0.0018]);
% 11. Display the Canny edge-detected image
figure(2)
imshow(BW1);
% 12. Clean up the edges
BW1 = bwmorph(BW1, "clean", Inf);
se = strel('disk',6, 8);
BW2 = imdilate(BW1, se);
% 13. Bridge small gaps in the edges
BW4 = bwmorph(BW2,'bridge', Inf);
% Display the cleaned and enhanced edges
figure(3)
imshow(BW4)
Third :
% 1. Load the image
%image = imread('your_image.jpg'); % Replace 'your_image.jpg' with the path to your image
% 2. Convert to grayscale if necessary
if size(image, 3) == 3
grayImage = rgb2gray(image);
else
grayImage = image;
end
% Apply median filter to reduce noise
med_grayImage = medfilt2(grayImage, [20 20]);
% 3. Apply Sobel mask in the x-direction
sobelMaskX = [-1 0 1; -2 0 2; -1 0 1];
sobelFilteredX = imfilter(double(med_grayImage), sobelMaskX);
% 4. Apply Sobel mask in the y-direction
sobelMaskY = [-1 -2 -1; 0 0 0; 1 2 1];
sobelFilteredY = imfilter(double(med_grayImage), sobelMaskY);
% 5. Compute gradient magnitude
gradientMagnitude = sqrt(sobelFilteredX.^2 + sobelFilteredY.^2);
% 6. Display the gradient magnitude image
imshow(uint8(gradientMagnitude));
% % 7. Apply Gaussian filter
sigma = 3; % Standard deviation of the Gaussian filter
filteredImage = imgaussfilt(gradientMagnitude, sigma);
% 8. Apply Laplacian operator
laplacian = fspecial('laplacian');
laplacianFiltered = imfilter(filteredImage, laplacian);
% 9. Display the image with detected contours
figure(1)
imshow(laplacianFiltered);
% Apply Canny edge detection
BW1 = edge(laplacianFiltered,'canny', [0.0010 0.0018]);
% Display the Canny edge-detected image
figure(2)
imshow(BW1);
% Clean up the edges
BW1 = bwmorph(BW1, "clean", Inf);
se = strel('disk',6, 8);
BW2 = imdilate(BW1, se);
% Bridge small gaps in the edges
BW4 = bwmorph(BW2,'bridge', Inf);
% Display the cleaned and enhanced edges
figure(3)
imshow(BW4)
here what I get using one of the 3 codes above :
Do you think I can improve it ?
Thank you !

Plus de réponses (0)

Produits

Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by