Why the start point of boundingbox of regionprops is not integers, but offset with 0.5?

6 ビュー (過去 30 日間)
raym
raym 2024 年 12 月 9 日
編集済み: Matt J 2024 年 12 月 10 日
I am trying to use the boundingbox to create the index to crop the tile, but find that the start position is not integer.
Can I use [ceil(CC2.BoundingBox(1)),ceil(CC2.BoundingBox(2))] to specify the upper-left position of the box?
Thank you.
a = [0,0,0,0;0,0,0,1;0,0,1,1;]
CC2 = regionprops(a,a,{'Area','Centroid','PixelIdxList','MaxIntensity','BoundingBox'})
aCellTile = zeros(2,2);
[aCellTileIdxY,aCellTileIdxX] = ind2sub([3,4],CC2.PixelIdxList);
aCellTileIdxX = aCellTileIdxX-ceil(CC2.BoundingBox(1))+1;
aCellTileIdxY = aCellTileIdxY-ceil(CC2.BoundingBox(2))+1;
aTilePixelIdxList = sub2ind([CC2.BoundingBox(4),CC2.BoundingBox(3)],aCellTileIdxX,aCellTileIdxY);
aCellTile(aTilePixelIdxList) = 1;

採用された回答

Matt J
Matt J 2024 年 12 月 9 日
編集済み: Matt J 2024 年 12 月 10 日
In the conventions of regionprops(), the centers of the pixels have coordinates at the natural integers. The coordinates that BoundingBox gives you are the coordinates of the upper-left corner of the upper-left pixel in the box. Therefore, it's coordinates will be 0.5 pixels off the pixel center.
To index the bounding box, however, you should really be using the SubarrayIdx property, e.g.,
a = [0,0,0,0;0,0,0,1;0,0,1,1]
a = 3×4
0 0 0 0 0 0 0 1 0 0 1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
RP = regionprops(logical(a),{'SubarrayIdx'});
a(RP(1).SubarrayIdx{:})=17
a = 3×4
0 0 0 0 0 0 17 17 0 0 17 17
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by