VLFeat SIFT with OpenCV + Code

So, you used the VLFeat  SIFT successfuly in Matlab but you need to use the library with C++ and you can't find the functions reference nor a tutorial? Well I have been there, done that and sharing the code for integrating VLFeat's SIFT with OpenCV.

 Actually I harvest the below code from the 'Toolbox' folder of VLFeat, from the source of the dot mex files. With minor modification the ' VLSIFT' function takes an OpenCV IplImage (grayscale) and returns pointers to the frames and descriptors found as well as the number of the frames. I removed the selection for exporting the descriptors in double; they always be exported as the vl_uint8 datatype. 



?
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
void VLSIFT(IplImage* image, vl_uint8* DATAdescr, double * DATAframes, int * nframes){
 
    //Take IplImage -> convert to SINGLE (float):
 
    float * frame = ( float *) malloc (image->height*image->width* sizeof ( float ));
 
    uchar* Ldata      = (uchar *)image->imageData;
 
    for ( int i=0;i<image->height;i  )
 
        for ( int j=0;j<image->width;j  )
 
            frame[j*image->height i*image->nChannels] = ( float )Ldata[i*image->widthStep j*image->nChannels];
 
 
 
    // VL SIFT computation:
 
    vl_sift_pix const *data ;
 
    int                 M, N ;
 
    data = (vl_sift_pix*)frame;
 
    M = image->height;
 
    N = image->width;
 
    
 
    // VL SIFT PARAMETERS
 
    int                 verbose = 1 ; // change to 2 for more verbose..
 
    int                 O     =   -1 ; //Octaves
 
    int                 S     =   3 ; //Levels
 
    int                 o_min =   0 ;
 
 
 
    double             edge_thresh = -1 ;   //-1 will use the default (as in matlab)
 
    double             peak_thresh =  -1 ;
 
    double             norm_thresh = -1 ;
 
    double             magnif      = -1 ;
 
    double             window_size = -1 ;
 
 
 
    double             *ikeys = 0 ; //?
 
    int                 nikeys = -1 ; //?
 
    vl_bool            force_orientations = 0 ;
 
    vl_bool            floatDescriptors = 0 ;
 
 
 
    /* -----------------------------------------------------------------
 
    *                                                            Do job
 
    * -------------------------------------------------------------- */
 
    {
 
        VlSiftFilt        *filt ;
 
        vl_bool            first ;
 
        double             *frames = 0 ;
 
        vl_uint8              *descr  = 0 ;
 
        int                 reserved = 0, i,j,q ;
 
 
 
        /* create a filter to process the image */
 
        filt = vl_sift_new (M, N, O, S, o_min) ;
 
 
 
        if (peak_thresh >= 0) vl_sift_set_peak_thresh (filt, peak_thresh) ;
 
        if (edge_thresh >= 0) vl_sift_set_edge_thresh (filt, edge_thresh) ;
 
        if (norm_thresh >= 0) vl_sift_set_norm_thresh (filt, norm_thresh) ;
 
        if (magnif      >= 0) vl_sift_set_magnif      (filt, magnif) ;
 
        if (window_size >= 0) vl_sift_set_window_size (filt, window_size) ;
 
 
 
        if (verbose) {
 
            printf ( "vl_sift: filter settings:\n" ) ;
 
            printf ( "vl_sift:   octaves      (O)      = %d\n" ,
 
                vl_sift_get_noctaves      (filt)) ;
 
            printf ( "vl_sift:   levels       (S)      = %d\n" ,
 
                vl_sift_get_nlevels       (filt)) ;
 
            printf ( "vl_sift:   first octave (o_min)  = %d\n" ,
 
                vl_sift_get_octave_first  (filt)) ;
 
            printf ( "vl_sift:   edge thresh           = %g\n" ,
 
                vl_sift_get_edge_thresh   (filt)) ;
 
            printf ( "vl_sift:   peak thresh           = %g\n" ,
 
                vl_sift_get_peak_thresh   (filt)) ;
 
            printf ( "vl_sift:   norm thresh           = %g\n" ,
 
                vl_sift_get_norm_thresh   (filt)) ;
 
            printf ( "vl_sift:   window size           = %g\n" ,
 
                vl_sift_get_window_size   (filt)) ;
 
            printf ( "vl_sift:   float descriptor      = %d\n" ,
 
                floatDescriptors) ;
 
 
 
            printf ((nikeys >= 0) ?
 
                "vl_sift: will source frames? yes (%d read)\n" :
 
            "vl_sift: will source frames? no\n" , nikeys) ;
 
            printf ( "vl_sift: will force orientations? %s\n" ,
 
                force_orientations ? "yes" : "no" ) ;
 
        }
 
 
 
        /* ...............................................................
 
        *                                             Process each octave
 
        * ............................................................ */
 
        i     = 0 ;
 
        first = 1 ;
 
        while (1) {
 
            int                   err ;
 
            VlSiftKeypoint const *keys  = 0 ;
 
            int                   nkeys = 0 ;
 
 
 
            if (verbose) {
 
                printf ( "vl_sift: processing octave %d\n" ,
 
                    vl_sift_get_octave_index (filt)) ;
 
            }
 
 
 
            /* Calculate the GSS for the next octave .................... */
 
            if (first) {
 
                err   = vl_sift_process_first_octave (filt, data) ;
 
                first = 0 ;
 
            } else {
 
                err   = vl_sift_process_next_octave  (filt) ;
 
            }
 
 
 
            if (err) break ;
 
 
 
            if (verbose > 1) {
 
                printf ( "vl_sift: GSS octave %d computed\n" ,
 
                    vl_sift_get_octave_index (filt));
 
            }
 
 
 
            /* Run detector ............................................. */
 
            if (nikeys < 0) {
 
                vl_sift_detect (filt) ;
 
 
 
                keys  = vl_sift_get_keypoints  (filt) ;
 
                nkeys = vl_sift_get_nkeypoints (filt) ;
 
                i     = 0 ;
 
 
 
                if (verbose > 1) {
 
                    printf ( "vl_sift: detected %d (unoriented) keypoints\n" , nkeys) ;
 
                }
 
            } else {
 
                nkeys = nikeys ;
 
            }
 
 
 
            /* For each keypoint ........................................ */
 
            for (; i < nkeys ;   i) {
 
                double                 angles [4] ;
 
                int                   nangles ;
 
                VlSiftKeypoint        ik ;
 
                VlSiftKeypoint const *k ;
 
 
 
                /* Obtain keypoint orientations ........................... */
 
                if (nikeys >= 0) {
 
                    vl_sift_keypoint_init (filt, &ik,
 
                        ikeys [4 * i   1] - 1,
 
                        ikeys [4 * i   0] - 1,
 
                        ikeys [4 * i   2]) ;
 
 
 
                    if (ik.o != vl_sift_get_octave_index (filt)) {
 
                        break ;
 
                    }
 
 
 
                    k = &ik ;
 
 
 
                    /* optionally compute orientations too */
 
                    if (force_orientations) {
 
                        nangles = vl_sift_calc_keypoint_orientations
 
                            (filt, angles, k) ;
 
                    } else {
 
                        angles [0] = VL_PI / 2 - ikeys [4 * i   3] ;
 
                        nangles    = 1 ;
 
                    }
 
                } else {
 
                    k = keys   i ;
 
                    nangles = vl_sift_calc_keypoint_orientations
 
                        (filt, angles, k) ;
 
                }
 
 
 
                /* For each orientation ................................... */
 
                for (q = 0 ; q < nangles ;   q) {
 
                    vl_sift_pix  buf [128] ;
 
                    vl_sift_pix rbuf [128] ;
 
 
 
                    /* compute descriptor (if necessary) */
 
                    vl_sift_calc_keypoint_descriptor (filt, buf, k, angles [q]) ;
 
                    transpose_descriptor (rbuf, buf) ;
 
 
 
                    /* make enough room for all these keypoints and more */
 
                    if (reserved < (*nframes)   1) {
 
                        reserved  = 2 * nkeys ;
 
                        frames = ( double *) realloc (frames, 4 * sizeof ( double ) * reserved) ;
 
                        descr  = (vl_uint8*) realloc (descr,  128 * sizeof (vl_uint8) * reserved) ;
 
                    }
 
 
 
                    /* Save back with MATLAB conventions. Notice tha the input
 
                    * image was the transpose of the actual image. */
 
                    frames [4 * (*nframes)   0] = k -> y ;
 
                    frames [4 * (*nframes)   1] = k -> x ;
 
                    frames [4 * (*nframes)   2] = k -> sigma ;
 
                    frames [4 * (*nframes)   3] = VL_PI / 2 - angles [q] ;
 
 
 
                    for (j = 0 ; j < 128 ;   j) {
 
                        float x = 512.0F * rbuf [j] ;
 
                        x = (x < 255.0F) ? x : 255.0F ;
 
                        descr[128 * (*nframes)   j] = (vl_uint8)x ;
 
                    }
 
 
 
                        (*nframes) ;
 
                } /* next orientation */
 
            } /* next keypoint */
 
        } /* next octave */
 
 
 
        if (verbose) {
 
            printf ( "vl_sift: found %d keypoints\n" , (*nframes)) ;
 
        }
 
        // save variables:
 
        memcpy (DATAframes, frames, 4 * (*nframes ) * sizeof ( double ));
 
        memcpy (DATAdescr, descr, 128 * (*nframes ) * sizeof (vl_uint8));
 
 
 
        /* cleanup */
 
        vl_sift_delete (filt) ;
 
    } /* end: do job */
 
 
 
 
 
    return ;
 
}



