Error when using maskrcnn/segmentObjects : "Index in position 2 exceeds array bounds"
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
Shubh
2024-1-25
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!
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Computer Vision with Simulink 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!