forked from go-opencv/go-opencv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgocv_core.go
85 lines (67 loc) · 2.07 KB
/
gocv_core.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
package gocv
// #cgo CXXFLAGS: -std=c++11
// #cgo darwin pkg-config: opencv
// #cgo linux pkg-config: opencv
import "C"
import "github.com/gonum/matrix/mat64"
func NewGcvPoint3f32(pts ...float64) GcvPoint3f32_ {
// This make sure we have default values
safePts := getSafePts(pts, 3)
return NewGcvPoint3f32_(float32(safePts[0]), float32(safePts[1]), float32(safePts[2]))
}
func NewGcvPoint3f64(pts ...float64) GcvPoint3f64_ {
safePts := getSafePts(pts, 3)
return NewGcvPoint3f64_(safePts[0], safePts[1], safePts[2])
}
func NewGcvPoint2f32(pts ...float64) GcvPoint2f32_ {
safePts := getSafePts(pts, 2)
return NewGcvPoint2f32_(float32(safePts[0]), float32(safePts[1]))
}
func NewGcvPoint2f64(pts ...float64) GcvPoint2f64_ {
safePts := getSafePts(pts, 2)
return NewGcvPoint2f64_(safePts[0], safePts[1])
}
func NewGcvSize2f32(pts ...float64) GcvSize2f32_ {
safePts := getSafePts(pts, 2)
return NewGcvSize2f32_(float32(safePts[0]), float32(safePts[1]))
}
func NewGcvSize2f64(pts ...float64) GcvSize2f64_ {
safePts := getSafePts(pts, 2)
return NewGcvSize2f64_(safePts[0], safePts[1])
}
// Convert Mat, which defined by SWIG, to *mat64.Dense.
// The reason is the latter is much easier to handle
// in Go.
// GcvMat is assumed to be 2-dimensional matrix.
func GcvMatToMat64(mat GcvMat) *mat64.Dense {
col := mat.GetCols()
row := mat.GetRows()
data := []float64{}
for i := 0; i < row; i++ {
for j := 0; j < col; j++ {
if fltPtr, ok := mat.GcvAtf64(i, j).(*float64); ok {
data = append(data, *fltPtr)
} else {
panic("Non *float64 passed to MatToMat64")
}
}
}
return mat64.NewDense(row, col, data)
}
// Convert *mat64.Dense to Mat
func Mat64ToGcvMat(mat *mat64.Dense) GcvMat {
row, col := mat.Dims()
rawData := NewGcvFloat64Vector(int64(row * col))
for i := 0; i < row; i++ {
for j := 0; j < col; j++ {
rawData.Set(i*col+j, mat.At(i, j))
}
}
return Mat64ToGcvMat_(row, col, rawData)
}
func getSafePts(pts []float64, size int) []float64 {
// This make sure we have default values
safePts := make([]float64, size, size)
copy(safePts, pts)
return safePts
}