MATLAB: Can anyone explain me these lines

bounding boximage processingMATLAB

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

Best 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"