How can I "remove" a certain part of a colored image? (Double Edges??)
Show older comments
Okay, So I have hundreds of images that I need to analyze using MATLAB. The Images are of the underside of a turtle. The images looks something like this (not the image im using, but just for reference) http://upload.wikimedia.org/wikipedia/commons/2/28/B3_Southern_painted_turtle_underside.jpg.
I want to get rid of the "body of the turtle, and just have the shell to work with (if you know turtle parts, i just want to use the plastron). I have tried using many edge detection codes, but i cant seem to just get rid of the "body" of the turtle.
Here's an example of what im trying to do/have so far.
f = imread('IMG_0003.jpg');
g = imcrop(f,[750 500 1800 1300]);
imshow(g)
% % % reads and crops
B=rgb2gray(g);
D=double(B);
for i=1:size(D,1)-2
for j=1:size(D,2)-2
%Sobel mask for x-direction:
Gx=((2*D(i+2,j+1)+D(i+2,j)+D(i+2,j+2))-(2*D(i,j+1)+D(i,j)+D(i,j+2)));
%Sobel mask for y-direction:
Gy=((2*D(i+1,j+2)+D(i,j+2)+D(i+2,j+2))-(2*D(i+1,j)+D(i,j)+D(i+2,j)));
%The gradient of the image
%B(i,j)=abs(Gx)+abs(Gy);
B(i,j)=sqrt(Gx.^2+Gy.^2);
end
end
figure,imshow(B); title('Sobel gradient');
% % % Converts it to gray scale, edge detection.
C = num2cell(D);
C(~D) = {{}};
imagesc(D, 'AlphaData', D)
colormap(gray)
set(gca, 'color', 'none')
Z = cell2mat(C);
% % % % Removes the background
q = double(Z);
im=mat2gray(q);
level = graythresh(im);
imb = im2bw(im,level);
imb = ~imb;
hope = imfill(imb,'holes');
figure,imshow(hope); title('HOPE');
A = numel(hope(hope==0))
B = numel(hope(hope==1))
******* I am a beginner at Matlab, so sorry for that. But, if you look at the image "hope", you can see that i can get an image of the entire turtle and shell to be white while discluding the background. I then counted the pixels of white to find how many made up the turtle. I WANT TO DO THIS WITH ONLY THE SHELL! (So maybe theres a way to highlight both the inner and outer edge and then cut out that middle part??? I dont know).
Please, any help would be greatly appreciated!! :) THANKS!
PS: I'm doing this so I can eventually find a proportion of "orange colored pixels on the turtle shell" to the "whole shell" of the turtle. I'm doing this for analytical purposes, so i can corolate this value to the same ratio on the top of the turtle shell. So once again, Just want to cut out the "body of the turtle" and be left with the shell. Thank you!
1 Comment
Amith Kamath
on 11 Jan 2013
You could save the pains of coding the sobel edge detector by using:
edge(IMG,'sobel')
Accepted Answer
More Answers (2)
Image Analyst
on 13 Jan 2013
Edited: Image Analyst
on 13 Jan 2013
0 votes
I don't know why you used edge detection. Simple thresholding will do it. Similarly, using cell arrays and cropping (at least for that image) are unnecessary.
Anyway, I think you'll have to use imline() to draw a line across the legs, head, and tail? Why? Because legs are both under the shell area and outside the shell are. You want to consider the part of the leg under the shell as "shell" but you don't want the part of the leg not under the shell to count as shell area. And without getting really complicated, you can't do it. Assuming you have only a few hundred images or so, it will be faster to just manually draw across the body parts jutting out. Then once that's done, you can just threshold to find the total shell. Then call regionprops to get the areas. The largest area is the shell of course - smaller areas are the legs, head, or tail. Now do color segmentation -- there are several examples of different methods in my File Exchange. You might have to do a morphological closing to get rid of some lines on the legs which are pretty close in color to the lighter undershell color. Now you have everything - total shell area, and lighter undershell area.
But I didn't see a ColorChecker chart in the image. Depending on how variable your illumination and exposure are, you might need this.
11 Comments
Dimitri
on 15 Jan 2013
Image Analyst
on 15 Jan 2013
Can you upload any of your images?
Dimitri
on 15 Jan 2013
Dimitri
on 15 Jan 2013
Dimitri
on 15 Jan 2013
Image Analyst
on 15 Jan 2013
Well the legs look so much like the shell, and they are tucked in various amounts, so it would be difficult to find which little bump in the outline is part of a leg sticking out as opposed to just part of the shell. So I think a manual method is probably the way to go. If you wanted to try, you could fit an ellipse to the shell and mask off anything that stuck outside the ellipse, figuring that anything what stuck out was a leg. That might sort of work for some images but wouldn't be robust enough to do it correctly on thousands of images. See my code below for how to write a line into an image. You' want to write in a line with gray levels 255 so that it basically draws a background line between the shell and the leg sticking out.
Dimitri
on 15 Jan 2013
Dimitri
on 15 Jan 2013
Dimitri
on 15 Jan 2013
Dimitri
on 23 Jan 2013
0 votes
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!