Can anyone explain me these lines?
3 次查看(过去 30 天)
显示 更早的评论
Iam new to image processing. Please explain these lines. what does BoundingBox(2) and BoundingBox(4) mean? Iam trying to form bounding box around brain tumour. How can these lines help me?
starti=round(STATS.BoundingBox(2));
endi=round(STATS.BoundingBox(2) + STATS.BoundingBox(4));
0 个评论
采纳的回答
Walter Roberson
2016-3-6
Bounding boxes extracted by regionprops are vectors of 4 items per region. The first item in the vector is the x coordinate of the left of the box. The second item in the vector is the y coordinate of the bottom of the box. The third item in the vector is the width of the box. The fourth item in the vector is the height of the box.
The second item plus the fourth item is thus the row (y) of the bottom of the box, plus the height of the box. The total will be the row (y) that is just past the top of the box.
The code is likely in error. Usually what is desired is to find the row of the bottom of the box and the row of the top of the box so that you can index. That would be
starti = STATS.BoundingBox(2);
endi = starti + STATS.BoundingBox(4) - 1;
and then you would be able to do
TheImageArray(starti : endi, ....)
Remember, that if something starts at (say) row 3 and is (say) 5 high, then that would be rows 3, 4, 5, 6, 7 -- 5 rows including the starting row. The calculation in the code you were looking at would be endi = 3 + 5 = 8, but row 8 is past the top of the box.
People make this mistake with bounding boxes a lot. It is often not noticed, and often does not affect much. But if the bounding box happens to end at the edge of the image, then that "one too far" would try to index past the end of the array.
"There are 2 hard problems in computer science: caching, naming, and off-by-1 errors"
10 个评论
Image Analyst
2016-3-8
A slight correction. The bounding box does not actually lie ON the left most and top most line of pixels, it lies in between the lines of pixels. Try it with a small example and you'll see.
b = false(4,7);
b(2:3, 4:6) = true;
m = regionprops(logical(b), 'BoundingBox')
m.BoundingBox
startRow = ceil(m.BoundingBox(2))
endRow = startRow + m.BoundingBox(4) - 1
b =
0 0 0 0 0 0 0
0 0 0 1 1 1 0
0 0 0 1 1 1 0
0 0 0 0 0 0 0
m =
BoundingBox: [3.5 1.5 3 2]
ans =
3.5 1.5 3 2
2
endRow =
3
The first element is 0.5 pixels to the left of the first column with something in it, and the top row is half a pixel above the first row with something in it. This is so that the bounding box actually contains the image, whereas if it were right on top of it, it could be somewhat ambiguous whether those columns and rows were "inside" the box or not.
The width and height are correct using pixel center-to-pixel center paradigm. So if you want to crop with indexing you'd have to do
startRow = ceil(m.BoundingBox(2))
endRow = startRow + m.BoundingBox(4) - 1
startCol = ceil(m.BoundingBox(1))
endCol = startCol + m.BoundingBox(3) - 1
startRow =
2
endRow =
3
startCol =
4
endCol =
6
Just as you expect and see in the "b" image above.
As far as cropping goes, note the different ways imcrop() and indexing operate.
croppedImage = imcrop(b, m.BoundingBox)
croppedImage2 = b(startRow:endRow, startCol:endCol)
croppedImage =
1 1 1 0
1 1 1 0
0 0 0 0
croppedImage2 =
1 1 1
1 1 1
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Convert Image Type 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!