필터 지우기
필터 지우기

?? Index exceeds matrix dimensions.

조회 수: 2 (최근 30일)
Saad Abdullah
Saad Abdullah 2011년 12월 16일
Please can any body tell me whats wrong with my code? Its giving error on the 'If' statement. '??? Index exceeds matrix dimensions.'
function color()
img = imread('peppers.png');
r = img(:,:,1);
g = img(:,:,2);
b = img(:,:,3);
[m n] = size(img);
for i = 1:m
for j = 1:n
if ((r(i,j) - g(i,j)) >= 200) %&& mod(r(i,j) - b(i,j)) >= 200)
im = r(i,j);
else
im = 0;
end
end
end
imshow(im);
  댓글 수: 1
Fangjun Jiang
Fangjun Jiang 2011년 12월 17일
Don't use color as the function name. Strange enough, help color and doc color give me different type of function help.

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

답변 (1개)

Jan
Jan 2011년 12월 16일
img = rand(2,3,4);
[m n] = size(img);
Now m is 2 as expected, but n is 12, the product of all trailing dimensions. Either use:
[m, n, dummy] = size(img);
or
[m, n] = size(r);
The program overwrite im in each iteration. I assume you want to set im(i,j) instead.
A more efficient approach without a loop:
r = img(:, :, 1);
g = img(:, :, 2);
im = r;
im(r - g > 200) = 0;

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by