Can anyone explain me these lines?

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));

 Accepted Answer

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 Comments

Thank you so much. It is really helpful. I have some more doubts. I hope you respond to those questions also. Once again thank you so much.
I is my input image
h = size(I,1);
midx = round(STATS.Centroid(1));
plot([midx,midx],[h,1],'linewidth',1);
Please explain me the parameters inside plot() function. I don't understand what [h 1] represents. Thank you
h would be the number of rows in "I", which would be the height of "I".
midx is going to be the x coordinate of the single centroid (or an error if there are multiple objects in the image).
What the plot doing is drawing a vertical line at the x coordinate of the centroid -- a line between y = the height and y = 1
Thank you so much. I already asked about bounding box. Please explain me these lines too.
startj=1;
endj=floor(min(STATS.BoundingBox(1) + STATS.BoundingBox(3)-midx+1, midx- STATS.BoundingBox(1)+1));
The first part of the min() is the distance between the centroid and the right edge. The second part of the min() is the distance between the centroid and the left edge. The min() is therefore figuring out the distance of the closer edge to the centroid. I am not sure why you would want to do that.
Thank you. I wanted to perform horizontal scan(left to right). For that purpose, starting and ending positions are determined by above those two steps.
what does zeros(endi-starti+1,1); returns?
Thank you.
zeros(K,1) for any integer value K, returns a column vector of zeros which is K long. For example zeros(5,1) would be [0; 0; 0; 0; 0] which is
0
0
0
0
0
Thank you so much
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

Sign in to comment.

More Answers (0)

Categories

Products

Community Treasure Hunt

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

Start Hunting!