How to create a silhouette

3 views (last 30 days)
Ian
Ian on 13 Apr 2013
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
Image Analyst
Image Analyst on 14 Apr 2013
Edited: Image Analyst on 14 Apr 2013
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');
Cedric
Cedric on 14 Apr 2013
Thank you for the update!

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 13 Apr 2013
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!