-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsift.go
352 lines (296 loc) · 7.78 KB
/
sift.go
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
package sift
/*
#cgo CFLAGS : -Iinclude -I/usr/local/include/opencv
#cgo LDFLAGS: -Llib/ubuntu -L/usr/local/lib -lopensift -lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_ocl -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab -ltbb -lXext -lX11 -lICE -lSM -lGL -lGLU -lrt -lpthread -lm -ldl -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgio-2.0 -lpangoft2-1.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lcairo -lpango-1.0 -lfontconfig -lgobject-2.0 -lglib-2.0 -lfreetype
#include "sift.h"
#include "imgfeatures.h"
#include "kdtree.h"
#include "utils.h"
#include "xform.h"
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <stdio.h>
#define KDTREE_BBF_MAX_NN_CHKS 200
#define NN_SQ_DIST_RATIO_THR 0.49
int compare_features(struct feature* f0, int count0,struct feature* f1,int count1){
struct kd_node* kd_root;
double d0, d1;
struct feature** nbrs;
int num_matches = 0;
size_t i;
kd_root = kdtree_build(f1, count1);
for(i = 0; i < count0; ++i) {
int k;
struct feature* feat;
feat = f0 + i;
k = kdtree_bbf_knn( kd_root, feat, 2, &nbrs, KDTREE_BBF_MAX_NN_CHKS );
if( k == 2 ) {
d0 = descr_dist_sq( feat, nbrs[0] );
d1 = descr_dist_sq( feat, nbrs[1] );
if( d0 < d1 * NN_SQ_DIST_RATIO_THR ) ++num_matches;
}
free( nbrs );
}
kdtree_release( kd_root );
return num_matches;
}
int getlplImageSize(IplImage* img) {
if(!img) return 0;
return img->imageSize;
}
*/
import "C"
import (
"fmt"
"unsafe"
"sync"
"github.com/pkg/errors"
)
/*
the test impl
*/
func MatchImpl(path1, path2 string) (int, int, int) {
var features, features2 *C.struct_feature
img := C.cvLoadImage(C.CString(path1), C.int(1))
img2 := C.cvLoadImage(C.CString(path2), C.int(1))
n := C.sift_features(img, &features)
n2 := C.sift_features(img2, &features2)
fmt.Printf("nums %d\nnums %d\n", n, n2)
res := C.compare_features(features, n, features2, n2)
fmt.Printf("res %d\n", res)
// release memory
C.free(unsafe.Pointer(features))
C.free(unsafe.Pointer(features2))
C.cvReleaseImage(&img)
C.cvReleaseImage(&img2)
return int(n), int(n2), int(res)
}
func Match(ie1, ie2 *ImageEntity) bool {
defer func() {
if e := recover(); e != nil {
fmt.Printf("panic recover %s\n", e)
}
}()
return DEFAULT_SIFT_CLIENT.Match(ie1, ie2)
}
const (
DEFAULT_KDTREE_BBF_MAX_NN_CHKS = 200
DEFAULT_NN_SQ_DIST_RATIO_THR = 0.49
)
//pai nao dai
var DEFAULT_THRESHOLDFUNC thresholdFunc = func(f1 int, f2 int, m int) bool {
return float32(m) / float32(f2) >= float32(0.2) || float32(m) / float32(f1) >= float32(0.2) || m > 80
}
type thresholdFunc func(int, int, int) bool
/*
feature_t
C struct feature wrapper
*/
type feature_t struct {
Feat *C.struct_feature
}
type img_t struct {
Img *C.struct__IplImage //don't know why
}
/**
C functions wrapper
*/
func cvLoadImg(path string) *img_t {
return &img_t{
Img: C.cvLoadImage(C.CString(path), C.int(1)),
}
}
func getImageSize(img *img_t) int {
if img == nil {
return 0
}
return int(C.getlplImageSize(img.Img))
}
func freeImg(img *img_t) {
C.cvReleaseImage(&(img.Img))
}
func sift_features(img *img_t) *featureData {
var features *C.struct_feature
n := C.sift_features(img.Img, &features)
return &featureData{
Feature:&feature_t{
Feat:features,
},
Count:int(n),
}
}
func free_featureData(featData *featureData) {
/**
the C.Feat.feature_data has been freed during function sift_features,
here may just need to free the non-pointer memory in this pointer
*/
C.free(unsafe.Pointer(featData.Feature.Feat))
}
func loadFeatureData(ie *ImageEntity) *featureData {
img := cvLoadImg(ie.FilePath)
if img.Img == nil {
panic(fmt.Sprintf("can't load image path %s", ie.FilePath))
}
defer freeImg(img)
featData := sift_features(img)
if featData.Feature.Feat == nil {
panic(fmt.Sprintf("can't load image feature %s", ie.FilePath))
}
featData.ImageEntity = *ie
return featData
}
func compareFeatureData(feature1, feature2 *featureData) int {
return int(C.compare_features(feature1.Feature.Feat, C.int(feature1.Count), feature2.Feature.Feat, C.int(feature2.Count)))
}
/*
imgSignature
*/
type imgSignature struct {
FilePath string
Md5 string
}
func (is *imgSignature)Match(img *imgSignature) bool {
return false
}
/**
featureData
*/
type featureData struct {
Feature *feature_t
Count int
ImageEntity
}
/**
*/
type ImageEntity struct {
FilePath string
UniqueID interface{} //used for cache
}
/**
-------interface----------
*/
type Client interface {
Load(*ImageEntity) error
Match(*ImageEntity, *ImageEntity) bool
/*cache interface*/
}
/*
siftClient
*/
type siftClient struct {
kdtree_bbf_max_nn_chks int
nn_sq_dist_ratio_thr float32
tf thresholdFunc
lru Lru //interface
lruMutex *sync.RWMutex
}
/**
preload cache
*/
func (c *siftClient)Load(ie *ImageEntity) (err error) {
defer func() {
if e := recover(); e != nil {
err = errors.New(e.(string))
}
}()
feature := loadFeatureData(ie)
c.setCache(ie, feature)
return nil
}
/*
TODO there is a panic ,catch it?
*/
func (c *siftClient)Match(ie1 *ImageEntity, ie2 *ImageEntity) bool {
var features1, features2 *featureData
var ok1, ok2 bool
var wg *sync.WaitGroup = new(sync.WaitGroup)
/*
if many coroutine come into this place and check the cache,they all don't get it.
and they all load the real data,lt's not friend to concurrency.
TODO design a flag for spin lock,the after coroutine wait and retry until done?
TODO how to deal with a dead lock when exception?
*/
features1, ok1 = c.getCache(ie1)
features2, ok2 = c.getCache(ie2)
if !ok1 {
wg.Add(1)
go func() {
features1 = loadFeatureData(ie1)
//c.setCache(ie1, features1)
wg.Done()
}()
}
if !ok2 {
wg.Add(1)
go func() {
features2 = loadFeatureData(ie2)
c.setCache(ie2, features2)
wg.Done()
}()
}
wg.Wait()
res := compareFeatureData(features1, features2)
if c.tf != nil {
return c.tf(features1.Count, features2.Count, res)
}else {
return DEFAULT_THRESHOLDFUNC(features1.Count, features2.Count, res)
}
}
func (c *siftClient)getCache(ie *ImageEntity) (*featureData, bool) {
c.lruMutex.RLock()
defer c.lruMutex.RUnlock()
if feature, ok := c.lru.Get(ie.UniqueID); ok {
return feature.(*featureData), ok
}
return nil, false
}
func (c *siftClient)setCache(ie *ImageEntity, feature *featureData) {
c.lruMutex.Lock()
c.lru.Add(ie.UniqueID, feature)
c.lruMutex.Unlock()
}
/*
SiftClientOption
*/
var DEFAULT_SIFT_OPTION SiftClientOption = SiftClientOption{
Kdtree_bbf_max_nn_chks: DEFAULT_KDTREE_BBF_MAX_NN_CHKS,
Nn_sq_dist_ratio_thr:DEFAULT_NN_SQ_DIST_RATIO_THR,
Tf:DEFAULT_THRESHOLDFUNC,
CacheSize: 1024, //1024
}
var DEFAULT_SIFT_CLIENT Client = NewSiftClient(&DEFAULT_SIFT_OPTION)
var DEFAULT_ON_EVICTED func(interface{}, interface{}) = func(key, value interface{}) {
fmt.Printf("on evicted %+v\n", key)
featData := value.(*featureData)
free_featureData(featData)
}
type SiftClientOption struct {
Kdtree_bbf_max_nn_chks int //not used
Nn_sq_dist_ratio_thr float32 //not used
Tf thresholdFunc
CacheSize int //TODO
}
/*
*/
func NewSiftClient(option *SiftClientOption) Client {
if option == nil {
option = &DEFAULT_SIFT_OPTION
}
var c *siftClient = &siftClient{}
if option.Kdtree_bbf_max_nn_chks != 0 {
c.kdtree_bbf_max_nn_chks = option.Kdtree_bbf_max_nn_chks
}else {
c.kdtree_bbf_max_nn_chks = DEFAULT_KDTREE_BBF_MAX_NN_CHKS
}
if option.Nn_sq_dist_ratio_thr <= 1e-7 {
c.nn_sq_dist_ratio_thr = DEFAULT_NN_SQ_DIST_RATIO_THR
}else {
c.nn_sq_dist_ratio_thr = option.Nn_sq_dist_ratio_thr
}
c.lru = NewClassicLru(option.CacheSize)
c.lruMutex = new(sync.RWMutex)
c.lru.SetOnEvicted(DEFAULT_ON_EVICTED)
c.tf = option.Tf
return c
}