-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcommon.go
100 lines (75 loc) · 1.87 KB
/
common.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
// gOCR - Common Module
package main
import (
"image"
"gocv.io/x/gocv"
)
// Show - show image dialog
func Show(img gocv.Mat) {
window := gocv.NewWindow("Display")
for {
window.IMShow(img)
if window.WaitKey(1) >= 0 {
break
}
}
}
// GetImgArray - Convert cvMat to 3-dimension array
func GetImgArray(img gocv.Mat) [][][]uint8 {
height := img.Rows()
width := img.Cols()
channels := img.Channels()
i := 0
flatArr := img.ToBytes()
arr := make([][][]uint8, height)
for row := 0; row < height; row++ {
arr[row] = make([][]uint8, width)
for col := 0; col < width; col++ {
arr[row][col] = make([]uint8, channels)
for ch := 0; ch < channels; ch++ {
arr[row][col][ch] = flatArr[i]
i++
}
}
}
return arr
}
// CropImgArr - Crop image array
func CropImgArr(imgArr [][][]uint8, bound image.Rectangle) [][][]uint8 {
newImg := make([][][]uint8, bound.Max.Y-bound.Min.Y)
for r := range newImg {
newImg[r] = make([][]uint8, bound.Max.X-bound.Min.X)
for c := range newImg[r] {
newImg[r][c] = make([]uint8, 1)
newImg[r][c][0] = imgArr[bound.Min.Y+r][bound.Min.X+c][0]
}
}
return newImg
}
// GetImgMat - Convert cvMat to 3-dimension array
func GetImgMat(arr [][][]uint8) gocv.Mat {
flag := [...]gocv.MatType{gocv.MatTypeCV8UC1, gocv.MatTypeCV8UC2, gocv.MatTypeCV8UC3, gocv.MatTypeCV8UC4}
height := len(arr)
width := len(arr[0])
channels := len(arr[0][0])
i := 0
flatArr := make([]byte, height*width*channels)
for row := 0; row < height; row++ {
for col := 0; col < width; col++ {
for ch := 0; ch < channels; ch++ {
flatArr[i] = arr[row][col][ch]
i++
}
}
}
return gocv.NewMatFromBytes(height, width, flag[channels-1], flatArr)
}
// GetHistArray - Get histrogram array
func GetHistArray(img gocv.Mat) []int {
hist := make([]int, 256)
flatArr := img.ToBytes()
for i := range flatArr {
hist[flatArr[i]]++
}
return hist
}