-
Notifications
You must be signed in to change notification settings - Fork 871
/
main.go
80 lines (63 loc) · 1.39 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
// What it does:
//
// This example shows how to find circles in an image using Hough transform.
//
// How to run:
//
// go run ./cmd/find-circles/main.go ./images/circles.jpg
//
package main
import (
"fmt"
"image"
"image/color"
"os"
"gocv.io/x/gocv"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("How to run:\n\tfind-circles [imgfile]")
return
}
filename := os.Args[1]
window := gocv.NewWindow("detected circles")
defer window.Close()
img := gocv.IMRead(filename, gocv.IMReadGrayScale)
defer img.Close()
gocv.MedianBlur(img, &img, 5)
cimg := gocv.NewMat()
defer cimg.Close()
gocv.CvtColor(img, &cimg, gocv.ColorGrayToBGR)
circles := gocv.NewMat()
defer circles.Close()
gocv.HoughCirclesWithParams(
img,
&circles,
gocv.HoughGradient,
1, // dp
float64(img.Rows()/8), // minDist
75, // param1
20, // param2
10, // minRadius
0, // maxRadius
)
blue := color.RGBA{0, 0, 255, 0}
red := color.RGBA{255, 0, 0, 0}
for i := 0; i < circles.Cols(); i++ {
v := circles.GetVecfAt(0, i)
// if circles are found
if len(v) > 2 {
x := int(v[0])
y := int(v[1])
r := int(v[2])
gocv.Circle(&cimg, image.Pt(x, y), r, blue, 2)
gocv.Circle(&cimg, image.Pt(x, y), 2, red, 3)
}
}
for {
window.IMShow(cimg)
if window.WaitKey(10) >= 0 {
break
}
}
}