bwconncomp returns a variable with 4 fields. one of them is pixelIdxlist. it contains the connected component list.how can we extract each component and show in different images.

4 views (last 30 days)
in my code pixelIdx has 4 components:
PixelIdxList: {[866x1 double] [628x1 double] [1092x1 double] [4x1 double]}
how can I store each component in a different image so that it can b processed further..
can anyone plz help me to write the code for this..

Accepted Answer

Anand
Anand on 4 Dec 2013
You can use the regionprops function with 'Image' as the requested statistic and pass the output of bwconncomp to it. Your code would look like this:
BW = imread('text.png');
CC = bwconncomp(BW);
stats = regionprops(CC,'Image');
%Display the first component as an image
imshow(stats(1).Image);
Note that you can instead directly call regionprops on the binary image BW. regionprops will internally invoke bwconncomp for you.
stats = regionprops(BW,'Image');

More Answers (1)

Image Analyst
Image Analyst on 4 Dec 2013
I know this is old, but if anyone wants to do this, each blob that was measured (4 in Gaur's case) will have a label if you called bwlabel() or bwconncomp() prior to calling regionprops(). To get each by itself, you use ismember():
[labeledImage, numberOfBlobs] = bwlabel(binaryImage);
measurements = regionprops(labeledImage);
for blob = 1 : numberOfBlobs
thisBlob = ismember(labeledImage, 1);
% Display blob in a new figure.
figure;
imshow(thisBlob);
% Or use imwrite if you want to save to disk.
eightBitImage = uint8(255*(thisBlob>0));
filename = sprintf('Blob #%d.png', blob);
imwrite(eightBitImage, filename);
end
You can also look at my Image Segmentation Tutorial to see how you can use the BoundingBox property if you want the blob cropped out to form a smaller image instead of the above, which has the full sized image.
  1 Comment
Iman Haghgooie
Iman Haghgooie on 6 Aug 2015
Hi Image Analyst, bwlabel is the most common way of labeling connected pixels. But it is memory-intensive and computationally expensive. bwconncomp provides much better performance for real-time applications. Pls see below.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!