Clear Filters
Clear Filters

Error when using maskrcnn/segmentObjects : "Index in position 2 exceeds array bounds"

3 views (last 30 days)
Sorry for my poor English.
I am trying to detect objects in images using mask r-cnn. The following error occurs when a particular image is input.
Index in position 2 exceeds array bounds
Error in vision.internal.cnn.boxUtils.xywhToX1Y1X2Y2 (line 81)
boxes(:,3) = boxes(:,1) + boxes(:,3) - 1;
Error in maskrcnn/segmentObjectsInBatch (line 1012)
maskBranchBoxes = vision.internal.cnn.boxUtils.xywhToX1Y1X2Y2(...
Error in maskrcnn/segmentObjectsInDatastore (line 1141)
segmentObjectsInBatch(obj, imBatch, executionEnvironment);
Error in maskrcnn/segmentObjects (line 781)
segmentObjectsInDatastore(obj, im,...
This error does not occur for all images, but for some. We checked the contents of the bounding box and found nothing more. Below are the successful and unsuccessful bounding boxes.
The bounding box is a NumObjects by-4 matrix with rows in the form [x y w h].
successful image bbox
37.1000000000000 554.800000000000 29.8900000000000 86.2000000000000
104.720000000000 596.290000000000 50.7200000000000 44.7100000000000
1.08000000000000 597.490000000000 43.3400000000000 43.5100000000000
failed image bbox
438.600000000000 253.660000000000 12.3500000000000 31.1200000000000
380.640000000000 254.060000000000 15.5000000000000 29.1100000000000
341.290000000000 232.960000000000 13.6900000000000 24.1100000000000
331.980000000000 229.080000000000 12.6800000000000 16.4400000000000
480.660000000000 251.190000000000 15.0800000000000 27.7500000000000
432.990000000000 229.670000000000 11.4500000000000 25.0600000000000
409.420000000000 246.130000000000 8.05000000000000 24.7500000000000
413.580000000000 248.760000000000 15.8500000000000 32.9500000000000
If you have seen this kind of problem outside of Mask R-CNN, any information would be appreciated.
Thanks.

Answers (1)

Shubh
Shubh on 25 Jan 2024
Hi Tatsuya,
The error you're encountering in MATLAB while using Mask R-CNN suggests that there's an issue with the bounding box (bbox) calculations for certain images. The error message:
"Index in position 2 exceeds array bounds"
indicates that the code is trying to access an array element that does not exist. This often happens in object detection tasks when the calculated coordinates of a bounding box extend outside the dimensions of the image.
In your case, the problem seems to occur in the conversion of bbox format from [x y w h] (x-coordinate, y-coordinate, width, height) to [x1 y1 x2 y2] (top-left and bottom-right coordinates). The specific line causing the error:
boxes(:,3) = boxes(:,1) + boxes(:,3) - 1;
is trying to calculate the x2 coordinate (right side of the bbox) but goes out of the image bounds.
To address this issue, you should add a check before this line to ensure that the 'x2' and 'y2' coordinates do not exceed the image dimensions. Here is a modification of the relevant part of your code to include this check:
% Assuming you have the image dimensions as imgWidth and imgHeight
% Loop through each bounding box and adjust if necessary
for i = 1:size(boxes, 1)
% Calculate x2 and y2 while ensuring they don't exceed image bounds
boxes(i, 3) = min(boxes(i, 1) + boxes(i, 3) - 1, imgWidth);
boxes(i, 4) = min(boxes(i, 2) + boxes(i, 4) - 1, imgHeight);
end
Before using this code, ensure you have the image width (imgWidth) and height (imgHeight) available. These values should correspond to the dimensions of the image being processed.
This modification will prevent the bounding box coordinates from exceeding the image's boundaries, thus avoiding the index out-of-bounds error. However, keep in mind that this is a workaround, and it's essential to further investigate why certain bounding boxes are calculated to be outside the image dimensions. It could be due to incorrect bbox predictions from the Mask R-CNN model or issues with the input data.
Hope this helps!
  1 Comment
Tatsuya Onishi
Tatsuya Onishi on 27 Jan 2024
Hi, Shubh. Thanks again and again for your help! Your advice has helped me understand what is causing the error. The part where the error is occurring is in a built-in function, so I can see it but not edit it.
Perhaps the bounding box is outside the image due to the runtime threshold. In fact, when I changed the threshold, the number of images that could be loaded increased.
One thing I don't understand is that the images used are validation data, so I did not create them myself. Why is it common for this to happen with validation images?

Sign in to comment.

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!