-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathframe_data.go
More file actions
53 lines (46 loc) · 1.13 KB
/
Copy pathframe_data.go
File metadata and controls
53 lines (46 loc) · 1.13 KB
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
package odam
import (
"bytes"
"image"
"image/jpeg"
"gocv.io/x/gocv"
)
// FrameData Wrapper around gocv.Mat
type FrameData struct {
ImgSource gocv.Mat // Source image
ImgScaled gocv.Mat // Scaled image
ImgScaledCopy gocv.Mat // Copy of scaled image
}
// NewFrameData Simplifies creation of FrameData
func NewFrameData() *FrameData {
fd := FrameData{
ImgSource: gocv.NewMat(),
ImgScaled: gocv.NewMat(),
}
return &fd
}
// Close Simplify memory management for each gocv.Mat of FrameData
func (fd *FrameData) Close() {
fd.ImgSource.Close()
fd.ImgScaled.Close()
fd.ImgScaledCopy.Close()
}
// Preprocess Scales image to given width and height
func (fd *FrameData) Preprocess(width, height int) error {
gocv.Resize(fd.ImgSource, &fd.ImgScaled, image.Point{X: width, Y: height}, 0, 0, gocv.InterpolationDefault)
fd.ImgScaledCopy = fd.ImgScaled.Clone()
return nil
}
func matToBytes(im *gocv.Mat) (ans []byte, err error) {
stdImage, err := im.ToImage()
if err != nil {
return ans, err
}
buf := new(bytes.Buffer)
err = jpeg.Encode(buf, stdImage, nil)
if err != nil {
return ans, err
}
ans = buf.Bytes()
return ans, nil
}