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

1 visualización (últimos 30 días)
Erza Naziha
Erza Naziha el 10 de Jun. de 2022
Comentada: Erza Naziha el 11 de Jun. de 2022
I've ask similar question before and solved the issue. However, I'm facing another error after running the codes. Can anyone tell me what does it mean by How to fix Index position 1 exceeds array bounds (must not exceed 227)? Btw, 227 is the size of image that I want after resizing for futher use in deep learning classification. Is there anything that I can change on the codes? Please help me.
%% Bounding box
% 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 and zero padding
if ~isempty(bboxes)
for p=1:length(bboxes)
CurrBB=bboxes(p).BoundingBox;
obj = imcrop(I10,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

Respuestas (1)

Simon Chan
Simon Chan el 10 de Jun. de 2022
After image resize, the size of the image is always 227x227 in terms of number of rows and columns.
However, the original image may be larger, so that variable m or n can be larger than 227.
I think in your case, the value of m is larger than 227 so that the value of index i can be larger than 227. However, it is not possible to extract value from variable obj for i>227 anymore and gives you an error.
for i=1:m-1
for j=1:n-1
for k=1:3
I12(i,j,k)=obj(i,j,k); % i can be 228 if m=300, for example.
end
end
end
Since the image size is always 227x227, you may just hardcode the index from 1 to 226 as follows:
for i=1:226
for j=1:226
for k=1:3
I12(i,j,k)=obj(i,j,k);
end
end
end
  1 comentario
Erza Naziha
Erza Naziha el 11 de Jun. de 2022
Hi. Thanks for answering. I've tried the codes given but same error still occur. However, I've found ways to resize the image that is larger than 227 by doing this
%%crop and zero padding
if ~isempty(bboxes)
for p=1:length(bboxes)
CurrBB=bboxes(p).BoundingBox;
obj = imcrop(I10,CurrBB);
[m, n, l]=size(obj);
% I don't know if this is necessary, since it doesn't cause error,
% I just leave it there, from here
if (m > 227 || n > 227)
obj=imresize(obj,[227 227]);
end
% until here
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);
% I added these codes (it runs perfectly) from here
if (m > 227 || n > 227)
obj=imresize(obj,[227 227]);
end
% until here
end
end
end
% figure;imshow(I12);
% figure;imshow(obj);
% imwrite(I12,sprintf('%dpad.png',p));
end
end

Iniciar sesión para comentar.

Categorías

Más información sobre Image Data Workflows en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by