Dr Ema
Dr Ema

Reputation: 35

How to display the connected component through a specific pixel coordinate

I have a gray scale image add1, however there are only two pixel intensities in it (0 for Black and 255 for White). I am able to track the coordinate of my pixel of consideration i.e. add1(i,j). now I want to display the connected component of which this pixel is part of. I have tried it with the regionprop using 'PixelIdxList' and 'PixelList' unsuccesfully.
Can someone help please.Thanks in advance.

Upvotes: 1

Views: 634

Answers (2)

Shai
Shai

Reputation: 114826

If I understand your question, what you want is: given a specific coordinates (i,j) what is the label, and mask of the connected component that (i,j) is part of.

add = bwlabel( add1 ); %// convert to label mask
lij = add(i,j); %// get the label to which i,j belongs to
figure;
imshow( add == lij, [] ); %// select only the relevant label

Upvotes: 1

Ankit Dixit
Ankit Dixit

Reputation: 750

As much i understand you want this:

            clc
            clear all
            close all

            im = imread('labelProb.png');
            im = im2bw(im);

            labelIm = bwlabel(im);
            rg = regionprops(im,'PixelIdxList','Centroid');

            figure,imshow(labelIm,[]),hold on
            for i = 1:length(rg)
                cc = rg(i).Centroid;
                text(cc(1),cc(2),['label: ',num2str(i)],'Color','b','FontSize',9)
            end
            f = getframe();
            lab = frame2im(f);
            hold off

            % suppose you want label number 3 only.

            cc = rg(3).Centroid; % this is your pixel index;
            % Extract label number through this index.
            cc = round(cc);
            labelNumber = labelIm(cc(2),cc(1));

            % create a new blank image.
            blankImage = false(size(im));

            for i = 1:length(rg)
                if i == labelNumber
                    blankImage(rg(i).PixelIdxList) = true;
                end
            end
            figure,imshow(blankImage,[])

And result of above execution are:

enter image description here

enter image description here

enter image description here

Upvotes: 2

Related Questions