?? Index exceeds matrix dimensions.
2 ビュー (過去 30 日間)
古いコメントを表示
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
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
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;
0 件のコメント
参考
カテゴリ
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!