Why Index out of range?
조회 수: 7 (최근 30일)
이전 댓글 표시
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,:)
댓글 수: 0
답변 (1개)
Walter Roberson
2016년 4월 21일
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
Walter Roberson
2016년 4월 21일
rgb=imread('car3.jpg'); %# Load an RGB image
rPlane = rgb(:,:,1);
gPlane = rgb(:,:,2);
bPlane = rgb(:,:,3);
No loop needed.
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!