forked from hybridgroup/gocv
/
xfeatures2d.go
98 lines (84 loc) · 2.16 KB
/
xfeatures2d.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
package cuda
/*
#include <stdlib.h>
#include "cuda.h"
#include "xfeatures2d.h"
*/
import "C"
import (
"reflect"
"unsafe"
)
// SURF is a wrapper around the cv::cuda::SURF_CUDA.
type SURF struct {
// C.ORB
p unsafe.Pointer
}
// NewSURF returns a new SURF
func NewSURF(threshold float64) SURF {
return SURF{p: unsafe.Pointer(C.CudaSURF_Create(C.double(threshold)))}
}
func (o *SURF) DetectAndCompute(src GpuMat, mask GpuMat) ([]KeyPoint, GpuMat) {
desc := NewGpuMat()
ret := C.CudaSURF_DetectAndCompute((C.CudaSURF)(o.p), src.p, mask.p, desc.p)
defer C.deleteKeypoints(ret)
return getKeyPoints(ret), desc
}
type DMatch struct {
QueryIdx int
TrainIdx int
ImgIdx int
Distance float64
}
func getMultiDMatches(ret C.MultiDMatches) [][]DMatch {
cArray := ret.dmatches
length := int(ret.length)
hdr := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(cArray)),
Len: length,
Cap: length,
}
s := *(*[]C.DMatches)(unsafe.Pointer(&hdr))
keys := make([][]DMatch, length)
for i := range s {
keys[i] = getDMatches(C.CudaMultiDMatches_get(ret, C.int(i)))
}
return keys
}
func getDMatches(ret C.DMatches) []DMatch {
cArray := ret.dmatches
length := int(ret.length)
hdr := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(cArray)),
Len: length,
Cap: length,
}
s := *(*[]C.DMatch)(unsafe.Pointer(&hdr))
keys := make([]DMatch, length)
for i, r := range s {
keys[i] = DMatch{int(r.queryIdx), int(r.trainIdx), int(r.imgIdx),
float64(r.distance)}
}
return keys
}
// BFMatcher is a wrapper around the the cv::cuda::DescriptionMatcher algorithm
type BFMatcher struct {
// C.BFMatcher
p unsafe.Pointer
}
// NewBFMatcher returns a new BFMatcher
func NewBFMatcher() BFMatcher {
return BFMatcher{p: unsafe.Pointer(C.CudaBFMatcher_Create())}
}
// Close BFMatcher
func (b *BFMatcher) Close() error {
C.CudaBFMatcher_Close((C.CudaBFMatcher)(b.p))
b.p = nil
return nil
}
// KnnMatch Finds the k best matches for each descriptor from a query set.
func (b *BFMatcher) KnnMatch(query, train GpuMat, k int) [][]DMatch {
ret := C.CudaBFMatcher_KnnMatch((C.CudaBFMatcher)(b.p), query.p, train.p, C.int(k))
defer C.CudaMultiDMatches_Close(ret)
return getMultiDMatches(ret)
}