-
Notifications
You must be signed in to change notification settings - Fork 870
/
main.go
97 lines (80 loc) · 1.83 KB
/
main.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
// What it does:
//
// This example uses Brute-Force Matching
// with SIFT Descriptors and Ratio Test
//
// How to run:
//
// feature-matching [query image] [training image]
//
// go run ./cmd/feature-matching/main.go input.jpg training.jpg
//
package main
import (
"fmt"
"image/color"
"os"
"gocv.io/x/gocv"
)
func main() {
if len(os.Args) != 3 {
fmt.Println("Usage: feature-matching /path/to/query /path/to/train")
panic("error: no files provided")
}
// opening query image
query := gocv.IMRead(os.Args[1], gocv.IMReadGrayScale)
defer query.Close()
// opening train image
train := gocv.IMRead(os.Args[2], gocv.IMReadGrayScale)
defer train.Close()
// creating new SIFT
sift := gocv.NewSIFT()
defer sift.Close()
// detecting and computing keypoints using SIFT method
queryMask := gocv.NewMat()
defer queryMask.Close()
kp1, des1 := sift.DetectAndCompute(query, queryMask)
defer des1.Close()
trainMask := gocv.NewMat()
defer trainMask.Close()
kp2, des2 := sift.DetectAndCompute(train, trainMask)
defer des2.Close()
// finding K best matches for each descriptor
bf := gocv.NewBFMatcher()
matches := bf.KnnMatch(des1, des2, 2)
// application of ratio test
var good []gocv.DMatch
for _, m := range matches {
if len(m) > 1 {
if m[0].Distance < 0.75*m[1].Distance {
good = append(good, m[0])
}
}
}
// matches color
c1 := color.RGBA{
R: 0,
G: 255,
B: 0,
A: 0,
}
// point color
c2 := color.RGBA{
R: 255,
G: 0,
B: 0,
A: 0,
}
// creating empty mask
mask := make([]byte, 0)
// new matrix for output image
out := gocv.NewMat()
defer out.Close()
// drawing matches
gocv.DrawMatches(query, kp1, train, kp2, good, &out, c1, c2, mask, gocv.DrawDefault)
// creating output window with result
window := gocv.NewWindow("Output")
window.IMShow(out)
defer window.Close()
window.WaitKey(0)
}