Add support for OpenCV 3

This commit is contained in:
Milan Nikolic
2018-03-14 13:45:38 +01:00
parent 8a740337ab
commit 3c0c949f31
6 changed files with 181 additions and 45 deletions

View File

@@ -1,3 +1,5 @@
// +build !cv3
// Package camera.
package camera
@@ -8,49 +10,6 @@ import (
"github.com/lazywei/go-opencv/opencv"
)
// Property identifiers.
const (
PropPosMsec = iota
PropPosFrames
PropPosAviRatio
PropFrameWidth
PropFrameHeight
PropFps
PropFourcc
PropFrameCount
PropFormat
PropMode
PropBrightness
PropContrast
PropSaturation
PropHue
PropGain
PropExposure
PropConvertRgb
PropWhiteBalanceU
PropRectification
PropMonocrome
PropSharpness
PropAutoExposure
PropGamma
PropTemperature
PropTrigger
PropTriggerDelay
PropWhiteBalanceV
PropZoom
PropFocus
PropGuid
PropIsoSpeed
PropMaxDc1394
PropBacklight
PropPan
PropTilt
PropRoll
PropIris
PropSettings
PropBuffersize
)
// Camera represents camera.
type Camera struct {
camera *opencv.Capture
@@ -86,8 +45,8 @@ func (c *Camera) GetProperty(id int) float64 {
}
// SetProperty sets a camera property.
func (c *Camera) SetProperty(id int, value float64) int {
return c.camera.SetProperty(id, value)
func (c *Camera) SetProperty(id int, value float64) {
c.camera.SetProperty(id, value)
}
// Close closes camera.

44
camera/camera_const.go Normal file
View File

@@ -0,0 +1,44 @@
package camera
// Property identifiers.
const (
PropPosMsec = iota
PropPosFrames
PropPosAviRatio
PropFrameWidth
PropFrameHeight
PropFps
PropFourcc
PropFrameCount
PropFormat
PropMode
PropBrightness
PropContrast
PropSaturation
PropHue
PropGain
PropExposure
PropConvertRgb
PropWhiteBalanceU
PropRectification
PropMonocrome
PropSharpness
PropAutoExposure
PropGamma
PropTemperature
PropTrigger
PropTriggerDelay
PropWhiteBalanceV
PropZoom
PropFocus
PropGuid
PropIsoSpeed
PropMaxDc1394
PropBacklight
PropPan
PropTilt
PropRoll
PropIris
PropSettings
PropBuffersize
)

70
camera/camera_cv3.go Normal file
View File

@@ -0,0 +1,70 @@
// +build cv3
// Package camera.
package camera
import (
"fmt"
"image"
"gocv.io/x/gocv"
)
// Camera represents camera.
type Camera struct {
camera *gocv.VideoCapture
}
// New returns new Camera for given camera index.
func New(index int) (camera *Camera, err error) {
camera = &Camera{}
camera.camera, err = gocv.VideoCaptureDevice(index)
if err != nil {
err = fmt.Errorf("camera: can not open camera %d: %s", index, err.Error())
}
return
}
// Read reads next frame from camera and returns image.
func (c *Camera) Read() (img image.Image, err error) {
mat := gocv.NewMat()
defer mat.Close()
ok := c.camera.Read(mat)
if !ok {
err = fmt.Errorf("camera: can not grab frame")
return
}
img, e := mat.ToImage()
if e != nil {
err = fmt.Errorf("camera: %v", e)
return
}
return
}
// GetProperty returns the specified camera property.
func (c *Camera) GetProperty(id int) float64 {
return c.camera.Get(gocv.VideoCaptureProperties(id))
}
// SetProperty sets a camera property.
func (c *Camera) SetProperty(id int, value float64) {
c.camera.Set(gocv.VideoCaptureProperties(id), value)
}
// Close closes camera.
func (c *Camera) Close() (err error) {
if c.camera == nil {
err = fmt.Errorf("camera: camera is not opened")
return
}
err = c.camera.Close()
c.camera = nil
return
}

View File

@@ -11,6 +11,7 @@ LIBRARY_PATH="$CHROOT/usr/lib:$CHROOT/lib" \
PKG_CONFIG_PATH="$CHROOT/usr/lib/pkgconfig" \
PKG_CONFIG_LIBDIR="$CHROOT/usr/lib/pkgconfig" \
CGO_LDFLAGS="-L$CHROOT/usr/lib -L$CHROOT/lib" \
CGO_CFLAGS="-I$CHROOT/usr/include" \
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -v -x -o build/cam2ip.linux.amd64 -ldflags "-linkmode external -s -w" github.com/gen2brain/cam2ip

View File

@@ -1,3 +1,5 @@
// +build !cv3
// Package video.
package video

60
video/video_cv3.go Normal file
View File

@@ -0,0 +1,60 @@
// +build cv3
// Package video.
package video
import (
"fmt"
"image"
"gocv.io/x/gocv"
)
// Video represents video.
type Video struct {
video *gocv.VideoCapture
}
// New returns new Video for given path.
func New(filename string) (video *Video, err error) {
video = &Video{}
video.video, err = gocv.VideoCaptureFile(filename)
if err != nil {
err = fmt.Errorf("video: can not open video %s: %s", filename, err.Error())
}
return
}
// 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)
if !ok {
err = fmt.Errorf("video: can not grab frame")
return
}
img, e := mat.ToImage()
if e != nil {
err = fmt.Errorf("video: %v", e)
return
}
return
}
// Close closes video.
func (v *Video) Close() (err error) {
if v.video == nil {
err = fmt.Errorf("video: video is not opened")
return
}
err = v.video.Close()
v.video = nil
return
}