How to extract an object from an image

I've got this code to extract a single cashew from a image containing multiple cashews. I need the extracted image to be RGB as well.
cashew_RGB = imread('White Wholes 180.jpg');
cashew_BW = img_BW(cashew_RGB);
cashew_stats = regionprops(bwlabel(cashew_BW),'BoundingBox');
cashew_single = imcrop(cashew_RGB,cashew_stats(i).BoundingBox);
I have attached the image and the user-defined function 'img_BW' which converts RGB image to black and white If you execute the code you'll find that the extracted cashew image has an invasion from the neighboring cashew. Is there any way to extract the cashew without such invasions? Thanks for your help!

 Réponse acceptée

Image Analyst
Image Analyst le 13 Déc 2015
You can't use the bounding box or else you may have incursions from another blob that juts into the bounding box. You have to use either PixelList from regionprops, or (easier) use ismember() with the blob you want to extract:
thisBlob = ismember(labeledImage, blobIndexToExtract) > 0;

6 commentaires

Nikhil Hegde
Nikhil Hegde le 14 Déc 2015
Modifié(e) : Nikhil Hegde le 14 Déc 2015
I implemented your idea in this fashion:
cashew_RGB = imread('White Wholes 500.jpg');
cashew_BW = img_BW(cashew_RGB);
labeledImage = bwlabel(cashew_BW);
cashew_stats = regionprops(labeledImage,'BoundingBox');
for i = 1:numel(cashew_stats)
cashew_single = ismember(labeledImage, i) > 0;
figure, imshow(cashew_single);
end
And I have attached the output image of the last cashew extracted (cashew_segmented_BW.jpg). But what I want the extracted cashew image to be in RGB and not black and white. The later part of the code analyzes the cashew in HSV space which actually I extract from the RGB image. Is there any function for extracting the RGB image and not the black and white image? I apologize for these doubts. I'm new to MATLAB so my function literature isn't really vast. Thanks for all your help!
Mask the original RGB image with the binary image like this:
% Mask the image using bsxfun() function
maskedRgbImage = bsxfun(@times, cashew_RGB, cast(cashew_single, 'like', cashew_RGB));
Nikhil Hegde
Nikhil Hegde le 15 Déc 2015
That did the trick! Thanks!
Image Analyst
Image Analyst le 15 Déc 2015
Did it do it enough to let you officially "Accept this answer" and give me reputation points?
nour n
nour n le 10 Jan 2017
how to extract the object without the black background?
Jan
Jan le 10 Jan 2017
@nour: Please use flags for pointing out inproper use of the forum. Instead of attaching a new question as a comment, open a new thread. Thanks.

Connectez-vous pour commenter.

Plus de réponses (2)

r r
r r le 8 Août 2018

0 votes

I have an image I want to extract the small areas of black color within the blue How so؟؟؟

1 commentaire

Image Analyst
Image Analyst le 8 Août 2018
Start your own new, separate question and I'll show you how.

Connectez-vous pour commenter.

Arnab Banerjee
Arnab Banerjee le 13 Sep 2019

0 votes

first, convert to gray, by rgb2gray(), then find the perfect threshold of the region from the image matrix. it can be aslo done in RGB image if you knew the R:G:B ration of the threshold.
lastly, past the bounded area in a mask, or extract the partcular threshold

Community Treasure Hunt

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

Start Hunting!

Translated by