forked from go-opencv/go-opencv
/
thresholds.go
128 lines (110 loc) · 3.35 KB
/
thresholds.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
//
// thresholds.go
//
// Sample showing the different types of thresholding available.
// It does both fixed and adaptive thresholding.
//
// kevinabrandon@gmail.com - 9-14-2016
//
package main
import (
"fmt"
"github.com/lazywei/go-opencv/opencv"
"os"
)
func main() {
filename := "../images/lena.jpg"
if len(os.Args) == 2 {
filename = os.Args[1]
}
fmt.Println("Press ESC or 'q' to quit")
img := opencv.LoadImage(filename, opencv.CV_LOAD_IMAGE_GRAYSCALE)
if img == nil {
fmt.Printf("LoadImage fail: %s\n", filename)
return
}
defer img.Release()
win := opencv.NewWindow("Threshold Sample")
defer win.Destroy()
win.CreateTrackbar("ThresholdType", 0, 4, func(pos int) { ThresholdImage(img, win) })
win.CreateTrackbar("Threshold", 128, 255, func(pos int) { ThresholdImage(img, win) })
win.CreateTrackbar("MaxValue", 255, 255, func(pos int) { ThresholdImage(img, win) })
win.CreateTrackbar("Fixed/Adaptive", 0, 1, func(pos int) { ThresholdImage(img, win) })
win.CreateTrackbar("AdaptiveMethod", 0, 1, func(pos int) { ThresholdImage(img, win) })
win.CreateTrackbar("AdaptiveBlockSize", 3, 100, func(pos int) { ThresholdImage(img, win) })
ThresholdImage(img, win)
for {
key := opencv.WaitKey(10)
if key == 1048603 || key == 1048689 {
// esc or 'q' key pressed
break
}
}
}
var (
threshTypes = []string{
"Binary", // opencv.CV_THRESH_BINARY
"Binary Inverse", // opencv.CV_THRESH_BINARY_INV
"Truncated", // opencv.CV_THRESH_TRUNC
"To Zero", // opencv.CV_THRESH_TOZERO
"To Zero Inverse", // opencv.CV_THRESH_TOZERO_INV
}
adaptiveMethods = []string{
"Mean", // opencv.CV_ADAPTIVE_THRESH_MEAN_C
"Gaussian", // opencv.CV_ADAPTIVE_THRESH_GAUSSIAN_C
}
)
func ThresholdImage(img *opencv.IplImage, win *opencv.Window) {
threshImg := opencv.CreateImage(img.Width(), img.Height(), opencv.IPL_DEPTH_8U, 1)
defer threshImg.Release()
threshType, _ := win.GetTrackbarPos("ThresholdType")
thresh, _ := win.GetTrackbarPos("Threshold")
maxVal, _ := win.GetTrackbarPos("MaxValue")
fixedOrAdaptive, _ := win.GetTrackbarPos("Fixed/Adaptive")
adaptMethd, _ := win.GetTrackbarPos("AdaptiveMethod")
blockSize, _ := win.GetTrackbarPos("AdaptiveBlockSize")
if fixedOrAdaptive == 0 {
// fixed threshold:
fmt.Println("*************************")
fmt.Printf("Fixed threshold: %d\n", thresh)
fmt.Printf("maxValue: %d\n", maxVal)
fmt.Printf("thresType: '%s'\n", threshTypes[threshType])
opencv.Threshold(
img,
threshImg,
float64(thresh),
float64(maxVal),
threshType,
)
} else {
// check to make sure we have a valid threshold type. (Binary or Binary inverse only)
if threshType > 1 {
fmt.Println("*** Only threshold types of Binary or Binary Inverse are allowed for adaptive thresholds!")
return
}
// blockSize must be odd and >= 3
if blockSize < 3 {
blockSize = 3
}
if blockSize%2 == 0 {
blockSize++
}
fmt.Println("*************************")
fmt.Printf("Adaptive threshold: %d\n", thresh)
fmt.Printf("blockSize: %d\n", blockSize)
fmt.Printf("maxValue: %d\n", maxVal)
fmt.Printf("threshType: '%s'\n", threshTypes[threshType])
fmt.Printf("adaptiveMethod: '%s'\n", adaptiveMethods[adaptMethd])
// adaptive threshold:
opencv.AdaptiveThreshold(
img,
threshImg,
float64(maxVal),
adaptMethd,
threshType,
blockSize,
float64(thresh),
)
}
win.ShowImage(threshImg)
}