forked from foxis/EasyRobot
/
features.gocv.go
144 lines (123 loc) · 2.99 KB
/
features.gocv.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
package features
import (
"github.com/itohio/EasyRobot/pkg/backend"
"github.com/itohio/EasyRobot/pkg/core/options"
"github.com/itohio/EasyRobot/pkg/core/pipeline"
"github.com/itohio/EasyRobot/pkg/core/pipeline/steps"
"github.com/itohio/EasyRobot/pkg/core/plugin"
"github.com/itohio/EasyRobot/pkg/core/store"
"gocv.io/x/gocv"
)
type GOCVDetector interface {
Detect(gocv.Mat) []gocv.KeyPoint
DetectAndCompute(src gocv.Mat, mask gocv.Mat) ([]gocv.KeyPoint, gocv.Mat)
}
type GOCVMatcher interface {
KnnMatch(query, train gocv.Mat, k int) [][]gocv.DMatch
}
type stepImpl struct {
options Options
step pipeline.Step
orb gocv.ORB
kaze gocv.KAZE
akaze gocv.AKAZE
brisk gocv.BRISK
sift gocv.SIFT
fast gocv.FastFeatureDetector
gftt gocv.GFTTDetector
matcherFlann gocv.FlannBasedMatcher
matcherBF gocv.BFMatcher
matcher GOCVMatcher
detector GOCVDetector
}
func init() {
pipeline.Register(NAME, NewGoCV)
}
func NewGoCV(opts ...options.Option) (pipeline.Step, error) {
algorithm := &stepImpl{
options: Options{
featureType: ORB,
},
}
options.ApplyOptions(&algorithm.options, opts...)
data := store.New()
data.Set(store.FEATURES_TYPE, algorithm.options.featureType)
data.Set(store.MATCHER, algorithm)
newOpts := append([]options.Option{plugin.WithName(NAME)}, opts...)
newOpts = append(
newOpts,
steps.WithProcessor(algorithm),
steps.WithFields(data),
)
step, err := steps.NewProcessor(newOpts...)
if err != nil {
return nil, err
}
return step, nil
}
func (s *stepImpl) Init() error {
switch s.options.featureType {
case ORB:
s.orb = gocv.NewORB()
s.matcherFlann = gocv.NewFlannBasedMatcher()
s.matcher = &s.matcherFlann
s.detector = &s.orb
case SIFT:
s.sift = gocv.NewSIFT()
s.matcherBF = gocv.NewBFMatcher()
s.matcher = &s.matcherBF
s.detector = &s.sift
case KAZE:
s.kaze = gocv.NewKAZE()
s.matcherFlann = gocv.NewFlannBasedMatcher()
s.matcher = &s.matcherFlann
s.detector = &s.kaze
case AKAZE:
s.akaze = gocv.NewAKAZE()
s.matcherFlann = gocv.NewFlannBasedMatcher()
s.matcher = &s.matcherFlann
s.detector = &s.akaze
case BRISK:
s.brisk = gocv.NewBRISK()
s.matcherFlann = gocv.NewFlannBasedMatcher()
s.matcher = &s.matcherFlann
s.detector = &s.brisk
// case FAST:
// s.fast = gocv.NewFastFeatureDetector()
// s.detector = &s.fast
default:
return ErrNotSupported
}
return nil
}
func (s *stepImpl) Reset() {
return
}
func (s *stepImpl) Close() {
return
}
func (s *stepImpl) Process(src, dst pipeline.Data) error {
imgVal, ok := src.Get(store.IMAGE)
if !ok {
return nil
}
img, ok := imgVal.(*gocv.Mat)
if !ok {
return nil
}
keyPoints, descriptors := s.detector.DetectAndCompute(*img, gocv.NewMat())
dst.Set(store.KEY_POINTS, keyPoints)
dst.Set(store.DESCRIPTORS, backend.FromGoCVMat(descriptors))
return nil
}
func (s *stepImpl) Match(a, b interface{}) interface{} {
query, ok := a.(*gocv.Mat)
if !ok {
return nil
}
train, ok := b.(*gocv.Mat)
if !ok {
return nil
}
return s.matcher.KnnMatch(*query, *train, 2)
}