forked from dkogan/horizonator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfit.c
246 lines (185 loc) · 7.5 KB
/
fit.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <opencv2/core/core_c.h>
#include <opencv2/imgproc/imgproc_c.h>
#include <opencv2/highgui/highgui_c.h>
#define IRON_ANGLE 72.2 /* angle of view for my iron mt photo */
#define PHOTO_PRESMOOTH 9
static void writeNormalized( const char* filename, const CvMat* img )
{
double minval, maxval;
CvPoint minpoint, maxpoint;
cvMinMaxLoc( img, &minval, &maxval, &minpoint, &maxpoint, NULL );
CvMat* scaled_img = cvCreateMat( img->rows, img->cols, CV_8UC1);
cvConvertScale( img, scaled_img,
255.0/(maxval - minval),
-minval*255.0/(maxval - minval) );
cvSaveImage( filename, scaled_img, 0 );
}
typedef enum{ PANO, PHOTO } image_type_t;
static CvMat* extractEdges( IplImage* img, image_type_t source )
{
CvMat* temp_16sc;
CvMat* temp_8uc;
CvMat* edges;
edges = cvCreateMat( img->height, img->width, CV_8UC1 );
temp_16sc = cvCreateMat( img->height, img->width, CV_16SC1 );
temp_8uc = cvCreateMat( img->height, img->width, CV_8UC1 );
cvZero(edges);
void accumulateEdgesFromChannel( int channel )
{
if( channel > 0 )
cvSetImageCOI( img, channel );
cvCopy(img, temp_8uc, NULL); // needed only becaues cvLaplace() doesn't support COI
if( source == PHOTO )
cvSmooth(temp_8uc, temp_8uc, CV_GAUSSIAN, PHOTO_PRESMOOTH, PHOTO_PRESMOOTH, 0.0, 0.0);
cvLaplace(temp_8uc, temp_16sc, 3);
cvAbs( temp_16sc, temp_16sc );
cvAdd( edges, temp_16sc, edges, NULL);
}
if( img->nChannels == 1 )
accumulateEdgesFromChannel( -1 );
else if( source != PHOTO )
accumulateEdgesFromChannel( 1 );
else
for(int i = 0; i < 3; i++)
accumulateEdgesFromChannel( i+1 );
cvReleaseMat(&temp_16sc);
cvReleaseMat(&temp_8uc);
return edges;
}
static CvPoint alignImages( const CvMat* img, const CvMat* pano )
{
CvSize img_size = cvGetSize(img);
CvSize pano_size = cvGetSize(pano);
// to handle wraparound correctly, I set the output size to be at least as
// large as double the smallest dimensions
#define max(a,b) ( (a) > (b) ? (a) : (b) )
#define min(a,b) ( (a) < (b) ? (a) : (b) )
CvSize dft_size =
cvSize( max( 2*min(img_size.width, pano_size.width), max(img_size.width, pano_size.width) ),
max( 2*min(img_size.height, pano_size.height), max(img_size.height, pano_size.height) ) );
#undef min
#undef max
#define DoDft(mat) \
CvMat* mat ## _float = cvCreateMat( dft_size.height, dft_size.width, CV_32FC1); \
assert( mat ## _float ); \
\
CvMat* mat ## _dft = cvCreateMat( dft_size.height, dft_size.width, CV_32FC1); \
assert( mat ## _dft ); \
\
cvSet( mat ## _float, \
cvAvg(mat, NULL), \
NULL ); \
\
CvMat mat ## _float_origsize; \
cvGetSubRect( mat ## _float, &mat ## _float_origsize, \
cvRect(0,0, mat ## _size.width, mat ## _size.height ) ); \
cvConvert( mat, &mat ## _float_origsize ); \
cvDFT(mat ## _float, mat ## _dft, CV_DXT_FORWARD, 0)
DoDft(img);
DoDft(pano);
CvMat* correlation = cvCreateMat( dft_size.height, dft_size.width, CV_32FC1);
CvMat* correlation_freqdomain = cvCreateMat( dft_size.height, dft_size.width, CV_32FC1);
assert(correlation_freqdomain);
cvMulSpectrums(pano_dft, img_dft, correlation_freqdomain, CV_DXT_MUL_CONJ);
cvDFT(correlation_freqdomain, correlation, CV_DXT_INVERSE, 0);
double minval, maxval;
CvPoint minpoint, maxpoint;
cvMinMaxLoc( correlation, &minval, &maxval, &minpoint, &maxpoint, NULL );
printf("right answer: (644,86)\n");
cvSaveImage( "iedges.png", img_float ,0 );
cvSaveImage( "pedges.png", pano_float,0 );
CvMat* correlation_img = cvCreateMat( dft_size.height, dft_size.width, CV_8UC1);
cvConvertScale( correlation, correlation_img,
255.0/(maxval - minval),
-minval*255.0/(maxval - minval) );
cvSaveImage( "corr.png", correlation_img ,0 );
cvReleaseMat( &correlation_img );
cvReleaseMat( &correlation );
cvReleaseMat( &correlation_freqdomain );
cvReleaseMat( &img_float );
cvReleaseMat( &pano_float );
cvReleaseMat( &img_dft );
cvReleaseMat( &pano_dft );
return maxpoint;
}
int main(int argc, char* argv[])
{
if( argc != 3 )
{
fprintf(stderr, "Usage: %s panorender.png photo.jpg\n", argv[0]);
return 1;
}
IplImage* pano = cvLoadImage( argv[1], CV_LOAD_IMAGE_COLOR); assert(pano);
IplImage* img = cvLoadImage( argv[2], CV_LOAD_IMAGE_COLOR); assert(img);
CvMat* pano_edges;
CvMat* img_edges;
{
pano_edges = extractEdges(pano, PANO);
cvThreshold( pano_edges, pano_edges, 200.0, 0, CV_THRESH_TOZERO );
// the non-edge areas of the panorama should be dont-care areas. I implement
// this by
// x -> dilate ? x : mean;
// another way to state the same thing:
// !dilate -> mask
// cvSet(mean)
#define DILATE_R 9
#define EDGE_MINVAL 180
IplConvKernel* kernel = cvCreateStructuringElementEx( 2*DILATE_R + 1, 2*DILATE_R + 1,
DILATE_R, DILATE_R,
CV_SHAPE_ELLIPSE, NULL);
CvMat* dilated = cvCreateMat( pano->height, pano->width, CV_8UC1 );
cvDilate(pano_edges, dilated, kernel, 1);
CvScalar avg = cvAvg(pano_edges, dilated);
cvCmpS(dilated, EDGE_MINVAL, dilated, CV_CMP_LT);
cvSet( pano_edges, avg, dilated );
cvReleaseMat(&dilated);
cvReleaseStructuringElement(&kernel);
}
{
img_edges = extractEdges(img, PHOTO);
cvSmooth(img_edges, img_edges, CV_GAUSSIAN, 13, 13, 0.0, 0.0);
}
CvPoint offset = alignImages( img_edges, pano_edges );
printf("offset: x,y: %d %d\n", offset.x, offset.y );
cvReleaseMat ( &pano_edges );
cvReleaseMat ( &img_edges );
cvReleaseImage( &pano );
cvReleaseImage( &img );
return 0;
}
#if 0
static void undistort(void)
{
IplImage* remapped = cvCreateImage(size, 8, 3);
assert(remapped);
CvMat* mapx = cvCreateMat(size.height, size.width, CV_32FC1);
assert(mapx); assert(mapx->step == size.width*sizeof(float));
CvMat* mapy = cvCreateMat(size.height, size.width, CV_32FC1);
assert(mapy); assert(mapy->step == size.width*sizeof(float));
for(int j=0; j<size.height; j++ )
{
for( int i=0; i<size.width; i++ )
{
mapy->data.fl[i + mapy->cols*j] = j;
// I'm mapping the image from a perspective projection to a mercator one.
// Here 'i' represents an angle from a perspective projection.
float w = (float)(mapx->cols - 1);
float pixel_mid = w / 2.0f;
float i_mid = (float)i - pixel_mid;
float f = w / 2.0f / tanf(IRON_ANGLE * M_PI / 180.0f / 2.0f);
float angle = 180.0f / M_PI * atanf( i_mid / f );
mapx->data.fl[i + mapx->cols*j] = (angle / IRON_ANGLE + 0.5f) * w;
}
}
cvRemap(src, remapped,
mapx, mapy,
CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS,
cvScalarAll(0) );
cvReleaseImage( &remapped );
cvReleaseMat( &mapx );
cvReleaseMat( &mapy );
}
#endif