index must be a positive integer or logical.
1 回表示 (過去 30 日間)
古いコメントを表示
how to correct this error "Attempted to access thinned(0,2); index must be a positive integer or logical."
img = imread('5_1_4.jpg'); figure, imshow(img)
binary_image= im2bw(img,graythresh(img));
thinned=im2double(bwmorph(binary_image,'thin',Inf));
for x=1:size(thinned, 2)
for y=1:size(thinned,1)
if (thinned(x, y) == 1)
CN=(abs(thinned(x+1,y)-thinned(x+1,y-1))+ abs(thinned(x+1,y-1)-thinned(x,y-1))+ abs(thinned(x,y-1)-thinned(x-1,y-1))+ abs(thinned(x-1,y-1)-thinned(x-1,y))+ abs(thinned(x-1,y)-thinned(x-1,y+1))+ abs(thinned(x-1,y+1)-thinned(x,y+1))+ abs(thinned(x,y+1)-thinned(x+1,y+1))+ abs(thinned(x+1,y+1)-thinned(x+1,y)));
CN=CN/2;
end
end
end
end
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/168464/image.jpeg)
0 件のコメント
採用された回答
Jan
2018 年 1 月 4 日
編集済み: Jan
2018 年 1 月 4 日
You access the indices x-1, y-1 and x+1, y+1. Then replace:
for x=1:size(thinned, 2)
for y=1:size(thinned,1)
by:
for x = 2:size(thinned, 2) - 1 % [EDITED: x is 2nd dim, y is 1st dim]
for y = 2:size(thinned, 1) - 1
Currently you overwrite CN in each iteration. Is this wanted?
I think, that a proper indentation and sorting of the indices makes it much easier to debug:
% [EDITED, x and y swapped]
CN = (abs(thinned(y+1, x-1) - thinned(y, x-1)) + ...
abs(thinned(y+1, x) - thinned(y+1, x-1)) + ...
abs(thinned(y+1, x+1) - thinned(y+1, x))) + ...
abs(thinned(y, x-1) - thinned(y-1, x-1)) + ...
abs(thinned(y, x+1) - thinned(y+1, x+1)) + ...
abs(thinned(y-1, x-1) - thinned(y-1, x)) + ...
abs(thinned(y-1, x) - thinned(y-1, x+1)) + ...
abs(thinned(y-1, x+1) - thinned(y, x+1));
CN = CN/2;
4 件のコメント
Walter Roberson
2018 年 1 月 4 日
編集済み: Walter Roberson
2018 年 1 月 4 日
for row = 2:size(thinned, 1) - 1
for col = 2:size(thinned, 2) - 1
cn = (abs(thinned(row+1, col-1) - thinned(row, col-1)) + ...
abs(thinned(row+1, col) - thinned(row+1, col-1)) + ...
abs(thinned(row+1, col+1) - thinned(row+1, col))) + ...
abs(thinned(row, col-1) - thinned(row-1, col-1)) + ...
abs(thinned(row, col+1) - thinned(row+1, col+1)) + ...
abs(thinned(row-1, col-1) - thinned(row-1, col)) + ...
abs(thinned(row-1, col) - thinned(row-1, col+1)) + ...
abs(thinned(row-1, col+1) - thinned(row, col+1));
CN(row,col) = cn/2;
end
end
Jan
2018 年 1 月 5 日
編集済み: Jan
2018 年 1 月 5 日
@kmla: I asked, if overwriting CN is intended. Maybe you posted only a part of the code.
"but they give this error also" - we cannot know what "they" and "this" exactly means. Please be as clear as possible. Post a copy of the message.
Did you consider Image Analyst's comment already? I had overseen that the 1st and 2nd index have been confused. Now I've fixed the code I've posted.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Operators and Elementary Operations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!