0

I have a binary image (attached) with connected components isolated with bwconncomps. I'm trying to identify the outline of each of these components but in a way where I can still refer back to the filled object-(I am using the outline as a mask on a greyscale image to pull some value and then depending on that value performing an operation on the filled original region of interest)

When I run bwconncomps on the attached image I get 814 objects identified. I can run bwmorph(D,'remove'); and I get the outlines/perimeters of the objects but when I run bwconncomps on this I get 827 objects-(not sure where these extra objects are coming from and this screws up my ability to refer back to the filled object based on the value I pull from its' outline).

Basically I need a version of bwmorph(D,'remove') that will leave the same number of connected components as seen in bwconncomps of the original binary image..so that I can compare component #30 in the original binary to the outline of the same #30 in bwconncomps.

Hope this was clear, any suggestions?

Thanks

enter image description here

user3470496
  • 141
  • 7
  • 33

1 Answers1

1

You can use bwboundaries to find the pixels boundaries of the white connected components, such that each connected component has a corresponding set of boundaries.

%calculate boundries and generate boundry mask
B = bwboundaries(im,'noholes');
boundriesImage = zeros(size(im));
boundriesPixels = cell2mat(B);
boundriesImage(sub2ind(size(im),boundriesPixels(:,1),boundriesPixels(:,2)))=1;

%finds the connected component in the original image and in the boundry
%mask
CC = bwconncomp(im);
CC2 = bwconncomp(boundriesImage);

Result: CC and CC2 contain the same number of connected components

CC = 

    Connectivity: 8
       ImageSize: [535 1571]
      NumObjects: 814
    PixelIdxList: {1x814 cell}

CC2 = 

    Connectivity: 8
       ImageSize: [535 1571]
      NumObjects: 814
    PixelIdxList: {1x814 cell}

Also, each connected component CC2{ii} matches to it's CC{ii} as can be seen by the following test results:

%tests that for each ii, CC{ii} is contained in CC{i}
CC2MatchesToCC1 = true;
for ii=1:length(CC.PixelIdxList)
    if length(intersect(CC2.PixelIdxList{ii},CC.PixelIdxList{ii}))~=length(CC2.PixelIdxList{ii})
        CC2MatchesToCC1 = false;
    end
end

result:

CC2MatchesToCC1 =

 1
ibezito
  • 5,782
  • 2
  • 22
  • 46