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