How can i do this?

1 回表示 (過去 30 日間)
judy  frost
judy frost 2012 年 10 月 17 日
Here is my code:
[m,n] = size(I);
for c = 1:n
for r = 1:m
x = round(s*r);
y = round(s*c);
if x > 0 && x < row && y > 0 && y < col % inside
S(r,c,:) = I(x,y,:);
end
end
end
Implementation works fine but the timing issues is the problem.
Thanks in advance :)
  1 件のコメント
Randy Souza
Randy Souza 2012 年 10 月 22 日
judy, did you flag your question as inappropriate for a reason? If not, can you please delete the flag? Thanks!

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

採用された回答

Matt Fig
Matt Fig 2012 年 10 月 18 日
編集済み: Matt Fig 2012 年 10 月 18 日
One thing I notice in your code. It looks, by the way you index inside the FOR loop, like your image is 3D. If so, the variable col will not contain the number of columns of the matrix. It will contain the product of the number of columns and all higher dimensions. If your image is 2D, then why are you indexing like it is 3D??
I will assume your image is 3D, and you really want the variable col to store the number of columns. This vectorization is much faster than the FOR loop, depending on the sizes involved and the scale factor. Note that I use the variable IM instead of calling it image because naming a variable the same name as a MATLAB function masks that function.
C = 1:col;
R = 1:row;
X = round(R*s);
Y = round(C*s);
idx = X>0 & X<row;
X = X(idx);
R = R(idx);
idx = Y>0 & Y<col;
Y = Y(idx);
C = C(idx);
scaled_IM2 = zeros(size(IM),class(IM));
scaled_IM2(R,C,:) = IM(X,Y,:);

その他の回答 (1 件)

Sean de Wolski
Sean de Wolski 2012 年 10 月 17 日
You'll get an enormous speedup just by preallocating scaled_image so that it does not change size on every iteration.
scaled_image = zeros(size(your_image));
for c
for r
etc;
Also note, don't call your variable image since this is a useful builtin function.

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by