How to create a silhouette from image
Show older comments
I need to create a silhouette from an image to take the central moments on for a car classifier. I've already been able to process the image using k-means clustering, but can't get the last part of my problem solved. I basically need to remove the largest region that touches all four borders of the image, then make the rest black (leaving a white background). Below are how far I've gotten to my goal. How can I go from the second image to what i'm looking for?


Accepted Answer
More Answers (4)
Image Analyst
on 23 Feb 2017
You don't need kmeans. It's not needed for an image like that. Just wastes time for no reason. Simply find a mask and you're done
topGL = 255; % Whatever white is.
silhouetteImage = rgbImage(:,:,1) < topGL | rgbImage(:,:,2) < topGL | rgbImage(:,:,3) < topGL;
imshow(silhouetteImage); % Display logical/binary image.
2 Comments
Brenton Poke
on 23 Feb 2017
Image Analyst
on 23 Feb 2017
I'm just making a mask of everything that is not pure white. If you want to do it the more complicated way using kmeans(), then you need to take your kmeans image and extract the class that is the background, and use bwareafilt() (because there are more purple regions there than just the surround).
% kmeans code..... like attached demo.... then...
backgroundClass = kmeansImage == 1; % Or whatever index it is.
silhouetteImage = bwareafilt(backgroundClass, 1); % Extract largest region.
imshow(silhouetteImage); % Display logical/binary image.
John BG
on 23 Feb 2017
Hi Brenton
please help me out and tell if I am on the right direction
1.
capture
A=imread('etiquette.jpg');
% figure(1);imshow(A);
2. picking on peculiar combination of car colour
A(A<80)=255;
figure(2);imshow(A)

A1=A(:,:,1);A2=A(:,:,2);A3=A(:,:,3);
p1=find(A1>200);
p2=find(A2<170);
pc=intersect(p1,p2);
B=zeros(size(A1));
B(pc)=255;
figure(3);imshow(B)

.
would the chassis do as point for, from here, get the silhouette?
there is a bit of noise and reflection ribbon, you may also want it off, right?
awaiting answer
John BG
6 Comments
Brenton Poke
on 23 Feb 2017
Image Analyst
on 24 Feb 2017
Is the second, colored image an indexed image that just has some weird colormap applied, or is it a 3-D/RGB image
John BG
on 24 Feb 2017
If the shadow is not a problem, then starting from the picture you want:
1.
A=imread('eti2.jpg');
% figure(1);imshow(A);
2.
focus on cyan
A(A<80)=255;
figure(2);imshow(A)

A1=A(:,:,1);A2=A(:,:,2);A3=A(:,:,3);
A13=A(:,:,1)-A(:,:,3);A23=A(:,:,2)-A(:,:,3);
figure(3);imshow(A13)
figure(4);imshow(A23)

which one do you prefer to carry on, figure(3) or figure(4)?
I still think that your application to be more robust should remove the shadow underneath.
For such purpose I suggest first part catching chassis, of course an upgrade of previous for any colour, and another part spotting the wheels. Together would give the silhouette you are after, right?
regards
Brenton Poke
on 24 Feb 2017
Edited: Brenton Poke
on 24 Feb 2017
Image Analyst
on 24 Feb 2017
Did you see my second answer (below)? It does what you want in 3 lines of code.
John BG
on 24 Feb 2017
Brenton
because you told me to start from the image you obtained with your K-means analysis I started from im2.jpg here attached.
The size may not match because I took the image from your question.
Apply my lines to the outcome of your K-means, check it works with im2.jpg here attached.
John BG
Shiba Kuanar
on 11 Oct 2019
0 votes
Hi All,
I used the code - to get a black silhouette of the car (very beginning)
I am getting the result but with some error message (below): (also attached figure for review)
__________________________________________________________________
Error using images.internal.imageDisplayValidateParams>validateCData (line 119)
If input is logical (binary), it must be two-dimensional.
Error in images.internal.imageDisplayValidateParams (line 27)
common_args.CData = validateCData(common_args.CData,image_type);
Error in images.internal.imageDisplayParseInputs (line 78)
common_args = images.internal.imageDisplayValidateParams(common_args);
Error in imshow (line 241)
images.internal.imageDisplayParseInputs({'Parent','Border','Reduce'},preparsed_varargin{:});
Error in SilhouetShiba (line 44)
imshow(mask, []);
1 Comment
Image Analyst
on 11 Oct 2019
TestImageSIL.png is probably not a grayscale image. Use rgb2rgray() to make in into grayscale.
Shiba Kuanar
on 11 Oct 2019
0 votes
Thank You
Categories
Find more on Convert Image Type 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!
