Why Index out of range?

7 views (last 30 days)
VicPeters
VicPeters on 21 Apr 2016
Commented: VicPeters on 21 Apr 2016
Hello everyone, I need assistance. I got an error "index exceeds matrix dimension"
with accessing the intensity of R, G and B components of an image. My code is as shown below:
rgb=imread('car3.jpg'); %# Load an RGB image
rgb=num2cell(rgb);
rPlane = zeros(size(rgb,1),size(rgb,2));
gPlane = zeros(size(rgb,1),size(rgb,2));
bPlane = zeros(size(rgb,1),size(rgb,2));
rPlane=[size(rgb,1),size(rgb,2)];
gPlane=[size(rgb,1),size(rgb,2)];
bPlane=[size(rgb,1),size(rgb,2)];
for i = 1:size(rgb,1) %rows
for k=1:size(rgb,2) %columns
rPlane=rgb{i,k}(:,:,1);% j=1(:,:,k);
gPlane=rgb{i,k}(:,:,2);
bPlane=rgb{i,k}(:,:,3);%j=3
end
end
The error occurs at "gPlane=rgb{i,k}(:,:,2);" inside the nested for loop. The car3.jpg picture has the following dimension: rgb(302,435,:)

Answers (1)

Walter Roberson
Walter Roberson on 21 Apr 2016
The result of num2cell() is going to be a cell array which each entry is a single scalar value. You are trying to access the third dimension of that scalar.
  3 Comments
Walter Roberson
Walter Roberson on 21 Apr 2016
rgb=imread('car3.jpg'); %# Load an RGB image
rPlane = rgb(:,:,1);
gPlane = rgb(:,:,2);
bPlane = rgb(:,:,3);
No loop needed.
VicPeters
VicPeters on 21 Apr 2016
Wao... it's so short? Thanks Walter, I remain very grateful to you!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!