Avoiding FOR loops to select a region in matrix and minimise computation time

조회 수: 1 (최근 30일)
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
Image Analyst
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
Mohammed Manna 2012년 11월 9일
Actually better!! I have got my own correlation implementation that is computationally a) cheap b) faster. I use cross-sectional histogram that is O(2n) rather than O(n^2). compared to normxcorr2 or xcorr2, it is about %20 faster...

댓글을 달려면 로그인하십시오.

답변 (1개)

Image Analyst
Image Analyst 2012년 10월 18일
Why not use xcorr2()?

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by