Edge detection method for image cropping

4 views (last 30 days)
Arvind
Arvind on 11 Dec 2014
Commented: Image Analyst on 10 Jun 2021
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 Comments
Simon Kuriakose
Simon Kuriakose on 10 Jun 2021
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;
Image Analyst
Image Analyst on 10 Jun 2021
@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.

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 11 Dec 2014
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 Comments
Image Analyst
Image Analyst on 9 Dec 2016
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.
Kimo Kalip
Kimo Kalip on 3 Jul 2018
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);

Sign in to comment.


Image Analyst
Image Analyst on 11 Dec 2014
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 Comment
Arvind
Arvind on 11 Dec 2014
Edited: Arvind on 11 Dec 2014
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?

Sign in to comment.

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!