Concecuently the next function will compute matches between to pictures using the matlab's 'vl_ubcmatch' function transformed for C++. Again I harvest this from the source of the .mex file. I deleted the macros used to choose between datatypes, because I use the vl_uint8 type for my descriptors (I think is the vl's default). So if you want to use a different datatype, don'd forget to change this.



?
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
void VLMATCH(vl_uint8* L1_pt,vl_uint8* L2_pt, int K1, int K2, double thresh, int * nMatches, double * MATCHES ){
 
    //Match descriptors!
 
 
 
    int ND = 128;
 
 
 
    Pair* pairs_begin = (Pair*) malloc ( sizeof (Pair) * (K1 K2)) ;
 
    Pair* pairs_iterator = pairs_begin ;
 
 
 
    int k1, k2 ;                                                      
 
    const int maxval = 0x7fffffff ;                        
 
    for (k1 = 0 ; k1 < K1 ;   k1, L1_pt  = ND ) {     //kalooo!                  
 
 
 
        int best = maxval ;                                    
 
        int second_best = maxval ;                            
 
        int bestk = -1 ;                                                
 
 
 
        /* For each point P2[k2] in the second image... */              
 
        for (k2 =  0 ; k2 < K2 ;   k2, L2_pt  = ND) {                    
 
 
 
            int bin ;                                                      
 
            int acc = 0 ;                                        
 
            for (bin = 0 ; bin < ND ;   bin) {                              
 
                int delta =                                        
 
                    (( int ) L1_pt[bin]) -                            
 
                    (( int ) L2_pt[bin]) ;                            
 
                acc  = delta*delta ;                                        
 
            }                                                              
 
 
 
            /* Filter the best and second best matching point. */          
 
            if (acc < best) {                                              
 
                second_best = best ;                                        
 
                best = acc ;                                                
 
                bestk = k2 ;                                                
 
            } else if (acc < second_best) {                                
 
                second_best = acc ;                                          
 
            }                                                              
 
        }                                                                
 
 
 
        L2_pt -= ND*K2 ;                                                
 
 
 
        /* Lowe's method: accept the match only if unique. */            
 
        if (thresh * ( float ) best < ( float ) second_best &&                
 
            bestk != -1) {                                                
 
                pairs_iterator->k1 = k1 ;                                      
 
                pairs_iterator->k2 = bestk ;                                  
 
                pairs_iterator->score = best ;                                
 
                pairs_iterator   ;
 
                (*nMatches)  ;
 
        }                                                                
 
    }                                                                  
 
 
 
    Pair* pairs_end = pairs_iterator ;
 
    //double* M_pt = (double*)calloc((pairs_end-pairs_begin)*2,sizeof(double));
 
    double * M_pt = ( double *) calloc ((*nMatches)*2, sizeof ( double ));
 
    //double* M_start = M_pt;
 
 
 
    for (pairs_iterator = pairs_begin ;
 
        pairs_iterator < pairs_end  ;
 
          pairs_iterator) {
 
            *M_pt   = pairs_iterator->k1 ;
 
            *M_pt   = pairs_iterator->k2 ;
 
    }
 
    M_pt -= (*nMatches)*2 ;
 
    memcpy (MATCHES,M_pt,(*nMatches) * 2 * sizeof ( double ));
 
    free (pairs_begin) ;
 
    free (M_pt);
 
 
 
    return ;
 
}


