robocar-camera/vendor/gocv.io/x/gocv/videoio.go

333 lines
10 KiB
Go
Raw Normal View History

2019-12-18 23:08:07 +00:00
package gocv
/*
#include <stdlib.h>
#include "videoio.h"
*/
import "C"
import (
"errors"
"fmt"
"strconv"
"sync"
"unsafe"
)
// VideoCaptureProperties are the properties used for VideoCapture operations.
type VideoCaptureProperties int
const (
// VideoCapturePosMsec contains current position of the
// video file in milliseconds.
VideoCapturePosMsec VideoCaptureProperties = 0
// VideoCapturePosFrames 0-based index of the frame to be
// decoded/captured next.
2020-09-06 12:53:53 +00:00
VideoCapturePosFrames VideoCaptureProperties = 1
2019-12-18 23:08:07 +00:00
// VideoCapturePosAVIRatio relative position of the video file:
// 0=start of the film, 1=end of the film.
2020-09-06 12:53:53 +00:00
VideoCapturePosAVIRatio VideoCaptureProperties = 2
2019-12-18 23:08:07 +00:00
// VideoCaptureFrameWidth is width of the frames in the video stream.
2020-09-06 12:53:53 +00:00
VideoCaptureFrameWidth VideoCaptureProperties = 3
2019-12-18 23:08:07 +00:00
// VideoCaptureFrameHeight controls height of frames in the video stream.
2020-09-06 12:53:53 +00:00
VideoCaptureFrameHeight VideoCaptureProperties = 4
2019-12-18 23:08:07 +00:00
// VideoCaptureFPS controls capture frame rate.
2020-09-06 12:53:53 +00:00
VideoCaptureFPS VideoCaptureProperties = 5
2019-12-18 23:08:07 +00:00
// VideoCaptureFOURCC contains the 4-character code of codec.
// see VideoWriter::fourcc for details.
2020-09-06 12:53:53 +00:00
VideoCaptureFOURCC VideoCaptureProperties = 6
2019-12-18 23:08:07 +00:00
// VideoCaptureFrameCount contains number of frames in the video file.
2020-09-06 12:53:53 +00:00
VideoCaptureFrameCount VideoCaptureProperties = 7
2019-12-18 23:08:07 +00:00
// VideoCaptureFormat format of the Mat objects returned by
// VideoCapture::retrieve().
2020-09-06 12:53:53 +00:00
VideoCaptureFormat VideoCaptureProperties = 8
2019-12-18 23:08:07 +00:00
// VideoCaptureMode contains backend-specific value indicating
// the current capture mode.
2020-09-06 12:53:53 +00:00
VideoCaptureMode VideoCaptureProperties = 9
2019-12-18 23:08:07 +00:00
// VideoCaptureBrightness is brightness of the image
// (only for those cameras that support).
2020-09-06 12:53:53 +00:00
VideoCaptureBrightness VideoCaptureProperties = 10
2019-12-18 23:08:07 +00:00
// VideoCaptureContrast is contrast of the image
// (only for cameras that support it).
2020-09-06 12:53:53 +00:00
VideoCaptureContrast VideoCaptureProperties = 11
2019-12-18 23:08:07 +00:00
// VideoCaptureSaturation saturation of the image
// (only for cameras that support).
2020-09-06 12:53:53 +00:00
VideoCaptureSaturation VideoCaptureProperties = 12
2019-12-18 23:08:07 +00:00
// VideoCaptureHue hue of the image (only for cameras that support).
2020-09-06 12:53:53 +00:00
VideoCaptureHue VideoCaptureProperties = 13
2019-12-18 23:08:07 +00:00
// VideoCaptureGain is the gain of the capture image.
// (only for those cameras that support).
2020-09-06 12:53:53 +00:00
VideoCaptureGain VideoCaptureProperties = 14
2019-12-18 23:08:07 +00:00
// VideoCaptureExposure is the exposure of the capture image.
// (only for those cameras that support).
2020-09-06 12:53:53 +00:00
VideoCaptureExposure VideoCaptureProperties = 15
2019-12-18 23:08:07 +00:00
// VideoCaptureConvertRGB is a boolean flags indicating whether
// images should be converted to RGB.
2020-09-06 12:53:53 +00:00
VideoCaptureConvertRGB VideoCaptureProperties = 16
2019-12-18 23:08:07 +00:00
// VideoCaptureWhiteBalanceBlueU is currently unsupported.
2020-09-06 12:53:53 +00:00
VideoCaptureWhiteBalanceBlueU VideoCaptureProperties = 17
2019-12-18 23:08:07 +00:00
// VideoCaptureRectification is the rectification flag for stereo cameras.
// Note: only supported by DC1394 v 2.x backend currently.
2020-09-06 12:53:53 +00:00
VideoCaptureRectification VideoCaptureProperties = 18
2019-12-18 23:08:07 +00:00
// VideoCaptureMonochrome indicates whether images should be
// converted to monochrome.
2020-09-06 12:53:53 +00:00
VideoCaptureMonochrome VideoCaptureProperties = 19
2019-12-18 23:08:07 +00:00
// VideoCaptureSharpness controls image capture sharpness.
2020-09-06 12:53:53 +00:00
VideoCaptureSharpness VideoCaptureProperties = 20
2019-12-18 23:08:07 +00:00
// VideoCaptureAutoExposure controls the DC1394 exposure control
// done by camera, user can adjust reference level using this feature.
2020-09-06 12:53:53 +00:00
VideoCaptureAutoExposure VideoCaptureProperties = 21
2019-12-18 23:08:07 +00:00
// VideoCaptureGamma controls video capture gamma.
2020-09-06 12:53:53 +00:00
VideoCaptureGamma VideoCaptureProperties = 22
2019-12-18 23:08:07 +00:00
// VideoCaptureTemperature controls video capture temperature.
2020-09-06 12:53:53 +00:00
VideoCaptureTemperature VideoCaptureProperties = 23
2019-12-18 23:08:07 +00:00
// VideoCaptureTrigger controls video capture trigger.
2020-09-06 12:53:53 +00:00
VideoCaptureTrigger VideoCaptureProperties = 24
2019-12-18 23:08:07 +00:00
// VideoCaptureTriggerDelay controls video capture trigger delay.
2020-09-06 12:53:53 +00:00
VideoCaptureTriggerDelay VideoCaptureProperties = 25
2019-12-18 23:08:07 +00:00
// VideoCaptureWhiteBalanceRedV controls video capture setting for
// white balance.
2020-09-06 12:53:53 +00:00
VideoCaptureWhiteBalanceRedV VideoCaptureProperties = 26
2019-12-18 23:08:07 +00:00
// VideoCaptureZoom controls video capture zoom.
2020-09-06 12:53:53 +00:00
VideoCaptureZoom VideoCaptureProperties = 27
2019-12-18 23:08:07 +00:00
// VideoCaptureFocus controls video capture focus.
2020-09-06 12:53:53 +00:00
VideoCaptureFocus VideoCaptureProperties = 28
2019-12-18 23:08:07 +00:00
// VideoCaptureGUID controls video capture GUID.
2020-09-06 12:53:53 +00:00
VideoCaptureGUID VideoCaptureProperties = 29
2019-12-18 23:08:07 +00:00
// VideoCaptureISOSpeed controls video capture ISO speed.
2020-09-06 12:53:53 +00:00
VideoCaptureISOSpeed VideoCaptureProperties = 30
2019-12-18 23:08:07 +00:00
// VideoCaptureBacklight controls video capture backlight.
2020-09-06 12:53:53 +00:00
VideoCaptureBacklight VideoCaptureProperties = 32
2019-12-18 23:08:07 +00:00
// VideoCapturePan controls video capture pan.
2020-09-06 12:53:53 +00:00
VideoCapturePan VideoCaptureProperties = 33
2019-12-18 23:08:07 +00:00
// VideoCaptureTilt controls video capture tilt.
2020-09-06 12:53:53 +00:00
VideoCaptureTilt VideoCaptureProperties = 34
2019-12-18 23:08:07 +00:00
// VideoCaptureRoll controls video capture roll.
2020-09-06 12:53:53 +00:00
VideoCaptureRoll VideoCaptureProperties = 35
2019-12-18 23:08:07 +00:00
// VideoCaptureIris controls video capture iris.
2020-09-06 12:53:53 +00:00
VideoCaptureIris VideoCaptureProperties = 36
2019-12-18 23:08:07 +00:00
// VideoCaptureSettings is the pop up video/camera filter dialog. Note:
// only supported by DSHOW backend currently. The property value is ignored.
2020-09-06 12:53:53 +00:00
VideoCaptureSettings VideoCaptureProperties = 37
2019-12-18 23:08:07 +00:00
// VideoCaptureBufferSize controls video capture buffer size.
2020-09-06 12:53:53 +00:00
VideoCaptureBufferSize VideoCaptureProperties = 38
2019-12-18 23:08:07 +00:00
// VideoCaptureAutoFocus controls video capture auto focus..
2020-09-06 12:53:53 +00:00
VideoCaptureAutoFocus VideoCaptureProperties = 39
2019-12-18 23:08:07 +00:00
)
// VideoCapture is a wrapper around the OpenCV VideoCapture class.
//
// For further details, please see:
// http://docs.opencv.org/master/d8/dfe/classcv_1_1VideoCapture.html
//
type VideoCapture struct {
p C.VideoCapture
}
// VideoCaptureFile opens a VideoCapture from a file and prepares
// to start capturing. It returns error if it fails to open the file stored in uri path.
func VideoCaptureFile(uri string) (vc *VideoCapture, err error) {
vc = &VideoCapture{p: C.VideoCapture_New()}
cURI := C.CString(uri)
defer C.free(unsafe.Pointer(cURI))
if !C.VideoCapture_Open(vc.p, cURI) {
err = fmt.Errorf("Error opening file: %s", uri)
}
return
}
// VideoCaptureDevice opens a VideoCapture from a device and prepares
// to start capturing. It returns error if it fails to open the video device.
func VideoCaptureDevice(device int) (vc *VideoCapture, err error) {
vc = &VideoCapture{p: C.VideoCapture_New()}
if !C.VideoCapture_OpenDevice(vc.p, C.int(device)) {
err = fmt.Errorf("Error opening device: %d", device)
}
return
}
// Close VideoCapture object.
func (v *VideoCapture) Close() error {
C.VideoCapture_Close(v.p)
v.p = nil
return nil
}
// Set parameter with property (=key).
func (v *VideoCapture) Set(prop VideoCaptureProperties, param float64) {
C.VideoCapture_Set(v.p, C.int(prop), C.double(param))
}
// Get parameter with property (=key).
func (v VideoCapture) Get(prop VideoCaptureProperties) float64 {
return float64(C.VideoCapture_Get(v.p, C.int(prop)))
}
// IsOpened returns if the VideoCapture has been opened to read from
// a file or capture device.
func (v *VideoCapture) IsOpened() bool {
isOpened := C.VideoCapture_IsOpened(v.p)
return isOpened != 0
}
// Read reads the next frame from the VideoCapture to the Mat passed in
// as the param. It returns false if the VideoCapture cannot read frame.
func (v *VideoCapture) Read(m *Mat) bool {
return C.VideoCapture_Read(v.p, m.p) != 0
}
// Grab skips a specific number of frames.
func (v *VideoCapture) Grab(skip int) {
C.VideoCapture_Grab(v.p, C.int(skip))
}
// CodecString returns a string representation of FourCC bytes, i.e. the name of a codec
func (v *VideoCapture) CodecString() string {
res := ""
hexes := []int64{0xff, 0xff00, 0xff0000, 0xff000000}
for i, h := range hexes {
res += string(int64(v.Get(VideoCaptureFOURCC)) & h >> (uint(i * 8)))
}
return res
}
// ToCodec returns an float64 representation of FourCC bytes
func (v *VideoCapture) ToCodec(codec string) float64 {
if len(codec) != 4 {
return -1.0
}
c1 := []rune(string(codec[0]))[0]
c2 := []rune(string(codec[1]))[0]
c3 := []rune(string(codec[2]))[0]
c4 := []rune(string(codec[3]))[0]
return float64((c1 & 255) + ((c2 & 255) << 8) + ((c3 & 255) << 16) + ((c4 & 255) << 24))
}
// VideoWriter is a wrapper around the OpenCV VideoWriter`class.
//
// For further details, please see:
// http://docs.opencv.org/master/dd/d9e/classcv_1_1VideoWriter.html
//
type VideoWriter struct {
mu *sync.RWMutex
p C.VideoWriter
}
// VideoWriterFile opens a VideoWriter with a specific output file.
// The "codec" param should be the four-letter code for the desired output
// codec, for example "MJPG".
//
// For further details, please see:
// http://docs.opencv.org/master/dd/d9e/classcv_1_1VideoWriter.html#a0901c353cd5ea05bba455317dab81130
//
func VideoWriterFile(name string, codec string, fps float64, width int, height int, isColor bool) (vw *VideoWriter, err error) {
if fps == 0 || width == 0 || height == 0 {
return nil, fmt.Errorf("one of the numerical parameters "+
"is equal to zero: FPS: %f, width: %d, height: %d", fps, width, height)
}
vw = &VideoWriter{
p: C.VideoWriter_New(),
mu: &sync.RWMutex{},
}
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
cCodec := C.CString(codec)
defer C.free(unsafe.Pointer(cCodec))
C.VideoWriter_Open(vw.p, cName, cCodec, C.double(fps), C.int(width), C.int(height), C.bool(isColor))
return
}
// Close VideoWriter object.
func (vw *VideoWriter) Close() error {
C.VideoWriter_Close(vw.p)
vw.p = nil
return nil
}
// IsOpened checks if the VideoWriter is open and ready to be written to.
//
// For further details, please see:
// http://docs.opencv.org/master/dd/d9e/classcv_1_1VideoWriter.html#a9a40803e5f671968ac9efa877c984d75
//
func (vw *VideoWriter) IsOpened() bool {
isOpend := C.VideoWriter_IsOpened(vw.p)
return isOpend != 0
}
// Write the next video frame from the Mat image to the open VideoWriter.
//
// For further details, please see:
// http://docs.opencv.org/master/dd/d9e/classcv_1_1VideoWriter.html#a3115b679d612a6a0b5864a0c88ed4b39
//
func (vw *VideoWriter) Write(img Mat) error {
vw.mu.Lock()
defer vw.mu.Unlock()
C.VideoWriter_Write(vw.p, img.p)
return nil
}
// OpenVideoCapture return VideoCapture specified by device ID if v is a
// number. Return VideoCapture created from video file, URL, or GStreamer
// pipeline if v is a string.
func OpenVideoCapture(v interface{}) (*VideoCapture, error) {
switch vv := v.(type) {
case int:
return VideoCaptureDevice(vv)
case string:
id, err := strconv.Atoi(vv)
if err == nil {
return VideoCaptureDevice(id)
}
return VideoCaptureFile(vv)
default:
return nil, errors.New("argument must be int or string")
}
}