-
Notifications
You must be signed in to change notification settings - Fork 0
/
mat_option.go
63 lines (49 loc) · 1.13 KB
/
mat_option.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
package easymotion
import (
"bytes"
"encoding/gob"
"github.com/rlaskowski/easymotion/service/opencvservice"
"gocv.io/x/gocv"
)
type MatOption struct {
Rows, Cols int
Kind int
Data []byte
}
func MatToBytes(cam *opencvservice.Camera) ([]byte, error) {
mat, err := cam.ReadMat()
if err != nil {
return nil, err
}
b := mat.ToBytes()
moption := &MatOption{
Rows: mat.Rows(),
Cols: mat.Cols(),
Kind: int(mat.Type()),
Data: b,
}
w := &bytes.Buffer{}
enc := gob.NewEncoder(w)
if err := enc.Encode(moption); err != nil {
return nil, err
}
//buff := *(*[]byte)(unsafe.Pointer(&moption))
/* buff := &bytes.Buffer{}
if err := binary.Write(buff, binary.BigEndian, moption); err != nil {
return nil, err
} */
return w.Bytes(), nil
}
func MatFromBytes(data []byte) (gocv.Mat, error) {
moption := &MatOption{}
r := bytes.NewReader(data)
dec := gob.NewDecoder(r)
if err := dec.Decode(moption); err != nil {
return gocv.Mat{}, err
}
mat, err := gocv.NewMatFromBytes(int(moption.Rows), int(moption.Cols), gocv.MatType(moption.Kind), moption.Data)
if err != nil {
return gocv.Mat{}, err
}
return mat, nil
}