Avoiding FOR loops to select a region in matrix and minimise computation time
古いコメントを表示
Hello,
I am trying to write a cross-correlation script where I need to slide my template across all rows and columns in the subject. Currently I am using nested FOR loops to select rows and columns every time I do a search. However, the computational time is quite high and I wanted to minimise it by avoiding FOR loops by any mean possible. For obvious reasons, I cannot publish my code yet. However, I can provide a pseudo code:
T = imread('template.png')
S = imread('subject.png')
[rowT, columnT] = size(T);
[rowS,columnS] = size(S);
coeff = zeros(size(S));
for ii=0:(rowS-rowT)
for jj = 0:(columnS - columnT)
ss = S(ii+1:rowT+ii,jj+1:columnT+jj);
.
.
.
coeff(ii+1,jj+1) = (lambdaRo^2)*(lambdaGamma^2);
end
end
Is there any way to avoid using for loops to select region of my subject image and speed the calculation time up? From my limited experience in Mathematics and MATLAB (compared to what the community users' experience is) I don't see how it is possible. Please help me out!!
Kind Regards,
6 件のコメント
Björn
2012 年 10 月 18 日
Yes there is, you have to vectorize your script. I can show you how, but can you post the whole script? (at least including the complete for-loops)
Mohammed Manna
2012 年 10 月 18 日
Björn
2012 年 10 月 18 日
Ok, I do run into one little problem: I can't really find a way to create ss without a for-loop. The other calculations I can take outside the loop. But in order for making it faster that way, you must be sure that you will have enough RAM left. How much percent of your RAM is used while executing this script? If there is in total less then 50MB RAM left during the calculations, then that is the problem of the duration of your simulation. In that case you can easily solve it by converting your matrices to single-valued or maybe even 16- or 32-bit integers.
Mohammed Manna
2012 年 10 月 18 日
Image Analyst
2012 年 10 月 18 日
Again, why not use xcorr2()? Though, I admit I didn't study your code. Do you have some kind of unusual custom correlation that doesn't do the same thing as the built-in function?
Mohammed Manna
2012 年 11 月 9 日
回答 (1 件)
Image Analyst
2012 年 10 月 18 日
0 投票
Why not use xcorr2()?
カテゴリ
ヘルプ センター および File Exchange で Signal Processing Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!