4
Object Sort Order for bwlabel, bwconncomp, and regionprops
source link: https://blogs.mathworks.com/steve/2023/01/30/bwlabel-bwconncomp-regionprops-order/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Object Sort Order for bwlabel, bwconncomp, and regionprops » Steve on Image Processing with MATLAB
Someone recently asked me about the order of objects found by the functions bwlabel, bwconncomp, and regionprops. In this binary image, for example, what accounts for the object order?
The functions bwlabel and bwconncomp both scan for objects in the same direction. They look down each column, starting with the left-most column. This animation illustrates the search order:
Because of this search procedure, the object order depends on the top, left-most pixel in each object. Specifically, the order is a lexicographic sort of (c,r)(c,r) coordinates, where c and r are the column and row coordinates of the top, left-most pixel.
If you pass a binary image to regionprops, the resulting object order is the same as for bwconncomp, and that is because regionprops calls bwconncomp under the hood.
Sometimes, people who are working with images of text ask to change things so that the search proceeds along rows instead of columns. The motivation is to get the object order to be same as the order of characters on each line. (Assuming a left-to-right language, that is.) That generally doesn't work well, though. Consider this text fragment:
With a row-wise search, the letter "b" would be found first, followed by "L" and then "t." This animation shows why that happens:
If you want a different sort order, I recommend returning the regionprops output as a table, and then you can use sortrows. Here are a couple of examples.
url = "https://blogs.mathworks.com/steve/files/rice-bw-ul.png";
A = imread(url);
imshow(A)
props = regionprops("table",A,["Centroid" "Area"])
props = 11×2 table
Area | Centroid | ||
---|---|---|---|
1 | 138 | 5.9855 | 35.0870 |
2 | 87 | 5.3678 | 60.3563 |
3 | 208 | 24.1971 | 26.3125 |
4 | 199 | 28.1859 | 48.2563 |
5 | 61 | 23.8197 | 4.3443 |
6 | 20 | 26.7500 | 62.3500 |
7 | 189 | 43.6349 | 19.8783 |
8 | 97 | 43.8454 | 58.5361 |
9 | 115 | 58.2087 | 7.0696 |
10 | 70 | 61.7857 | 58.2286 |
11 | 26 | 62.9231 | 31.8846 |
You could sort the objects according to their centroids, with the primary sort in the vertical direction and the secondary sort in the horizontal direction.
props.Centroid_x = props.Centroid(:,1);
props.Centroid_y = props.Centroid(:,2);
props_by_row = sortrows(props,["Centroid_y" "Centroid_x"])
props_by_row = 11×4 table
Area | Centroid | Centroid_x | Centroid_y | ||
---|---|---|---|---|---|
1 | 61 | 23.8197 | 4.3443 | 23.8197 | 4.3443 |
2 | 115 | 58.2087 | 7.0696 | 58.2087 | 7.0696 |
3 | 189 | 43.6349 | 19.8783 | 43.6349 | 19.8783 |
4 | 208 | 24.1971 | 26.3125 | 24.1971 | 26.3125 |
5 | 26 | 62.9231 | 31.8846 | 62.9231 | 31.8846 |
6 | 138 | 5.9855 | 35.0870 | 5.9855 | 35.0870 |
7 | 199 | 28.1859 | 48.2563 | 28.1859 | 48.2563 |
8 | 70 | 61.7857 | 58.2286 | 61.7857 | 58.2286 |
9 | 97 | 43.8454 | 58.5361 | 43.8454 | 58.5361 |
10 | 87 | 5.3678 | 60.3563 | 5.3678 | 60.3563 |
11 | 20 | 26.7500 | 62.3500 | 26.7500 | 62.3500 |
imshow(A)
for k = 1:height(props_by_row)
x = props_by_row.Centroid_x(k);
y = props_by_row.Centroid_y(k);
text(x,y,string(k),Color = "yellow", BackgroundColor = "blue", FontSize = 16, ...
FontWeight = "bold", HorizontalAlignment = "center",...
VerticalAlignment = "middle");
For another example, you could sort by area.
props_by_area = sortrows(props,"Area","descend")
props_by_area = 11×4 table
Area | Centroid | Centroid_x | Centroid_y | ||
---|---|---|---|---|---|
1 | 208 | 24.1971 | 26.3125 | 24.1971 | 26.3125 |
2 | 199 | 28.1859 | 48.2563 | 28.1859 | 48.2563 |
3 | 189 | 43.6349 | 19.8783 | 43.6349 | 19.8783 |
4 | 138 | 5.9855 | 35.0870 | 5.9855 | 35.0870 |
5 | 115 | 58.2087 | 7.0696 | 58.2087 | 7.0696 |
6 | 97 | 43.8454 | 58.5361 | 43.8454 | 58.5361 |
7 | 87 | 5.3678 | 60.3563 | 5.3678 | 60.3563 |
8 | 70 | 61.7857 | 58.2286 | 61.7857 | 58.2286 |
9 | 61 | 23.8197 | 4.3443 | 23.8197 | 4.3443 |
10 | 26 | 62.9231 | 31.8846 | 62.9231 | 31.8846 |
11 | 20 | 26.7500 | 62.3500 | 26.7500 | 62.3500 |
imshow(A)
for k = 1:height(props_by_area)
x = props_by_area.Centroid_x(k);
y = props_by_area.Centroid_y(k);
text(x,y,string(k),Color = "yellow", BackgroundColor = "blue", FontSize = 16, ...
FontWeight = "bold", HorizontalAlignment = "center", ...
VerticalAlignment = "middle");
title("Objects sorted by area (biggest to smallest)")
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK