How regionprops orders the regions ?

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

 Réponse acceptée

Image Analyst
Image Analyst le 1 Juin 2017

4 votes

The blobs are labeled/ordered/identified/numbered by bwconncomp() or bwlabel(). The order is column major order like most things in MATLAB.
So it goes down the image starting in the upper left pixel and goes down the first column. If it "sees" a pixel that's part of a blob that is not yet labeled, then it does a region growing to label that whole blob, even if it goes off into other rows and columns to the right or above. Then, once that blob is labeled, it continues on to find any other blobs. It proceeds like this top-to-bottom, left-to-right (i.e. column-by-column) until it has found and labeled every blob.
Does that explain it well enough?

1 commentaire

Tala Hed
Tala Hed le 2 Juin 2017
Modifié(e) : Walter Roberson le 30 Août 2020
yes. Thanks a lot. I also found a function online which labels the regions. The function exactly works as you explained. here is the function https://www.mathworks.com/matlabcentral/fileexchange/19665-visualize-output-of-bwlabel

Connectez-vous pour commenter.

Plus de réponses (1)

Sippapas Mongkoldao
Sippapas Mongkoldao le 29 Août 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 commentaires

Image Analyst
Image Analyst le 29 Août 2020
Modifié(e) : Image Analyst le 29 Août 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:
  1. 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
  2. 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
Sippapas Mongkoldao le 23 Oct 2020
Modifié(e) : Sippapas Mongkoldao le 23 Oct 2020
  1. Sorting based on top pixel
  2. 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
Image Analyst le 23 Oct 2020
Modifié(e) : Image Analyst le 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
Sippapas Mongkoldao le 24 Oct 2020
Modifié(e) : Sippapas Mongkoldao le 24 Oct 2020
This seem logically works. I'm trying now and facing some problems.
  1. 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!
  1. You need to pass k into ismember(): thisBlob = ismember(labeledImage, k)
  2. 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;

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by