Advertisement
Guest User

Untitled

a guest
Jul 29th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "cv.h"
  4. #include "highgui.h"
  5. #include "fftw3.h"
  6. void phase_correlation( IplImage *ref, IplImage *tpl, IplImage *poc );
  7. int main( int argc, char** argv )
  8. {
  9. IplImage *tpl = 0;
  10. IplImage *ref = 0;
  11. IplImage *poc = 0;
  12. char String[255];
  13. if( argc < 3 ) {
  14. fprintf( stderr, "Usage: phase_correlation <url1> <url2>\n" );
  15. return 1;
  16. }
  17. sprintf(String, "wget %s -O image1.jpg", argv[1]);
  18. system(String);
  19. sprintf(String, "wget %s -O image2.jpg", argv[2]);
  20. system(String);
  21. /* load reference image */
  22. ref = cvLoadImage( "image1.jpg", CV_LOAD_IMAGE_GRAYSCALE );
  23. /* always check */
  24. if( ref == 0 ) {
  25. fprintf( stderr, "Cannot load %s!\n", argv[1] );
  26. return 1;
  27. }
  28. /* load template image */
  29. tpl = cvLoadImage( "image2.jpg", CV_LOAD_IMAGE_GRAYSCALE );
  30. /* always check */
  31. if( tpl == 0 ) {
  32. fprintf( stderr, "Cannot load %s!\n", argv[2] );
  33. return 1;
  34. }
  35. /* both images' size should be equal */
  36. if( ( tpl->width != ref->width ) || ( tpl->height != ref->height ) ) {
  37. fprintf( stderr, "Both images must have equal width and height!\n" );
  38. return 1;
  39. }
  40. /* create a new image, to store phase correlation result */
  41. poc = cvCreateImage( cvSize( tpl->width, tpl->height ), IPL_DEPTH_64F, 1 );
  42. /* get phase correlation of input images */
  43. phase_correlation( ref, tpl, poc );
  44. /* find the maximum value and its location */
  45. CvPoint minloc, maxloc;
  46. double minval, maxval;
  47. cvMinMaxLoc( poc, &minval, &maxval, &minloc, &maxloc, 0 );
  48. /* print it */
  49. fprintf( stdout, "Maxval at (%d, %d) = %2.4f\n", maxloc.x, maxloc.y, maxval );
  50. fprintf(stdout,"percentage comparision= %2.4f \n",maxval*100);
  51. /* display images and free memory */
  52. cvNamedWindow( "tpl", CV_WINDOW_AUTOSIZE );
  53. cvNamedWindow( "ref", CV_WINDOW_AUTOSIZE );
  54. cvShowImage( "tpl", tpl );
  55. cvShowImage( "ref", ref );
  56. cvWaitKey( 0 );
  57. cvDestroyWindow( "tpl" );
  58. cvDestroyWindow( "ref" );
  59. cvReleaseImage( &tpl );
  60. cvReleaseImage( &ref );
  61. cvReleaseImage( &poc );
  62. return 0;
  63. }
  64. /*
  65. * get phase correlation from two images and save result to the third image
  66. */
  67. void phase_correlation( IplImage *ref, IplImage *tpl, IplImage *poc )
  68. {
  69. int i, j, k;
  70. double tmp;
  71. /* get image properties */
  72. int width = ref->width;
  73. int height = ref->height;
  74. int step = ref->widthStep;
  75. int fft_size = width * height;
  76. /* setup pointers to images */
  77. uchar *ref_data = ( uchar* ) ref->imageData;
  78. uchar *tpl_data = ( uchar* ) tpl->imageData;
  79. double *poc_data = ( double* )poc->imageData;
  80. /* allocate FFTW input and output arrays */
  81. fftw_complex *img1 = ( fftw_complex* )fftw_malloc( sizeof( fftw_complex ) * width * height );
  82. fftw_complex *img2 = ( fftw_complex* )fftw_malloc( sizeof( fftw_complex ) * width * height );
  83. fftw_complex *res = ( fftw_complex* )fftw_malloc( sizeof( fftw_complex ) * width * height );
  84. /* setup FFTW plans */
  85. fftw_plan fft_img1 = fftw_plan_dft_1d( width * height, img1, img1, FFTW_FORWARD, FFTW_ESTIMATE );
  86. fftw_plan fft_img2 = fftw_plan_dft_1d( width * height, img2, img2, FFTW_FORWARD, FFTW_ESTIMATE );
  87. fftw_plan ifft_res = fftw_plan_dft_1d( width * height, res, res, FFTW_BACKWARD, FFTW_ESTIMATE );
  88. /* load images' data to FFTW input */
  89. for( i = 0, k = 0 ; i < height ; i++ ) {
  90. for( j = 0 ; j < width ; j++, k++ ) {
  91. img1[k][0] = ( double )ref_data[i * step + j];
  92. img1[k][1] = 0.0;
  93. img2[k][0] = ( double )tpl_data[i * step + j];
  94. img2[k][1] = 0.0;
  95. }
  96. }
  97. /* obtain the FFT of img1 */
  98. fftw_execute( fft_img1 );
  99. /* obtain the FFT of img2 */
  100. fftw_execute( fft_img2 );
  101. /* obtain the cross power spectrum */
  102. for( i = 0; i < fft_size ; i++ ) {
  103. res[i][0] = ( img2[i][0] * img1[i][0] ) - ( img2[i][1] * ( -img1[i][1] ) );
  104. res[i][1] = ( img2[i][0] * ( -img1[i][1] ) ) + ( img2[i][1] * img1[i][0] );
  105. tmp = sqrt( pow( res[i][0], 2.0 ) + pow( res[i][1], 2.0 ) );
  106. res[i][0] /= tmp;
  107. res[i][1] /= tmp;
  108. }
  109. /* obtain the phase correlation array */
  110. fftw_execute(ifft_res);
  111. /* normalize and copy to result image */
  112. for( i = 0 ; i < fft_size ; i++ ) {
  113. poc_data[i] = res[i][0] / ( double )fft_size;
  114. }
  115. /* deallocate FFTW arrays and plans */
  116. fftw_destroy_plan( fft_img1 );
  117. fftw_destroy_plan( fft_img2 );
  118. fftw_destroy_plan( ifft_res );
  119. fftw_free( img1 );
  120. fftw_free( img2 );
  121. fftw_free( res );
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement