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

connected componentimage processingImage Processing Toolboximage segmentationocr

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..

Best Answer

  • 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
    Note that you can instead directly call regionprops on the binary image BW. regionprops will internally invoke bwconncomp for you.
    stats = regionprops(BW,'Image');