Add rotate option

This commit is contained in:
Milan Nikolic
2018-10-10 04:32:00 +02:00
parent 37b19fcfe8
commit c9d77f03e7
8 changed files with 141 additions and 20 deletions

View File

@@ -7,22 +7,31 @@ import (
"fmt"
"image"
"github.com/disintegration/imaging"
"github.com/lazywei/go-opencv/opencv"
)
// Options.
type Options struct {
Filename string
Rotate int
}
// Video represents video.
type Video struct {
opts Options
video *opencv.Capture
frame *opencv.IplImage
}
// New returns new Video for given path.
func New(filename string) (video *Video, err error) {
func New(opts Options) (video *Video, err error) {
video = &Video{}
video.opts = opts
video.video = opencv.NewFileCapture(filename)
video.video = opencv.NewFileCapture(opts.Filename)
if video.video == nil {
err = fmt.Errorf("video: can not open video %s", filename)
err = fmt.Errorf("video: can not open video %s", opts.Filename)
}
return
@@ -32,7 +41,24 @@ func New(filename string) (video *Video, err error) {
func (v *Video) Read() (img image.Image, err error) {
if v.video.GrabFrame() {
v.frame = v.video.RetrieveFrame(1)
if v.frame == nil {
err = fmt.Errorf("video: can not grab frame")
return
}
img = v.frame.ToImage()
if v.opts.Rotate == 0 {
return
}
switch v.opts.Rotate {
case 90:
img = imaging.Rotate90(img)
case 180:
img = imaging.Rotate180(img)
case 270:
img = imaging.Rotate270(img)
}
} else {
err = fmt.Errorf("video: can not grab frame")
}

View File

@@ -7,25 +7,34 @@ import (
"fmt"
"image"
"github.com/disintegration/imaging"
"gocv.io/x/gocv"
)
// Options.
type Options struct {
Filename string
Rotate int
}
// Video represents video.
type Video struct {
opts Options
video *gocv.VideoCapture
frame *gocv.Mat
}
// New returns new Video for given path.
func New(filename string) (video *Video, err error) {
func New(opts Options) (video *Video, err error) {
video = &Video{}
video.opts = opts
mat := gocv.NewMat()
video.frame = &mat
video.video, err = gocv.VideoCaptureFile(filename)
video.video, err = gocv.VideoCaptureFile(opts.Filename)
if err != nil {
err = fmt.Errorf("video: can not open video %s: %s", filename, err.Error())
err = fmt.Errorf("video: can not open video %s: %s", opts.Filename, err.Error())
}
return
@@ -39,12 +48,30 @@ func (v *Video) Read() (img image.Image, err error) {
return
}
if v.frame == nil {
err = fmt.Errorf("video: can not grab frame")
return
}
img, e := v.frame.ToImage()
if e != nil {
err = fmt.Errorf("video: %v", e)
return
}
if v.opts.Rotate == 0 {
return
}
switch v.opts.Rotate {
case 90:
img = imaging.Rotate90(img)
case 180:
img = imaging.Rotate180(img)
case 270:
img = imaging.Rotate270(img)
}
return
}