Edge detection method for image cropping

조회 수: 7 (최근 30일)
Arvind
Arvind 2014년 12월 11일
댓글: Image Analyst 2021년 6월 10일
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
Simon Kuriakose
Simon Kuriakose 2021년 6월 10일
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 2021년 6월 10일
@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일
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
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.
Kimo Kalip
Kimo Kalip 2018년 7월 3일
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일
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?

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

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by