normalized cross correlation: Improving performance (?) [message #35272] |
Thu, 05 June 2003 07:37 |
pogen2
Messages: 2 Registered: June 2003
|
Junior Member |
|
|
How can I perform the following normalized cross correlation
between an image and a template (not of the same size) without
the use of two FOR loops?
> ;f is the transformed image and w is the template (reference image)
>
> t = systime(1)
>
> rsz = size(w) ;dimension KxJ
> tsz = size(f) ;dimension MxN
> a = (rsz[1]-1)/2 ;a=(K-1)/2
> b = (rsz[2]-1)/2 ;b=(J-1)/2
>
> IF rsz[0] NE tsz[0] THEN message, 'ERROR:template and image must have the same dimension'
> IF rsz[4] GT tsz[4] THEN message, 'ERROR:template bigger than image'
>
> crosscorr = FLTARR(tsz[1]-rsz[1]+1,tsz[2]-rsz[2]+1) ;dimension M-K+1 x N-J+1
>
> FOR x=a, tsz[1]-(a+1) DO BEGIN
> FOR y=b, tsz[2]-(b+1) DO BEGIN
>
> f2 = f[x-a:x+a,y-b:y+b]
>
> wavg = total(w)/rsz[4] ;reference image average
> favg = total(f2) /tsz[4] ;transformed image average
>
> ; The cross correlation
> ;crosscorr = ((w-wavg))*((f-favg))
> ;crosscorr = total(crosscorr)/rsz[4]
>
> ; The normalized cross correlation (in range [-1,+1])
> ww = (w-wavg)
> ff = (f2-favg)
> crosscorr[x-a,y-b] = total(ww*ff)/sqrt(total(ww^2)*total(ff^2))
>
> ENDFOR
> ENDFOR
I tried something with arrays such as :
> subs_arr = ['''something working'''] ;array of subscripts
> farr = FLTARR(rsz[4],tsz[1]-rsz[1]+1,tsz[2]-rsz[2]+1) ;dimension K*J x M-K+1 x N-J+1
>
> ;the next line is the tricky part that isn't working. If something
can be done so that
> ;farr becomes an array of M-K+1 x N-J+1 (which is the size of the
crosscorr coefficients
> ;array) vectors each one containing the pixels of the masked area of
f, it would be
> ;wonderful!
>
> farr[*,subs_arr[?],subs_arr[?]] = reform(f[?:?+2*a,?:?+2*b],1)
>
> ;the following should be working after that...
>
> wavg = total(w)/rsz[4] ;reference image average
> favg = total(farr,1) /rsz[4] ;transformed image average
>
> ; The cross correlation
> ;crosscorr = ((w-wavg))*((f-favg))
> ;crosscorr = total(crosscorr)/rsz[4]
>
> ; The normalized cross correlation (in range [-1,+1])
> ww = (w-wavg)
> ff = (farr-favg)
> crosscorr = total(reform(ww,1)*ff,1)/sqrt(total(ww^2)*total(ff^2,1))
Thank you for any help,
Pier Genest
|
|
|