error : Index in position 3 exceeds array bounds (must not exceed 1).

please help
I=imread('AFTER.tif');
[R C] = find(I(:,:,1)<100 & I(:,:,2)>0.2 & I(:,:,3)>10); error
imagesc(I)% a is the image
% rows and columns of area found by thresholding
n=randi([50 length(R)],1,300);%randomly generated indexes
r=R(n);c=C(n);% extraction of entries corresponding to randomly generated values in row and column vectors
for ii=1:length(n)
subpatch=I(r(ii)-75:r(ii)+75,c(ii)-75:c(ii)+75);%for patches of size 151 by 151
imwrite(subpatch,strcat('C:\Users\user\Documents\MATLAB\before\p',num2str(ii),'.tif') );
end

10 Comments

What is l?
To the best of my knowledge, find doesn't have any problem looking to different sheets of a matrix, especially when only a single sheet is called at a time, so it must mean that something is up with l.
You might also try removing [R, C], and just replace it with a single variable.
After the line
I=imread('AFTER.tif');
type
size(I)
What do you get ?
i get the same error
but withe the answer
this
patching
ans =
1450 1733
Index in position 3 exceeds array bounds (must not exceed 1).
Error in patching (line 5)
[R, C] = find(I(:,:,1)>100 & I(:,:,2)>0.2 & I(:,:,3)>100);
... which explains the error since I is only two-dimensional (1450x1733).
BUt even is two dimension i alter the code the error remains the same
How did you alter the code ?
You can't access l(:,:,2) and l(:,:,3) in the find-command.
@Akash Kuber: If you change the code, of course you have to post the changed code also if you want us to identify, what the error is.
If I is a [1450 x 1733] matrix, you cann check I(:,:,3)>100, because there is no I(:, :, 3), exactly as the error message tells you.
i have jus added find(I >100);
is there anyway to resolve this?
this is the code it works not giving out rgb image but greyscale image
clc;clear all;close all
%samle 3D matrix, e.g. can be an RGB image
a=imread('BEFORE.tif') ;
size(i);
[R, C] = find(I(:,:,1)<1450);
imagesc(I)% a is the image
% rows and columns of area found by thresholding
n=randi([1 length(R)],1,300);%randomly generated indexes
r=R(n);c=C(n);% extraction of entries corresponding to randomly generated values in row and column vectors
for ii=1:length(n)
subpatch=I(r(ii)-75:r(ii)+75,c(ii)-75:c(ii)+75);%for patches of size 151 by 151
imwrite(subpatch,strcat('C:\Users\user\Documents\MATLAB\before\p',num2str(ii),'.tif') );
end
Does this mean, that the problem is solved?
"i have jus added find(I >100);" - but we do not know where you have added this.

Sign in to comment.

Answers (1)

Hi,
I understand that you are facing the error – ‘Index in position 3 exceeds array bounds (must not exceed 1) while trying to access matrix ‘I’ used to store an image file.
Error exceeding array bounds is caused when you try to access an element out of matrix dimensions.
So before trying to access indices of matrix ‘I’ check whether it’s a RGB image or a grayscale image
as in case of grayscale image you should not have – I(:,:,3) or I(:,:2) ’.
To check whether it’s a RGB or grayscale image try :
st = imfinfo(filename);
st.ColorType

4 Comments

Request :- Would you solve the error
img=imread('C:\Users\Bipin\Desktop\BRAINTUMOUR\1.jpg');
im=imresize(img,[256 256],'bilinear');
imwrite(im,'C:\Users\Bipin\Desktop\BRAINTUMOUR\result\inpimg.png');
imshow(im);
global he
B = strel('disk',10);
he=imclose(im,B);
imshow(he);
title('Preprocessed Image');
global nimg
global he
g=he(:,:,2);
ne=g>150;
binaryImage=ne;
binaryImage = imclearborder(binaryImage);
cup=imfill(binaryImage,'holes');
se1=strel('disk',1);
di=imdilate(cup,se1);
BW = bwareafilt(di, 1);
R=he(:,:,1);
G=he(:,:,2);
B=he(:,:,3);
R(BW)=0;
G(BW)=0;
B(BW)=0;
nimg=cat(3,R,G,B);
imshow(nimg);
imwrite(nimg,'C:\Users\Bipin\Desktop\BRAINTUMOUR\result\segimg.png');
ERROR :->> brain1
Index in position 3 exceeds array bounds (must not exceed 1).
Error in brain1 (line 14)
g=he(:,:,2)
D3 = size(I,3);
if ndims(I) > 3
fprintf('image is probably multispectral or a time series\n');
elseif D3 == 1
fprintf('image is very likely grayscale\n')
elseif D3 == 2
fprintf('Strange image. Might be grayscale + alpha. They exist, but pretty uncommon\n');
elseif D3 == 3
fprintf('image is most likely RGB\n');
elseif D3 == 4
fprintf('if this is a PNG image it is most likely RGBA. If it is a TIFF image it is more likely RGBA but might be CMYK or GeoTIFF\n');
else
fprintf('This must surely be a TIFF image. It might be multispectral or R+G+B+A+Ir or something more odd\n');
end
Your image 1.jpg is not an rgb image.

Sign in to comment.

Products

Release

R2018b

Tags

Asked:

on 2 Jul 2019

Commented:

on 9 Mar 2021

Community Treasure Hunt

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

Start Hunting!