Edge detection method for image cropping

As part of my project i am trying to extract an object from the image. I was able to extract using threshold/intensity level slicing. However, I am wanting to use the canny edge method to crop the image to extract the feature. How do i achieve this?
% code
img = imread('Image.jpg');
img = rgb2gray(img);
img = imresize(img,[600 350]);
for i =1:size(img,1)
for j =1:size(img,2)
if img(i,j)<60 && img(i,j)>5
extrct(i,j) = 1;
else
extrct(i,j) = 0;
end
end
end
extrct = logical(extrct);
final = img.*uint8(extrct);
final = final.*2.5;
ed_img = edge(extrct,'canny');
figure(1),imshow(img);figure(2),imshow(final);figure(3),imhist(final);figure(4),imshow(ed_img);
Following is the image I am using:
I was able to extract the following feature:
The edge feature of the original image is:

댓글 수: 2

Can you please explain the logic behind this part? I tried this code for a different image but didn't get the desired outputif img(i,j)<60 && img(i,j)>5 extrct(i,j) = 1; else extrct(i,j) = 0;
@Simon Kuriakose, That poorly written code just checks to see if the gray level is between 5 and 60. Basically it's the same as this vectorized version:
extrct = img > 5 & img < 60;
You should really use my code below. And of course you have to adjust the intensity range for whatever is in your image.

댓글을 달려면 로그인하십시오.

답변 (2개)

Image Analyst
Image Analyst 2014년 12월 11일

1 개 추천

Try the attached code which does background correction followed by global thresholding to produce this figure. The background is estimated by looking at the intensity pattern on the sides and then replicating that to a whole image and dividing the gray scale image by the background image.

댓글 수: 3

Josip Strutz
Josip Strutz 2016년 12월 9일
편집: Josip Strutz 2016년 12월 9일
Hi, I tried your Code on my image and it's not working, because I want to keep the grey parts and "delete" the black ones. Can you please help me? Attached is the picture and the Code
Image Analyst
Image Analyst 2016년 12월 9일
Why do you think that algorithm would work for your image? Arvind had places on the sides where the background gradient could be determined. You don't have that since your subject goes all the way across the image. I'm not sure what you want to do, but maybe try adapthisteq() -- if that doesn't work, then start your own question so we don't keep emailing Arvind that there has been activity on his question.
I have been referencing this example on and off the last few weeks, and realized I never really understood how exactly this chunk of code works:
% Let's get the vertical profile
sides = [grayImage(:, 1:70), grayImage(:, 300:end)];
verticalProfile = mean(sides, 2);
% Let's smooth it with a Saviztky-Golay filter (optional)
verticalProfile = sgolayfilt(verticalProfile, 2, 45);
subplot(3, 3, 3);
plot(verticalProfile, 'b-', 'LineWidth', 2);
grid on;
title('Vertical Profile of Background', 'FontSize', fontSize);
% Make a background image that we can divide by.
backgroundImage = repmat(verticalProfile, [1, columns]);
subplot(3, 3, 4);
imshow(backgroundImage, []);
axis on;
title('Background Image', 'FontSize', fontSize);
% divide the image by the profile
flattenedImage = uint8(255 * double(grayImage) ./ backgroundImage);
subplot(3, 3, 5);
imshow(flattenedImage, []);
axis on;
title('Background Corrected Image', 'FontSize', fontSize);
% Let's compute and display the histogram of the background corrected image.
[pixelCountBG, grayLevelsBG] = imhist(flattenedImage);
% Suppress last bin which is a huge spike at 255, just so we can see the shape.
pixelCountBG(end) = 0;
subplot(3, 3, 6);
bar(grayLevelsBG, pixelCountBG);
grid on;
title('Histogram of Background Corrected Image', 'FontSize', fontSize);
xlim([0 grayLevelsBG(end)]); % Scale x axis manually.
I more or less understand that this has the effect of wiping away some of the background, but how exactly does that work? Whats the difference between image subtraction and division? What is the vertical profile exactly?
If nothing else, could you explain these three lines of code?
sides = [grayImage(:, 1:70), grayImage(:, 300:end)];
...
backgroundImage = repmat(verticalProfile, [1, columns]);
...
flattenedImage = uint8(255 * double(grayImage) ./ backgroundImage);

댓글을 달려면 로그인하십시오.

Image Analyst
Image Analyst 2014년 12월 11일

0 개 추천

Did you try the edge() function with the Canny option?
If you want to know why the first method worked poorly, it's because you converted to grayscale and you didn't do a background correction. A simple global threshold won't work with this image because it's so much brighter at the top than the bottom of the image. If you want to know some ways to do background correction, let me know.

댓글 수: 1

Arvind
Arvind 2014년 12월 11일
편집: Arvind 2014년 12월 11일
Yes i did use the edge() with canny option.
I have also removed the minimum threshold to remove the blob in the mouse.
How do i do the background correction?

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

질문:

2014년 12월 11일

댓글:

2021년 6월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by