How to use regionprops on connected component labels

12 views (last 30 days)
Hi Everyone,
I have a matrix generated from an image consisting of a number of labeled objects. The ojects touch eachother so it is necessary for them to be labels and not represented as binary. However, since there are many objects the labels are used more than once to represent different objects. it looks something like the example below. There are also zeros to repersent a lack of objects in that region.
1 1 1 2 2 0 0
1 1 2 2 2 0 0
1 2 2 2 0 0 1
1 2 0 0 1 1 1
1 0 0 0 1 1 1
So in this example the ones in the top left repersent one object, the twos another and the ones in the bottom right a third.
I want to use regionprops on this image, but if I try to turn it into a bw image I can no longer distinguish the 1 and 2 regions in the top left and if I leave them as labels, the two 1 regions are read as a single object.
I could do something along these lines for the matrix,b ut it seems sloppy and inefficient. Also it would create an array of structures with the properties rather than one nice neat structure.
regions = [];
for i=1:max(max(labelmat))
label = labelmat;
label(find(label ~= i ) = 0;
label = im2bw(label,0);
regions = [regions regionprops(label,properties)];
end
If any has ideas I would be very greatful!
Jocelyn

Accepted Answer

Jeff E
Jeff E on 19 Mar 2013
What's really sloppy and inelegant is CellProfiler limiting the number of cells in a field to uint8. The below is my slight refinement to your solution, which also results in a single structure output:
fin_regions = [];
for i=1 : max(max(labelmat))
label = labelmat==i;
regions = regionprops(label,'Area');
fin_regions = cat(1, fin_regions, regions);
end

More Answers (2)

Walter Roberson
Walter Roberson on 18 Mar 2013
regionprops() accepts labeled arrays directly, and returns the requested properties for each distinct label.
  2 Comments
Jocelyn
Jocelyn on 18 Mar 2013
Yes I understand that, but it treats all regions with the same label as one region. In the example I gave, the two seperate regions both labeled with '1' would count as only one region. I want only connected components with the same label to count as a single region.

Sign in to comment.


Image Analyst
Image Analyst on 18 Mar 2013
You're going to have to relabel the image properly to have two distinct blobs with the same label be two different blobs with different label numbers. You do this by first calling regionprops and asking for the Euler number. Then for those blobs with an Euler number > 1, you have to reassign them to a new label that's greater than your greatest label number. You have to extract those blobs to a new image, then relabel, then add the greatest blob number to the new labeled image so that it has unique numbers not matching any of the old label numbers. Not real hard conceptually, but takes a few lines. Give it a try.
Then you have to call regionprops again to get the final measurements on the "fixed up" image. Not hard. If you can't do it, let me know and I might be able to do it later tonight if I have time.
  5 Comments
Image Analyst
Image Analyst on 19 Mar 2013
Edited: Image Analyst on 19 Mar 2013
Where did you get this image? It looks like some program tried to label it and did it incorrectly. This is how to fix it - you simply relabel it.
binaryImage = grayImage > 0;
labeledImage = bwlabel(binaryImage);
% Now you can use regionprops.
measurements = regionprops(labeledImage, 'all'); % Or whatever...
Jocelyn
Jocelyn on 19 Mar 2013
Sorry, that doesn't fix it. The image is the output of a CellProfiler pipeline that identifies nuclei in a tissue sample. Some of the nuclei touch eachother, so it needs labels to distinguish touching nuclei. However, the labels only go up to 255 so it starts repeating labels after that number. If I did what you are suggesting, it would label areas that have high nuclear density, so therefore many nuclei touching as one big ugly nuclei which would have a VERY different biological meaning.
Unfortunatly it is starting to sound like I may just have to call regionprops for each of the 255 labels. Oh well, thanks for the effort.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!