image processing - Performing a phase correlation with fft in R -
i trying implement 2d phase correlation algorithm in r using recipe wikipedia (http://en.wikipedia.org/wiki/phase_correlation) in order track movement between 2 images. these images (frames) captured camera shaking in wind , ultimate goal remove shake in these , subsequent frames. 2 example images , r code below:

## need tiff library library(tiff) ## read in tiff files f1=as.matrix(readtiff('f1.tiff',native=true)) f2=as.matrix(readtiff('f2.tiff',native=true)) ## take fft of first frame f1 <- fft(f1) ## take conjugate fft of second frame f2.c <- conj(fft(f2)) ## calculate cross power spectrum according wiki article r <- (f1*f2.c)/abs(f1*f2.c) ## take inverse fft of r r <- fft(r,inv=true)/length(r) ## because 0 valued imaginary numbers not needed r <- re(r) ## show normalized cross-correlation image(r) ## find max in cross correlation matrix, or phase shift - ## between 2 images shift <- which(r==max(r),arr.ind=true) the vector shift, understanding, should contain information on transitive shift (dx , dy) best corrects these 2 images. shift variable gives dx=1 , dy=1, assume indicates no shift in either x or y direction. occurs subsequent frames there visible shifts or several pixels in both x , y direction.
do of y'all see error in code/formulas? or need try fancier filtering images first before phase correlation?
cheers gals , guys!
the code looks correct know phase correlation. if understand want correctly, trying use phase correlation determine offset between 2 images given homographies nothing more horizontal , vertical offsets. fact you're getting shift @ origin due images lacking sufficient high frequency information in order determine shift.
try these 2 images instead (these wikipedia article referenced, extracted them out , saved them individual images):

when run these 2 images r code, phase correlation map. bear in mind images saved .png, had change library library(png) , used readpng instead of readtiff. keep in mind when try , run code above example images:

also, location of maximum peak occurred was:
> shift row col [1,] 132 153 this tells image shifted on 132 rows , 153 columns. take note with respect centre of image. if want determine actual offset, you'll need subtract half rows vertical coordinate , half columns horizontal coordinate.
therefore, code works totally fine... it's images lack sufficient high frequency information phase correlation work. correlation trying in case we're trying find "similar" variations between each image. if there lot of variations between each image , similar, phase correlation work well. however, if don't have variation, phase correlation won't work.
why case? basis behind phase correlation assume image corrupted gaussian white noise, , if correlate white noise (from 1 image another) give nice high peak @ offset or shift , 0 everywhere. because of fact images lack lot of high frequency information , fact images clean, phase correlation won't work. therefore, people suggest pre-whiten image image contains white noise can nice peak @ offset should we're talking about.
however, make sure eliminate false maximums, idea smoothen cross-correlation matrix in frequency domain (r in r code) there high probability there 1 true maximum. using gaussian filter in frequency / fft domain should work fine.
in case, don't see variation in images , take away gotta make sure image has lot of high frequency information work!
Comments
Post a Comment