-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.cpp
302 lines (232 loc) · 8.35 KB
/
main.cpp
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
/***********************************************************
* --- OpenSURF --- *
* This library is distributed under the GNU GPL. Please *
* use the contact form at http://www.chrisevansdev.com *
* for more information. *
* *
* C. Evans, Research Into Robust Visual Features, *
* MSc University of Bristol, 2008. *
* *
************************************************************/
#include "surflib.h"
#include "kmeans.h"
#include <ctime>
#include <iostream>
//-------------------------------------------------------
// In order to you use OpenSURF, the following illustrates
// some of the simple tasks you can do. It takes only 1
// function call to extract described SURF features!
// Define PROCEDURE as:
// - 1 and supply image path to run on static image
// - 2 to capture from a webcam
// - 3 to match find an object in an image (work in progress)
// - 4 to display moving features (work in progress)
// - 5 to show matches between static images
#define PROCEDURE 2
//-------------------------------------------------------
int mainImage(void)
{
// Declare Ipoints and other stuff
IpVec ipts;
IplImage *img=cvLoadImage("imgs/sf.jpg");
// Detect and describe interest points in the image
clock_t start = clock();
surfDetDes(img, ipts, false, 5, 4, 2, 0.0004f);
clock_t end = clock();
std::cout<< "OpenSURF found: " << ipts.size() << " interest points" << std::endl;
std::cout<< "OpenSURF took: " << float(end - start) / CLOCKS_PER_SEC << " seconds" << std::endl;
// Draw the detected points
drawIpoints(img, ipts);
// Display the result
showImage(img);
return 0;
}
//-------------------------------------------------------
int mainVideo(void)
{
// Initialise capture device
CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );
if(!capture) error("No Capture");
// Initialise video writer
//cv::VideoWriter vw("c:\\out.avi", CV_FOURCC('D','I','V','X'),10,cvSize(320,240),1);
//vw << img;
// Create a window
cvNamedWindow("OpenSURF", CV_WINDOW_AUTOSIZE );
// Declare Ipoints and other stuff
IpVec ipts;
IplImage *img=NULL;
// Main capture loop
while( 1 )
{
// Grab frame from the capture source
img = cvQueryFrame(capture);
// Extract surf points
surfDetDes(img, ipts, false, 4, 4, 2, 0.004f);
// Draw the detected points
drawIpoints(img, ipts);
// Draw the FPS figure
drawFPS(img);
// Display the result
cvShowImage("OpenSURF", img);
// If ESC key pressed exit loop
if( (cvWaitKey(10) & 255) == 27 ) break;
}
cvReleaseCapture( &capture );
cvDestroyWindow( "OpenSURF" );
return 0;
}
//-------------------------------------------------------
int mainMatch(void)
{
// Initialise capture device
CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );
if(!capture) error("No Capture");
// Declare Ipoints and other stuff
IpPairVec matches;
IpVec ipts, ref_ipts;
// This is the reference object we wish to find in video frame
// Replace the line below with IplImage *img = cvLoadImage("imgs/object.jpg");
// where object.jpg is the planar object to be located in the video
IplImage *img = cvLoadImage("imgs/object.jpg");
if (img == NULL) error("Need to load reference image in order to run matching procedure");
CvPoint src_corners[4] = {{0,0}, {img->width,0}, {img->width, img->height}, {0, img->height}};
CvPoint dst_corners[4];
// Extract reference object Ipoints
surfDetDes(img, ref_ipts, false, 3, 4, 3, 0.004f);
drawIpoints(img, ref_ipts);
showImage(img);
// Create a window
cvNamedWindow("OpenSURF", CV_WINDOW_AUTOSIZE );
// Main capture loop
while( true )
{
// Grab frame from the capture source
img = cvQueryFrame(capture);
// Detect and describe interest points in the frame
surfDetDes(img, ipts, false, 3, 4, 3, 0.004f);
// Fill match vector
getMatches(ipts,ref_ipts,matches);
// This call finds where the object corners should be in the frame
if (translateCorners(matches, src_corners, dst_corners))
{
// Draw box around object
for(int i = 0; i < 4; i++ )
{
CvPoint r1 = dst_corners[i%4];
CvPoint r2 = dst_corners[(i+1)%4];
cvLine( img, cvPoint(r1.x, r1.y),
cvPoint(r2.x, r2.y), cvScalar(255,255,255), 3 );
}
for (unsigned int i = 0; i < matches.size(); ++i)
drawIpoint(img, matches[i].first);
}
// Draw the FPS figure
drawFPS(img);
// Display the result
cvShowImage("OpenSURF", img);
// If ESC key pressed exit loop
if( (cvWaitKey(10) & 255) == 27 ) break;
}
// Release the capture device
cvReleaseCapture( &capture );
cvDestroyWindow( "OpenSURF" );
return 0;
}
//-------------------------------------------------------
int mainMotionPoints(void)
{
// Initialise capture device
CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );
if(!capture) error("No Capture");
// Create a window
cvNamedWindow("OpenSURF", CV_WINDOW_AUTOSIZE );
// Declare Ipoints and other stuff
IpVec ipts, old_ipts, motion;
IpPairVec matches;
IplImage *img;
// Main capture loop
while( 1 )
{
// Grab frame from the capture source
img = cvQueryFrame(capture);
// Detect and describe interest points in the image
old_ipts = ipts;
surfDetDes(img, ipts, true, 3, 4, 2, 0.0004f);
// Fill match vector
getMatches(ipts,old_ipts,matches);
for (unsigned int i = 0; i < matches.size(); ++i)
{
const float & dx = matches[i].first.dx;
const float & dy = matches[i].first.dy;
float speed = sqrt(dx*dx+dy*dy);
if (speed > 5 && speed < 30)
drawIpoint(img, matches[i].first, 3);
}
// Display the result
cvShowImage("OpenSURF", img);
// If ESC key pressed exit loop
if( (cvWaitKey(10) & 255) == 27 ) break;
}
// Release the capture device
cvReleaseCapture( &capture );
cvDestroyWindow( "OpenSURF" );
return 0;
}
//-------------------------------------------------------
int mainStaticMatch()
{
IplImage *img1, *img2;
img1 = cvLoadImage("imgs/img1.jpg");
img2 = cvLoadImage("imgs/img2.jpg");
IpVec ipts1, ipts2;
surfDetDes(img1,ipts1,false,4,4,2,0.0001f);
surfDetDes(img2,ipts2,false,4,4,2,0.0001f);
IpPairVec matches;
getMatches(ipts1,ipts2,matches);
for (unsigned int i = 0; i < matches.size(); ++i)
{
drawPoint(img1,matches[i].first);
drawPoint(img2,matches[i].second);
const int & w = img1->width;
cvLine(img1,cvPoint(matches[i].first.x,matches[i].first.y),cvPoint(matches[i].second.x+w,matches[i].second.y), cvScalar(255,255,255),1);
cvLine(img2,cvPoint(matches[i].first.x-w,matches[i].first.y),cvPoint(matches[i].second.x,matches[i].second.y), cvScalar(255,255,255),1);
}
std::cout<< "Matches: " << matches.size();
cvNamedWindow("1", CV_WINDOW_AUTOSIZE );
cvNamedWindow("2", CV_WINDOW_AUTOSIZE );
cvShowImage("1", img1);
cvShowImage("2",img2);
cvWaitKey(0);
return 0;
}
//-------------------------------------------------------
int mainKmeans(void)
{
IplImage *img = cvLoadImage("imgs/img1.jpg");
IpVec ipts;
Kmeans km;
// Get Ipoints
surfDetDes(img,ipts,true,3,4,2,0.0006f);
for (int repeat = 0; repeat < 10; ++repeat)
{
IplImage *img = cvLoadImage("imgs/img1.jpg");
km.Run(&ipts, 5, true);
drawPoints(img, km.clusters);
for (unsigned int i = 0; i < ipts.size(); ++i)
{
cvLine(img, cvPoint(ipts[i].x,ipts[i].y), cvPoint(km.clusters[ipts[i].clusterIndex].x ,km.clusters[ipts[i].clusterIndex].y),cvScalar(255,255,255));
}
showImage(img);
}
return 0;
}
//-------------------------------------------------------
int main(void)
{
if (PROCEDURE == 1) return mainImage();
if (PROCEDURE == 2) return mainVideo();
if (PROCEDURE == 3) return mainMatch();
if (PROCEDURE == 4) return mainMotionPoints();
if (PROCEDURE == 5) return mainStaticMatch();
if (PROCEDURE == 6) return mainKmeans();
}