How to use regionprops on connected component labels
11 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
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
0 Comments
More Answers (2)
Walter Roberson
on 18 Mar 2013
regionprops() accepts labeled arrays directly, and returns the requested properties for each distinct label.
2 Comments
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
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...
See Also
Categories
Find more on Particle Swarm in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!