How regionprops orders the regions ?
Show older comments
Hello all,
I am confused how regionprops organizes the results? I have a picture that has 7 dark spots on it and need to calculate the area of each.Using regionprops, it shows the areas in pixels but in a confusing order! Is there anyway that I can get the results in an understandable order? like vertically or horizontally with the corresponding area?
Thank you Faraz
Accepted Answer
More Answers (1)
Sippapas Mongkoldao
on 29 Aug 2020
0 votes
Hi , Sorry for digging again .
I just wonder if I can order it by left-to-right , top-to-bottom and row-by-row instead of column-by-column ? So it can detect the top piece first , roll to the right and detect the next piece under it. Is it possible to use the regionprops on this condition or someone has an code to share with me ?
Thanks a lot
5 Comments
Image Analyst
on 29 Aug 2020
Edited: Image Analyst
on 29 Aug 2020
Unless you want to manually figure out the new indexes to reorder it to, based on the top y value of each blob (which can be a bit tricky), the easiest way is to just transpose the image before call bwlabel(), bwconncomp(), or regionprops().
Some questions to consider on the label ordering:
- Do you want to sort based on the top pixel in the blob (coin), or do you want to order based on the centroid of the blob? These could results in different orderings
- What if the top of one blob (or it's centroid) is above and to the right of of a "previous" blob that is slightly lower but to its left? Which one should come first?
Sippapas Mongkoldao
on 23 Oct 2020
Edited: Sippapas Mongkoldao
on 23 Oct 2020
- Sorting based on top pixel
- Left should come first in the same row
This is image that already applied regionprops and I still struggle how to order each regions by not using subimage or imcrop. Now, I'm trying to use imrotate but it still have some errors. If you have any advice, I would really appreciate it!

Image Analyst
on 23 Oct 2020
Edited: Image Analyst
on 24 Oct 2020
I think you should use kmeans on the y centrolds to compute 14 rows. Then use kmeans() on the x centroids to get 8 columns. Then you can relabel the image according to the sort order you want.
[labeledImage, numBlobs] = bwlabel(mask);
props = regionprops(labeledImage, 'Centroid');
centroids = vertcat(props.Centroid)
xc = centroids(:, 1);
yc = centroids(:, 2);
[xClass, xClassCentroids] = kmeans(xc, 8);
[yClass, yClassCentroids] = kmeans(yc, 14);
% Sort them
xClassCentroids = sort(xClassCentroids, 'ascend');
yClassCentroids = sort(yClassCentroids, 'ascend');
Next I'd scan over each blob in labeledImage and determine which row and column index it belongs to and make a new labeled image with that index. Something like (untested)
labeledImage2 = labeledImage;
for k = 1 : numBlobs
xDiffs = xClassCentroids - xc(k);
yDiffs = yClassCentroids - yc(k);
% Find which class centroid this blob is closest to.
[minXDistance, xIndex] = min(xDiffs);
[minYDistance, yIndex] = min(yDiffs);
newLabel = 14 * (xIndex - 1) + yIndex;
thisBlob = ismember(labeledImage, k)
% Give the blob the new label
labeledImage2(thisBlob) = newLabel;
end
% Now, re-measure the image with the new labels that are sorted.
props = regionprops(labeledImage2, 'Centroid');
Adapt as needed.
Sippapas Mongkoldao
on 24 Oct 2020
Edited: Sippapas Mongkoldao
on 24 Oct 2020
This seem logically works. I'm trying now and facing some problems.
- This line is shown as error ''too few argument". What is happening?
thisBlob = ismember(labeledImage)
2. Do you have some ideas how to get rid of undesired blobs (the top-left one and the 2 in below-right)?
Appreciate!
Image Analyst
on 24 Oct 2020
- You need to pass k into ismember(): thisBlob = ismember(labeledImage, k)
- If the bad blobs are in known rows, then you can erase them
% Erase top part
mask(1:topRow, :) = false;
% Erase bottom part.
mask(bottomRow : end, :) = false;
Categories
Find more on Region and Image Properties 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!