Finally  call the functions in the main. Optionally plot the image with the found frames overlapped using OpenCV.




?
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
typedef unsigned char uchar;
 
int main()
 
{
 
    // Load template image:
 
    IplImage* Timage = cvLoadImage( "T.png" ,0);
 
 
 
    double *            TFrames = ( double *) calloc ( 4 * 10000, sizeof ( double ) ) ;
 
    vl_uint8*          TDescr  = (vl_uint8*) calloc ( 128 * 10000, sizeof (vl_uint8) ) ;
 
    int                 Tnframes = 0;
 
    VLSIFT(Timage, TDescr, TFrames, &Tnframes);
 
    TFrames = ( double *) realloc (TFrames, 4 * sizeof ( double ) * Tnframes) ; // = Y X Scale Angle
 
    TDescr  = (vl_uint8*) realloc (TDescr,  128 * sizeof (vl_uint8) * Tnframes) ;
 
    
 
    /*
 
    for(int i=0;i<Tnframes;i  ){
 
        cvCircle(Timage,              
 
        cvPoint(TFrames[0 i*4], TFrames[1 i*4]), TFrames[2 i*4],  
 
        cvScalar(255, 0, 0, 0),
 
        1, 8, 0);
 
    }
 
    cvShowImage("FrameT", Timage);
 
    cvWaitKey(0);
 
    */
 
return (0);
 
}
 
<span class = "Apple-tab-span" style= "white-space: pre;" > </span>


Sorry for the extra spaces in the code. I'm still familiarizing with the syntaxhighlighter..


Steve
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值