How to use regionprops on connected component labels

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

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)

regionprops() accepts labeled arrays directly, and returns the requested properties for each distinct label.

2 Comments

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.
You need to relabel properly. See my answer.

Sign in to comment.

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

Ok, I get the idea conceptually, but I am not sure how I will identify which blobs are part of one connected region for relabeling. The properties I can get out of regionprops includes 'PixelList', but it doesn't seperate the pixels into connected blobs as far as I can tell, so I feel like I would have to then call regionprops using the 'PixelList' to identify continuous blobs, but that would computationally take me back to where I started having to call regionprops for every label value in my matrix.
On a side note, I don't know how much it matters, but every label is representing multiple blobs, so the first calling of regionprops will always result in a Euler number greater than one.
Thank you so much for the help!
Upload an image that I can work with.
Ok this should have an example image for you to look at. Let me know if it doesn't work, this is my first time trying to share a file on mathworks.
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...
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

Asked:

on 18 Mar 2013

Community Treasure Hunt

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

Start Hunting!