forked from zhoukq/sift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
388 lines (311 loc) · 8.49 KB
/
utils.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
Miscellaneous utility functions.
Copyright (C) 2006-2010 Rob Hess <[email protected]>
@version 1.1.2-20100521
*/
/*
此文件中实现了图像的基本操作函数:
1、获取某位置的像素点
2、设置某位置的像素点(8位,32位和64位),
3、计算两点之间的距离的平方
4、在图片某一点画一个“X”
5、将两张图片合成为一个,高是二者之和,宽是二者的较大者。img1 在左上角,img2在右下角。
*/
#include "utils.h"
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
/*************************** Function Definitions ****************************/
/*
Prints an error message and aborts the program. The error message is
of the form "Error: ...", where the ... is specified by the \a format
argument
@param format an error message format string (as with \c printf(3)).
*/
void fatal_error(char* format, ...)
{
//va_list ap;
fprintf( stderr, "Error: ");
// va_start( ap, format );
//vfprintf( stderr, format, ap );
// va_end( ap );
fprintf( stderr, "\n" );
abort();
}
/*
Replaces a file's extension, which is assumed to be everything after the
last dot ('.') character.
@param file the name of a file
@param extn a new extension for \a file; should not include a dot (i.e.
\c "jpg", not \c ".jpg") unless the new file extension should contain
two dots.
@return Returns a new string formed as described above. If \a file does
not have an extension, this function simply adds one.
*/
char* replace_extension( const char* file, const char* extn )
{
char* new_file, * lastdot;
new_file = calloc( strlen( file ) + strlen( extn ) + 2, sizeof( char ) );
strcpy( new_file, file );
lastdot = strrchr( new_file, '.' );
if( lastdot )
*(lastdot + 1) = '\0';
else
strcat( new_file, "." );
strcat( new_file, extn );
return new_file;
}
/*
A function that removes the path from a filename. Similar to the Unix
basename command.
@param pathname a (full) path name
@return Returns the basename of \a pathname.
*/
char* basename1( const char* pathname )
{
char* base, * last_slash;
last_slash = strrchr( pathname, '/' );
if( ! last_slash )
{
base = calloc( strlen( pathname ) + 1, sizeof( char ) );
strcpy( base, pathname );
}
else
{
base = calloc( strlen( last_slash++ ), sizeof( char ) );
strcpy( base, last_slash );
}
return base;
}
/*
Displays progress in the console with a spinning pinwheel. Every time this
function is called, the state of the pinwheel is incremented. The pinwheel
has four states that loop indefinitely: '|', '/', '-', '\'.
@param done if 0, this function simply increments the state of the pinwheel;
otherwise it prints "done"
*/
void progress( int done )
{
char state[4] = { '|', '/', '-', '\\' };
static int cur = -1;
if( cur == -1 )
fprintf( stderr, " " );
if( done )
{
fprintf( stderr, "\b\bdone\n");
cur = -1;
}
else
{
cur = ( cur + 1 ) % 4;
fprintf( stdout, "\b\b%c ", state[cur] );
fflush(stderr);
}
}
/*
Erases a specified number of characters from a stream.
@param stream the stream from which to erase characters
@param n the number of characters to erase
*/
void erase_from_stream( FILE* stream, int n )
{
int j;
for( j = 0; j < n; j++ )
fprintf( stream, "\b" );
for( j = 0; j < n; j++ )
fprintf( stream, " " );
for( j = 0; j < n; j++ )
fprintf( stream, "\b" );
}
/*
Doubles the size of an array with error checking
@param array pointer to an array whose size is to be doubled
@param n number of elements allocated for \a array
@param size size in bytes of elements in \a array
@return Returns the new number of elements allocated for \a array. If no
memory is available, returns 0 and frees array.
*/
int array_double( void** array, int n, int size )
{
void* tmp;
tmp = realloc( *array, 2 * n * size );
if( ! tmp )
{
fprintf( stderr, "Warning: unable to allocate memory in array_double(),"
" %s line %d\n", __FILE__, __LINE__ );
if( *array )
free( *array );
*array = NULL;
return 0;
}
*array = tmp;
return n*2;
}
/*
Calculates the squared distance between two points.
@param p1 a point
@param p2 another point
*/
double dist_sq_2D( CvPoint2D64f p1, CvPoint2D64f p2 )
{
double x_diff = p1.x - p2.x;
double y_diff = p1.y - p2.y;
return x_diff * x_diff + y_diff * y_diff;
}
/*
Draws an x on an image.
@param img an image
@param pt the center point of the x
@param r the x's radius
@param w the x's line weight
@param color the color of the x
*/
void draw_x( IplImage* img, CvPoint pt, int r, int w, CvScalar color )
{
cvLine( img, pt, cvPoint( pt.x + r, pt.y + r), color, w, 8, 0 );
cvLine( img, pt, cvPoint( pt.x - r, pt.y + r), color, w, 8, 0 );
cvLine( img, pt, cvPoint( pt.x + r, pt.y - r), color, w, 8, 0 );
cvLine( img, pt, cvPoint( pt.x - r, pt.y - r), color, w, 8, 0 );
}
/*将两张图像合成为一张,垂直排放
参数:img1:位于上方的图像的指针,img2:位于下方的图像的指针
返回值:合成图像
*/
/*
Combines two images by scacking one on top of the other
@param img1 top image
@param img2 bottom image
@return Returns the image resulting from stacking \a img1 on top if \a img2
*/
extern IplImage* stack_imgs( IplImage* img1, IplImage* img2 )
{
//生成合成图像
IplImage* stacked = cvCreateImage( cvSize( MAX(img1->width, img2->width),
img1->height + img2->height ),
IPL_DEPTH_8U, 3 );
cvZero( stacked );//清零
cvSetImageROI( stacked, cvRect( 0, 0, img1->width, img1->height ) );
cvAdd( img1, stacked, stacked, NULL );//叠加第一张图像
cvSetImageROI( stacked, cvRect(0, img1->height, img2->width, img2->height) );
cvAdd( img2, stacked, stacked, NULL );//叠加第二张图像
cvResetImageROI( stacked );
return stacked;
}
/*(自己写的函数)
将两张图像合成为一张,水平排放
参数:img1:位于左边的图像的指针,img2:位于右边的图像的指针
返回值:合成图像
*/
extern IplImage* stack_imgs_horizontal( IplImage* img1, IplImage* img2 )
{
//生成合成图像
IplImage * stacked = cvCreateImage(cvSize(img1->width+img2->width, MAX(img1->height,img2->height)),
IPL_DEPTH_8U, 3);
cvZero(stacked);//清零
cvSetImageROI(stacked, cvRect(0,0,img1->width,img1->height));
cvAdd(img1,stacked,stacked,NULL);//叠加第一张图像
cvSetImageROI(stacked, cvRect(img1->width,0,img2->width,img2->height));
cvAdd(img2,stacked,stacked,NULL);//叠加第二张图像
cvResetImageROI(stacked);
return stacked;
}
/*
Allows user to view an array of images as a video. Keyboard controls
are as follows:
<ul>
<li>Space - start and pause playback</li>
<li>Page Down - skip forward 10 frames</li>
<li>Page Up - jump back 10 frames</li>
<li>Right Arrow - skip forward 1 frame</li>
<li>Left Arrow - jump back 1 frame</li>
<li>Backspace - jump back to beginning</li>
<li>Esc - exit playback</li>
<li>Closing the window also exits playback</li>
</ul>
@param imgs an array of images
@param n number of images in \a imgs
@param win_name name of window in which images are displayed
*/
void vid_view( IplImage** imgs, int n, char* win_name )
{
int k, i = 0, playing = 0;
cvNamedWindow( win_name, 1 );
cvShowImage( win_name, imgs[i] );
while( ! win_closed( win_name ) )
{
/* if already playing, advance frame and check for pause */
if( playing )
{
i = MIN( i + 1, n - 1 );
cvNamedWindow( win_name, 1 );
cvShowImage( win_name, imgs[i] );
k = cvWaitKey( 33 );
if( k == ' ' || i == n - 1 )
playing = 0;
}
else
{
k = cvWaitKey( 0 );
switch( k )
{
/* space */
case ' ':
playing = 1;
break;
/* esc */
case 27:
case 1048603:
cvDestroyWindow( win_name );
break;
/* backspace */
case '\b':
i = 0;
cvNamedWindow( win_name, 1 );
cvShowImage( win_name, imgs[i] );
break;
/* left arrow */
case 65288:
case 1113937:
i = MAX( i - 1, 0 );
cvNamedWindow( win_name, 1 );
cvShowImage( win_name, imgs[i] );
break;
/* right arrow */
case 65363:
case 1113939:
i = MIN( i + 1, n - 1 );
cvNamedWindow( win_name, 1 );
cvShowImage( win_name, imgs[i] );
break;
/* page up */
case 65365:
case 1113941:
i = MAX( i - 10, 0 );
cvNamedWindow( win_name, 1 );
cvShowImage( win_name, imgs[i] );
break;
/* page down */
case 65366:
case 1113942:
i = MIN( i + 10, n - 1 );
cvNamedWindow( win_name, 1 );
cvShowImage( win_name, imgs[i] );
break;
}
}
}
}
/*
Checks if a HighGUI window is still open or not
@param name the name of the window we're checking
@return Returns 1 if the window named \a name has been closed or 0 otherwise
*/
int win_closed( char* win_name )
{
if( ! cvGetWindowHandle(win_name) )
return 1;
return 0;
}