How to create a silhouette

Hi all,
I've been trying to create a silhouette with the following code;
function S = getsilhouette( im )
%GETSILHOUETTE - find the silhouette of an object centered in the image
[h,w,d] = size(im);
% Initial segmentation based on more red than blue
S = im(:,:,1) > (im(:,:,3)-2);
% Remove regions touching the border or smaller than 10% of image area
S = bwareaopen(S, ceil(h*w/10));
squeeze(S);
% % Now remove holes < 1% image area
S = ~bwareaopen(~S, ceil(h*w/100));
And this is what I get as an output;
I was wondering if anyone had any ideas to remove the base?
Many thanks and cheers for reading
Adam

5 Comments

Could you grab an image without the object (but with the support)? This could be a basis for extracting whatever object you place on the support..
Ian
Ian on 13 Apr 2013
Yes I could. Then somehow subtract the images from each other??
Cheers mate
Cedric
Cedric on 13 Apr 2013
Edited: Cedric on 14 Apr 2013
Yes, that's the idea. Any pixel of the image including the object that is not "close enough" to the corresponding pixel of the image with no object, defines the object.
Now I have little experience in image processing; I would be able to do it my way, but I am unable to tell you how to do it using proper techniques, so I would be interested as well in having Image Analyst's take on this matter.
PS: "my way" would be to set thresholds on the norm of differences between corresponding channels associated with both the image without and the image with the object.
If you can get a "blank shot" - an image without the object of interest in it - then that is certainly the way to go. Then just subtract and threshold.
diffImage = abs(double(grayImage) - double(backgroundImage));
binaryImage = diffImage > 4; % or whatever value you want.
You might want to fill holes too:
binaryImage = imfill(binaryImage, 'holes');
Thank you for the update!

Sign in to comment.

 Accepted Answer

Find out where the red channel is dark, the green channel is dark, and the blue channel is dark, all at the same time.
% Extract the individual red, green, and blue color channels.
% rgbImage is what you call im
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
blackPixels = redChannel < redThreshold & greenChannel < greenThreshold & blueChannel < blueThreshold;
binaryImage(blackPixels) = 0; % binaryImage is what you called S.

2 Comments

Ian
Ian on 13 Apr 2013
What are the redThreshold, blueThreshold and greenThreshold ? I'm getting an error saying they are undefined. What shall I make them?
Many thanks
Cedric
Cedric on 13 Apr 2013
Edited: Cedric on 13 Apr 2013
These are numbers that you have to define in order to differentiate apparently (but not exactly) black pixels from others. If apparently black pixels in your image were exactly black, they would be represented by 0's in each channel and you could easily flag them by finding 0 values in each channel. But in practice they are certainly not exactly 0; they must be close to 0 though, otherwise we wouldn't perceive them as almost black. You'll have to experiment a bit here and see how far/close to 0 you have to set them in order to target the black support, but not the dark blue background for example.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!