-
Notifications
You must be signed in to change notification settings - Fork 5
/
mt.go
319 lines (274 loc) · 8.64 KB
/
mt.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
package blurry
/*
#cgo CFLAGS: -I${SRCDIR}/include
#cgo darwin LDFLAGS: -L${SRCDIR}/lib -lruntime_osx -ldl -lm
#cgo darwin LDFLAGS: -lmatch_template_sad_osx
#cgo darwin LDFLAGS: -lmatch_template_ssd_osx
#cgo darwin LDFLAGS: -lmatch_template_ncc_osx
#cgo darwin LDFLAGS: -lmatch_template_zncc_osx
#cgo darwin LDFLAGS: -lprepare_ncc_template_osx
#cgo darwin LDFLAGS: -lprepared_match_template_ncc_osx
#cgo darwin LDFLAGS: -lprepare_zncc_template_osx
#cgo darwin LDFLAGS: -lprepared_match_template_zncc_osx
#cgo linux LDFLAGS: -L${SRCDIR}/lib -lruntime_linux -ldl -lm
#cgo linux LDFLAGS: -lmatch_template_sad_linux
#cgo linux LDFLAGS: -lmatch_template_ssd_linux
#cgo linux LDFLAGS: -lmatch_template_ncc_linux
#cgo linux LDFLAGS: -lmatch_template_zncc_linux
#cgo linux LDFLAGS: -lprepare_ncc_template_linux
#cgo linux LDFLAGS: -lprepared_match_template_ncc_linux
#cgo linux LDFLAGS: -lprepare_zncc_template_linux
#cgo linux LDFLAGS: -lprepared_match_template_zncc_linux
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "mt.h"
*/
import "C"
import (
"bytes"
"encoding/binary"
"errors"
"image"
"math"
)
var (
ErrMatchTemplateSAD = errors.New("match_template_sad cgo call error")
ErrMatchTemplateSSD = errors.New("match_template_ssd cgo call error")
ErrMatchTemplateNCC = errors.New("match_template_ncc cgo call error")
ErrMatchTemplateZNCC = errors.New("match_template_zncc cgo call error")
ErrCreatePrepareNCCTemplate = errors.New("create_prepare_ncc_template cgo call error")
ErrPrepareNCCTemplate = errors.New("prepare_ncc_template cgo call error")
ErrPreparedMatchTemplateNCC = errors.New("prepated_match_template_ncc cgo call error")
ErrCreatePrepareZNCCTemplate = errors.New("create_prepare_zncc_template cgo call error")
ErrPrepareZNCCTemplate = errors.New("prepare_zncc_template cgo call error")
ErrPreparedMatchTemplateZNCC = errors.New("prepated_match_template_zncc cgo call error")
)
type PreparedNCCTpl struct {
prepared *C.prepared_ncc_template_t
}
type PreparedZNCCTpl struct {
prepared *C.prepared_zncc_template_t
}
type MatchTemplateIntScore struct {
Point image.Point
Score uint32
}
type MatchTemplateFloatScore struct {
Point image.Point
Score float64
}
type byteSliceToIntScore func(data []byte) uint32
type byteSliceToFloatScore func(data []byte) float64
func readUint16(data []byte) uint32 {
score := binary.LittleEndian.Uint16(data)
return uint32(score)
}
func readInt32(data []byte) uint32 {
var score int32
buf := bytes.NewBuffer(data)
if err := binary.Read(buf, binary.LittleEndian, &score); err != nil {
return math.MaxUint32
}
return uint32(score)
}
func readFloat32(data []byte) float64 {
var score float64
buf := bytes.NewBuffer(data)
if err := binary.Read(buf, binary.LittleEndian, &score); err != nil {
return -1.0
}
return score
}
func makeScoresUint16(data []byte, width, height int, threshold uint16, bitSize int) []MatchTemplateIntScore {
return makeIntScore(data, width, height, uint32(threshold), readUint16, bitSize)
}
func makeScoresInt32(data []byte, width, height int, threshold uint32, bitSize int) []MatchTemplateIntScore {
return makeIntScore(data, width, height, threshold, readInt32, bitSize)
}
func makeScoresFloat32(data []byte, width, height int, threshold float64, bitSize int) []MatchTemplateFloatScore {
return makeFloatScore(data, width, height, threshold, readFloat32, bitSize)
}
func makeIntScore(data []byte, width, height int, threshold uint32, reader byteSliceToIntScore, bitSize int) []MatchTemplateIntScore {
scores := make([]MatchTemplateIntScore, 0, width*height)
offset := 0
for y := 0; y < height; y += 1 {
for x := 0; x < width; x += 1 {
score := reader(data[offset : offset+bitSize])
offset += bitSize
if threshold < score { // best match is nearest to 0
continue
}
scores = append(scores, MatchTemplateIntScore{
Point: image.Pt(x, y),
Score: score,
})
}
}
return scores
}
func makeFloatScore(data []byte, width, height int, threshold float64, reader byteSliceToFloatScore, bitSize int) []MatchTemplateFloatScore {
scores := make([]MatchTemplateFloatScore, 0, width*height)
offset := 0
for y := 0; y < height; y += 1 {
for x := 0; x < width; x += 1 {
score := reader(data[offset : offset+bitSize])
offset += bitSize
if math.IsNaN(score) {
continue
}
if score < threshold { // best match is nearest to 1.0
continue
}
scores = append(scores, MatchTemplateFloatScore{
Point: image.Pt(x, y),
Score: score,
})
}
}
return scores
}
func MatchTemplateSAD(img *image.RGBA, tpl *image.RGBA, threshold uint16) ([]MatchTemplateIntScore, error) {
width, height := wh(img)
tplWidth, tplHeight := wh(tpl)
out := GetByteBuf(width * height * C.sizeof_uint16_t)
defer PutByteBuf(out)
ret := C.libmtsad(
(*C.uchar)(&img.Pix[0]),
C.int(width),
C.int(height),
(*C.uchar)(&tpl.Pix[0]),
C.int(tplWidth),
C.int(tplHeight),
(*C.uchar)(&out[0]),
)
if int(ret) != 0 {
return nil, ErrMatchTemplateSAD
}
return makeScoresUint16(out, width, height, threshold, C.sizeof_uint16_t), nil
}
func MatchTemplateSSD(img *image.RGBA, tpl *image.RGBA, threshold uint32) ([]MatchTemplateIntScore, error) {
width, height := wh(img)
tplWidth, tplHeight := wh(tpl)
out := GetByteBuf(width * height * C.sizeof_int32_t)
defer PutByteBuf(out)
ret := C.libmtssd(
(*C.uchar)(&img.Pix[0]),
C.int(width),
C.int(height),
(*C.uchar)(&tpl.Pix[0]),
C.int(tplWidth),
C.int(tplHeight),
(*C.uchar)(&out[0]),
)
if int(ret) != 0 {
return nil, ErrMatchTemplateSSD
}
return makeScoresInt32(out, width, height, threshold, C.sizeof_int32_t), nil
}
func MatchTemplateNCC(img *image.RGBA, tpl *image.RGBA, threshold float64) ([]MatchTemplateFloatScore, error) {
width, height := wh(img)
tplWidth, tplHeight := wh(tpl)
out := GetByteBuf(width * height * C.sizeof_double)
defer PutByteBuf(out)
ret := C.libmtncc(
(*C.uchar)(&img.Pix[0]),
C.int(width),
C.int(height),
(*C.uchar)(&tpl.Pix[0]),
C.int(tplWidth),
C.int(tplHeight),
(*C.uchar)(&out[0]),
)
if int(ret) != 0 {
return nil, ErrMatchTemplateNCC
}
return makeScoresFloat32(out, width, height, threshold, C.sizeof_double), nil
}
func PrepareNCCTemplate(tpl *image.RGBA) (*PreparedNCCTpl, error) {
tplWidth, tplHeight := wh(tpl)
p := C.create_prepare_ncc_template(C.int(tplWidth), C.int(tplHeight))
if p == nil {
return nil, ErrCreatePrepareNCCTemplate
}
ret := C.libprepare_ncc_tpl(
(*C.uchar)(&tpl.Pix[0]),
p,
)
if ret != 0 {
return nil, ErrPrepareNCCTemplate
}
return &PreparedNCCTpl{prepared: p}, nil
}
func PrepareZNCCTemplate(tpl *image.RGBA) (*PreparedZNCCTpl, error) {
tplWidth, tplHeight := wh(tpl)
p := C.create_prepare_zncc_template(C.int(tplWidth), C.int(tplHeight))
if p == nil {
return nil, ErrCreatePrepareZNCCTemplate
}
ret := C.libprepare_zncc_tpl(
(*C.uchar)(&tpl.Pix[0]),
p,
)
if ret != 0 {
return nil, ErrPrepareZNCCTemplate
}
return &PreparedZNCCTpl{prepared: p}, nil
}
func FreePreparedNCCTemplate(ptpl *PreparedNCCTpl) {
C.free_prepare_ncc_template(ptpl.prepared)
}
func FreePreparedZNCCTemplate(ptpl *PreparedZNCCTpl) {
C.free_prepare_zncc_template(ptpl.prepared)
}
func PreparedMatchTemplateNCC(img *image.RGBA, ptpl *PreparedNCCTpl, threshold float64) ([]MatchTemplateFloatScore, error) {
width, height := wh(img)
out := GetByteBuf(width * height * C.sizeof_double)
defer PutByteBuf(out)
ret := C.libprepared_mtncc(
(*C.uchar)(&img.Pix[0]),
C.int(width),
C.int(height),
ptpl.prepared,
(*C.uchar)(&out[0]),
)
if int(ret) != 0 {
return nil, ErrPrepareNCCTemplate
}
return makeScoresFloat32(out, width, height, threshold, C.sizeof_double), nil
}
func MatchTemplateZNCC(img *image.RGBA, tpl *image.RGBA, threshold float64) ([]MatchTemplateFloatScore, error) {
width, height := wh(img)
tplWidth, tplHeight := wh(tpl)
out := GetByteBuf(width * height * C.sizeof_double)
defer PutByteBuf(out)
ret := C.libmtzncc(
(*C.uchar)(&img.Pix[0]),
C.int(width),
C.int(height),
(*C.uchar)(&tpl.Pix[0]),
C.int(tplWidth),
C.int(tplHeight),
(*C.uchar)(&out[0]),
)
if int(ret) != 0 {
return nil, ErrMatchTemplateZNCC
}
return makeScoresFloat32(out, width, height, threshold, C.sizeof_double), nil
}
func PreparedMatchTemplateZNCC(img *image.RGBA, ptpl *PreparedZNCCTpl, threshold float64) ([]MatchTemplateFloatScore, error) {
width, height := wh(img)
out := GetByteBuf(width * height * C.sizeof_double)
defer PutByteBuf(out)
ret := C.libprepared_mtzncc(
(*C.uchar)(&img.Pix[0]),
C.int(width),
C.int(height),
ptpl.prepared,
(*C.uchar)(&out[0]),
)
if int(ret) != 0 {
return nil, ErrPrepareZNCCTemplate
}
return makeScoresFloat32(out, width, height, threshold, C.sizeof_double), nil
}