How to solve this error (index must be a positive integer or logical)?

1 回表示 (過去 30 日間)
Mei Synn Tan
Mei Synn Tan 2017 年 4 月 13 日
編集済み: Mei Synn Tan 2017 年 4 月 13 日
function s = subim(f, m, n, rx, cy)
%SUBIM Extracts a subimage, s, from a given image, f.
%The subimage is of size m-by-n, and the coordinates of its top, left corner are (rx, cy).
s = zeros(m, n);
rowhigh = rx + m - 1;
colhigh = cy + n - 1;
xcount = 0;
for r = rx:rowhigh
xcount = xcount + 1;
ycount = 0;
for c = cy:colhigh
ycount = ycount + 1;
s(xcount, ycount) = f(r, c);
end
end
I am try to run the code, but this got error. Attempted to access f(0,0); index must be a positive integer or logical.
f = imread('13100.jpg');
[m,n]=size(f);
s = zeros(m, n);
rx = 0;
cy = 0;
s = zeros(m, n);
rowhigh = rx + m - 1;
colhigh = cy + n - 1;
xcount = 0;
for r = rx:rowhigh
xcount = xcount + 1;
ycount = 0;
for c = cy:colhigh
ycount = ycount + 1;
s(xcount, ycount) = f(r, c);
end
end
%
  1 件のコメント
Stephen23
Stephen23 2017 年 4 月 13 日
@Mei Synn Tan: rather than doing this in a loop you should use indexing with vectors. Also note that images are often 3D, which your code does not take into account.

サインインしてコメントする。

採用された回答

KSSV
KSSV 2017 年 4 月 13 日
編集済み: KSSV 2017 年 4 月 13 日
This:
rx = 0;
cy = 0;
Should be
rx = 1;
cy = 1;
You are taking indices 0, note that, matlab will not take -ve or zero indices. Indices should be +ve integers.
  1 件のコメント
Mei Synn Tan
Mei Synn Tan 2017 年 4 月 13 日
編集済み: Mei Synn Tan 2017 年 4 月 13 日
Many thanks in advance!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSatellite Mission Analysis についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by