-
Notifications
You must be signed in to change notification settings - Fork 4
/
cv.go
473 lines (390 loc) · 12.4 KB
/
cv.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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// Copyright 2016 Evans. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gcv
import (
"image"
"image/color"
"github.com/go-vgo/gt/hset"
"github.com/vcaesar/imgo"
"gocv.io/x/gocv"
)
// FindImgFile find image file in subfile
func FindImgFile(tempFile, file string, flag ...int) (float32, float32, image.Point, image.Point) {
return FindImgMatC(IMRead(file, flag...), IMRead(tempFile, flag...))
}
// FindImg find image in the subImg
func FindImg(subImg, imgSource image.Image) (float32, float32, image.Point, image.Point) {
m1, _ := ImgToMatA(imgSource)
m2, _ := ImgToMatA(subImg)
return FindImgMatC(m1, m2)
}
// FindImgByte find image in the subImg by []byte
func FindImgByte(subImg, imgSource []byte) (float32, float32, image.Point, image.Point) {
m1, _ := imgo.ByteToImg(imgSource)
m2, _ := imgo.ByteToImg(subImg)
return FindImg(m2, m1)
}
// FindImgX find image in the subImg return x, y
func FindImgX(subImg, imgSource image.Image) (int, int) {
_, _, _, maxLoc := FindImg(subImg, imgSource)
return maxLoc.X, maxLoc.Y
}
// FindImgMatC find the image Mat in the temp Mat and close gocv.Mat
func FindImgMatC(imgSource, temp gocv.Mat) (float32, float32, image.Point, image.Point) {
defer imgSource.Close()
defer temp.Close()
return FindImgMat(imgSource, temp)
}
// FindImgMat find the image Mat in the temp Mat
func FindImgMat(imgSource, temp gocv.Mat) (float32, float32, image.Point, image.Point) {
res := gocv.NewMat()
defer res.Close()
msk := gocv.NewMat()
defer msk.Close()
gocv.MatchTemplate(imgSource, temp, &res, gocv.TmCcoeffNormed, msk)
minVal, maxVal, minLoc, maxLoc := gocv.MinMaxLoc(res)
return minVal, maxVal, minLoc, maxLoc
}
// FlannbasedMatch new flann based match
func FlannbasedMatch(query, train gocv.Mat, k int) [][]gocv.DMatch {
fb := gocv.NewFlannBasedMatcher()
return fb.KnnMatch(query, train, k)
}
// Point the image X, Y point structure
type Point struct {
X, Y int
}
// Size the image size structure
type Size struct {
W, H int
}
// Rect the image rectangles structure
type Rect struct {
TopLeft, TopRight Point
BottomLeft, BottomRight Point
}
// Result find template result structure
type Result struct {
Middle, TopLeft Point // maxLoc
Rects Rect
MaxVal []float32
ImgSize Size
}
// Find find all the img search in the img source by
// find all template and sift and return Result
func Find(imgSearch, imgSource image.Image, args ...interface{}) (r Result) {
res := FindAll(imgSearch, imgSource, args...)
if len(res) > 0 {
r = res[0]
}
return
}
// FindX find all the img search in the img source by
// find all template and sift and return x, y
func FindX(imgSearch, imgSource image.Image, args ...interface{}) (x, y int) {
res := Find(imgSearch, imgSource, args...)
x, y = res.Middle.X, res.Middle.Y
return
}
// FindAllX find all the img search in the img source by
// find all template and sift and return []x, []y
func FindAllX(imgSearch, imgSource image.Image, args ...interface{}) (x, y []int) {
res := FindAll(imgSearch, imgSource, args...)
for i := 0; i < len(res); i++ {
x = append(x, res[i].Middle.X)
y = append(y, res[i].Middle.Y)
}
return
}
// FindAllImg find the search image all template in the source image return []Result
func FindAllImg(imgSearch, imgSource image.Image, args ...interface{}) []Result {
imSource, _ := ImgToMatA(imgSource)
imSearch, _ := ImgToMatA(imgSearch)
return FindAllTemplateC(imSource, imSearch, args...)
}
// FindAll find all the img search in the img source by
// find all template and sift and return []Result
func FindAll(imgSearch, imgSource image.Image, args ...interface{}) []Result {
imSource, _ := ImgToMatA(imgSource)
imSearch, _ := ImgToMatA(imgSearch)
res := FindAllTemplateC(imSource, imSearch, args...)
if len(res) <= 0 {
res = FindAllSiftC(imSource, imSearch, args...)
}
return res
}
// FindAllImgFlie find the search image all template in the source image file
// return []Result
func FindAllImgFile(fileSearh, file string, args ...interface{}) []Result {
return FindAllTemplateC(IMRead(file), IMRead(fileSearh), args...)
}
// FindMultiAllImgFile find the multi file search image all template
// in the file source image return [][]Result
func FindMultiAllImgFile(fileSearh []string, file string, args ...interface{}) [][]Result {
imSource := IMRead(file)
var imSearch []gocv.Mat
for i := 0; i < len(fileSearh); i++ {
search := IMRead(fileSearh[i])
imSearch = append(imSearch, search)
}
return FindMultiAllTemplateC(imSource, imSearch, args...)
}
// FindMultiAllImg find the multi search image all template in the source image
// return [][]Result
func FindMultiAllImg(imgSearch []image.Image, imgSource image.Image, args ...interface{}) [][]Result {
imSource, _ := ImgToMatA(imgSource)
var imSearch []gocv.Mat
for i := 0; i < len(imgSearch); i++ {
search, _ := ImgToMatA(imgSearch[i])
imSearch = append(imSearch, search)
}
return FindMultiAllTemplateC(imSource, imSearch, args...)
}
// FindMultiAllTemplate find the multi imgSearch all template in the imgSource return [][]Result
// and close gocv.Mat
func FindMultiAllTemplateC(imgSource gocv.Mat, imgSearch []gocv.Mat, args ...interface{}) (r [][]Result) {
defer imgSource.Close()
for i := 0; i < len(imgSearch); i++ {
r = append(r, FindAllTemplateCS(imgSource, imgSearch[i], args...))
}
return
}
// FindMultiAllTemplate find the multi imgSearch all template in the imgSource return [][]Result
func FindMultiAllTemplate(imgSource gocv.Mat, imgSearch []gocv.Mat, args ...interface{}) (r [][]Result) {
for i := 0; i < len(imgSearch); i++ {
r = append(r, FindAllTemplate(imgSource, imgSearch[i], args...))
}
return
}
// FindAllTemplateCS find the imgSearch all template in the imgSource return []Result
// and close gocv.Mat
func FindAllTemplateCS(imgSource, imgSearch gocv.Mat, args ...interface{}) []Result {
// defer imgSource.Close()
defer imgSearch.Close()
return FindAllTemplate(imgSource, imgSearch, args...)
}
// FindAllTemplateC find the imgSearch all template in the imgSource return []Result
// and close gocv.Mat
func FindAllTemplateC(imgSource, imgSearch gocv.Mat, args ...interface{}) []Result {
defer imgSource.Close()
defer imgSearch.Close()
return FindAllTemplate(imgSource, imgSearch, args...)
}
// FindAllSiftC find the imgSearch all sift in the imgSource return []Result
// and close gocv.Mat
func FindAllSiftC(imSource, imSearch gocv.Mat, args ...interface{}) []Result {
defer imSource.Close()
defer imSearch.Close()
return FindAllSift(imSource, imSearch, args...)
}
// FindAllTemplate find the imgSearch all template in the imgSource return []Result
func FindAllTemplate(imgSource, imgSearch gocv.Mat, args ...interface{}) []Result {
threshold := float32(0.8)
if len(args) > 0 {
threshold = float32(args[0].(float64))
}
maxCount := 10
if len(args) > 1 {
maxCount = args[1].(int)
}
// rgb := false
// if len(args) > 2 {
// rgb = args[2].(bool)
// }
method := false
if len(args) > 3 {
method = args[3].(bool)
}
iGray := gocv.NewMat()
defer iGray.Close()
sGray := gocv.NewMat()
defer sGray.Close()
// if !rgb {
gocv.CvtColor(imgSource, &iGray, gocv.ColorRGBToGray)
gocv.CvtColor(imgSearch, &sGray, gocv.ColorRGBToGray)
// }
results := make([]Result, 0)
for {
_, maxVal, minLoc, maxLoc := FindImgMat(iGray, sGray)
h, w := imgSearch.Rows(), imgSearch.Cols()
if maxVal < threshold || len(results) > maxCount {
break
}
if method {
maxLoc = minLoc
}
rs := getVal(maxLoc, maxVal, w, h)
results = append(results, rs)
if len(results) <= 0 {
return nil
}
Fill(iGray, rs.Rects)
// Rectangle(iGray, maxLoc, w, h)
// Show(iGray)
}
return results
}
func getVal(maxLoc image.Point, maxVal float32, w, h int) Result {
rect := Rect{
TopLeft: Point{maxLoc.X, maxLoc.Y},
BottomLeft: Point{maxLoc.X, maxLoc.Y + h},
BottomRight: Point{maxLoc.X + w, maxLoc.Y + h},
TopRight: Point{maxLoc.X + w, maxLoc.Y},
}
middle := image.Pt(maxLoc.X+w/2, maxLoc.Y+h/2)
middlePoint := Point{middle.X, middle.Y}
topLeft := Point{maxLoc.X, maxLoc.Y}
maxVals := []float32{maxVal}
size := Size{w, h}
return Result{
Middle: middlePoint,
TopLeft: topLeft, // MaxLoc
Rects: rect,
MaxVal: maxVals,
ImgSize: size,
}
}
// Rectangle rectangle the iGray image
func Rectangle(iGray gocv.Mat, maxLoc image.Point, w, h int) {
r := image.Rect(
int(maxLoc.X-w/2),
int(maxLoc.Y-h/2),
int(maxLoc.X+w/2),
int(maxLoc.Y+h/2),
)
// white := color.RGBA{255, 255, 255, 255}
black := color.RGBA{0, 0, 0, 0}
gocv.Rectangle(&iGray, r, black, -1)
}
// Fill fillpoly the iGray image
func Fill(iGray gocv.Mat, rect Rect) {
pts := [][]image.Point{{
image.Pt(rect.TopLeft.X, rect.TopLeft.Y),
image.Pt(rect.BottomLeft.X, rect.BottomLeft.Y),
image.Pt(rect.BottomRight.X, rect.BottomLeft.Y),
image.Pt(rect.TopRight.X, rect.TopRight.Y),
}}
pts1 := gocv.NewPointsVectorFromPoints(pts)
defer pts1.Close()
blue := color.RGBA{0, 0, 255, 0}
gocv.FillPoly(&iGray, pts1, blue)
}
func findH(kpS, kpSrc []gocv.KeyPoint, goodDiff []gocv.DMatch) (gocv.Mat, gocv.Mat) {
src := gocv.NewMatWithSize(4, 1, gocv.MatTypeCV64FC2)
defer src.Close()
dst := gocv.NewMatWithSize(4, 1, gocv.MatTypeCV64FC2)
// defer dst.Close()
mask := gocv.NewMat()
// defer mask.Close()
// Get the keypoints from the good matches
for i := 0; i < len(goodDiff); i++ {
dst.SetDoubleAt(i, 0, kpS[goodDiff[i].QueryIdx].X)
dst.SetDoubleAt(i, 1, kpS[goodDiff[i].QueryIdx].Y)
src.SetDoubleAt(i, 0, kpSrc[goodDiff[i].TrainIdx].X)
src.SetDoubleAt(i, 1, kpSrc[goodDiff[i].TrainIdx].Y)
}
// find estimate H
hm := gocv.FindHomography(src, &dst, gocv.HomograpyMethodRANSAC, 5.0,
&mask, 2000, 0.95)
return hm, mask
}
func transform(h, w int, hm gocv.Mat) gocv.Mat {
dst := gocv.NewMatWithSize(4, 2, gocv.MatTypeCV32FC2)
// defer dst.Close()
// new mat from the img search
src := gocv.NewMatWithSize(4, 2, gocv.MatTypeCV32FC2)
defer src.Close()
src.SetFloatAt(0, 0, 0)
src.SetFloatAt(0, 1, 0)
src.SetFloatAt(1, 0, 0)
src.SetFloatAt(1, 1, float32(h-1))
src.SetFloatAt(2, 0, float32(w-1))
src.SetFloatAt(2, 1, float32(h-1))
src.SetFloatAt(2, 0, float32(w-1))
src.SetFloatAt(3, 1, 0)
gocv.PerspectiveTransform(src, &dst, hm)
return dst
}
func getPoint(ms [][]gocv.DMatch, kpS, kpSrc []gocv.KeyPoint, ratio float64) []gocv.DMatch {
var good, goodDiff []gocv.DMatch
// Filter matches low distance by ratio
for i := 0; i < len(ms); i++ {
if ms[i][0].Distance < ratio*ms[i][1].Distance {
good = append(good, ms[i][0])
}
}
// Remove the duplicates point
h1 := hset.New()
for i := 0; i < len(good); i++ {
p1 := Point{
X: int(kpSrc[good[i].TrainIdx].X),
Y: int(kpSrc[good[i].TrainIdx].Y),
}
if !h1.Exists(p1) {
h1.Add(p1)
goodDiff = append(goodDiff, good[i])
}
}
return goodDiff
}
// FindAllSift find the imSearch all sift in imSource return result
func FindAllSift(imSource, imSearch gocv.Mat, args ...interface{}) (res []Result) {
sift := gocv.NewSIFT()
defer sift.Close()
//
mask1 := gocv.NewMat()
defer mask1.Close()
mask2 := gocv.NewMat()
defer mask2.Close()
minMatch := 4
ratio := 0.75 // 0.9
// detect the feature and compute descriptor
kpS, des := sift.DetectAndCompute(imSearch, mask1)
kpSrc, deSrc := sift.DetectAndCompute(imSource, mask2)
if len(kpS) < 2 || len(kpSrc) < 2 || len(kpS) < minMatch {
return
}
ms := FlannbasedMatch(des, deSrc, 2)
goodDiff := getPoint(ms, kpS, kpSrc, ratio)
if len(goodDiff) == 0 {
return
}
h, w := GetSize(imSearch)
// get the result value
if len(goodDiff) == 1 {
kpt := kpSrc[goodDiff[0].TrainIdx]
middlePoint := Point{int(kpt.X), int(kpt.Y)}
res = append(res, Result{
Middle: middlePoint,
MaxVal: []float32{0.5},
Rects: Rect{},
ImgSize: Size{w, h},
})
return
}
// if len(goodDiff) > 3 {
hm, mask := findH(kpS, kpSrc, goodDiff)
dst := transform(h, w, hm)
hm.Close()
defer mask.Close()
defer dst.Close()
p1 := Point{int(dst.GetFloatAt(0, 0)) + w, int(dst.GetFloatAt(0, 1))}
p2 := Point{int(dst.GetFloatAt(1, 0)) + w, int(dst.GetFloatAt(1, 1))}
p3 := Point{int(dst.GetFloatAt(2, 0)) + w, int(dst.GetFloatAt(2, 1))}
p4 := Point{int(dst.GetFloatAt(3, 0)) + w, int(dst.GetFloatAt(3, 1))}
res = append(res, Result{
Middle: Point{p1.X + p3.X/2, p1.Y + p3.Y/2},
TopLeft: p1,
Rects: Rect{
TopLeft: p1,
BottomLeft: p2,
BottomRight: p3,
TopRight: p4,
},
MaxVal: []float32{float32(mask.GetDoubleAt(h, w)), float32(len(goodDiff))},
ImgSize: Size{w, h},
})
// }
return
}