Reuse frame

This commit is contained in:
Milan Nikolic
2018-03-14 14:33:01 +01:00
parent 3c0c949f31
commit e1f03b55a1
4 changed files with 22 additions and 14 deletions

View File

@@ -13,6 +13,7 @@ import (
// Video represents video.
type Video struct {
video *opencv.Capture
frame *opencv.IplImage
}
// New returns new Video for given path.
@@ -30,8 +31,8 @@ func New(filename string) (video *Video, err error) {
// Read reads next frame from video and returns image.
func (v *Video) Read() (img image.Image, err error) {
if v.video.GrabFrame() {
frame := v.video.RetrieveFrame(1)
img = frame.ToImage()
v.frame = v.video.RetrieveFrame(1)
img = v.frame.ToImage()
} else {
err = fmt.Errorf("video: can not grab frame")
}
@@ -46,6 +47,7 @@ func (v *Video) Close() (err error) {
return
}
v.frame.Release()
v.video.Release()
v.video = nil
return

View File

@@ -13,12 +13,16 @@ import (
// Video represents video.
type Video struct {
video *gocv.VideoCapture
frame *gocv.Mat
}
// New returns new Video for given path.
func New(filename string) (video *Video, err error) {
video = &Video{}
mat := gocv.NewMat()
video.frame = &mat
video.video, err = gocv.VideoCaptureFile(filename)
if err != nil {
err = fmt.Errorf("video: can not open video %s: %s", filename, err.Error())
@@ -29,16 +33,13 @@ func New(filename string) (video *Video, err error) {
// Read reads next frame from video and returns image.
func (v *Video) Read() (img image.Image, err error) {
mat := gocv.NewMat()
defer mat.Close()
ok := v.video.Read(mat)
ok := v.video.Read(*v.frame)
if !ok {
err = fmt.Errorf("video: can not grab frame")
return
}
img, e := mat.ToImage()
img, e := v.frame.ToImage()
if e != nil {
err = fmt.Errorf("video: %v", e)
return
@@ -54,6 +55,7 @@ func (v *Video) Close() (err error) {
return
}
v.frame.Close()
err = v.video.Close()
v.video = nil
return