필터 지우기
필터 지우기

How to fix Index position 3 exceeds array bounds (must not exceed 1)

조회 수: 46 (최근 30일)
Erza Naziha
Erza Naziha 2022년 6월 6일
답변: dika 2024년 9월 24일 12:57
I'm doing cropping and zero padding for bounding box images. However, there is error 'Index in position 3 exceeds array bounds (must not exceed 1).' I don't understand the error and location of 'index in position 3' . Hence, I can't solve it.
Please help me with this problem. Below are my codes.
%% Bounding box
imbwlabel=bwlabel(I8); %bwlabel works only in binary (b&w,double) image
figure;
imshow(label2rgb(imbwlabel)); %this is important bcs it helps to create the bounding box
%in colored (uint8,unit16 etc) image and not in binary (b&w) image
bboxes=regionprops(imbwlabel,'BoundingBox');
[L, n]=bwlabel(I8);
bboxes=regionprops(I8,'BoundingBox','Centroid');
figure;imshow(I10);%title('Image with Bounding box');
axis image off
hold on
for k=1 : length(bboxes)
CurrBB=bboxes(k).BoundingBox;
% cent=cat(1,bboxes.BoundingBox);
% plot(cent(:,1),cent(:,2),'g*')
rectangle('Position', [CurrBB(1),CurrBB(2),CurrBB(3),CurrBB(4)], 'EdgeColor','m','LineWidth',2)
end
hold off
%% Crop image from bounding box
if ~isempty(bboxes)
for p=1:length(bboxes)
CurrBB=bboxes(p).BoundingBox;
obj = imcrop(I8,CurrBB);
[m, n, l]=size(obj);
if (m > 227 || n > 227)
obj=imresize(obj,[227 227]);
end
I12=zeros(227,227);
I12=uint8(I12);
for i=1:m-1
for j=1:n-1
for k=1:3
I12(i,j,k)=obj(i,j,k);
end
end
end
figure;imshow(I12);
% figure;imshow(obj);
imwrite(I12,sprintf('%dpad.png',p));
end
end

채택된 답변

KSSV
KSSV 2022년 6월 6일
편집: KSSV 2022년 6월 6일
This error is simple.. you are trying to extract more number of elements then present. I suspect, you have a black and white image and you are trying to consider it as RGB and tried to extarct extra index.
% Demo
I = rand(10,10,3) ; % 3D matrix
R = I(:,:,1) ; % no error
G = I(:,:,2) ; % no error
B = I(:,:,3) ; % no error
%
I = rand(10) ; % 2D matrix
I1 = I(:,:,1) ; % no error
I2 = I(:,:,2) ; % error, there is no second matrix
Index in position 3 exceeds array bounds. Index must not exceed 1.
In the same, way check the dimensions of the matrix, where you tried to extract like shown above.
  댓글 수: 3
Erza Naziha
Erza Naziha 2022년 6월 10일
hi again. Want to tell you that I've solved the problem already. Like you said, I'm expecting it to be RGB images but I accidently read the grayscale image. The error comes from this line
obj = imcrop(I8,CurrBB);
suppose to be I10 (RGB) instead I8(grayscale).
Thank so much for the hint!
KSSV
KSSV 2022년 6월 10일
Perfect....
Happy Coding....Enjoy MATLAB. :)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

dika
dika 2024년 9월 24일 12:57
I = imread('download.png');
R = I(:,:,1);
G = I(:,:,2);
Index in position 3 exceeds array bounds. Index must not exceed 1.
please tell me how to fix the code

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by