Skip to content

Commit

Permalink
VideoWriter example and code working as expected, tested with MP4 and…
Browse files Browse the repository at this point in the history
… AVI files

Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Sep 25, 2017
1 parent edc917a commit 17d4745
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 14 deletions.
18 changes: 14 additions & 4 deletions examples/saveimage.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// What it does:
//
// This example uses the VideoCapture class to capture a frame from a connected webcam,
// then save it to a file on disk
// then save it to an image file on disk
//
// How to run:
//
// saveimage [camera ID] [image file]
//
// go run ./examples/saveimage.go filename.jpg
//
// +build example
Expand All @@ -14,16 +16,24 @@ package main
import (
"fmt"
"os"
"strconv"

opencv3 ".."
)

func main() {
deviceID := 0
if len(os.Args) < 3 {
fmt.Println("How to run:\n\tsaveimage [camera ID] [image file]")
return
}

deviceID, _ := strconv.Atoi(os.Args[1])
saveFile := os.Args[2]

webcam := opencv3.NewVideoCapture()
defer webcam.Delete()

if ok := webcam.OpenDevice(int(deviceID)); !ok {
if ok := webcam.OpenDevice(deviceID); !ok {
fmt.Printf("error opening device: %v\n", deviceID)
}

Expand All @@ -39,5 +49,5 @@ func main() {
return
}

opencv3.IMWrite(os.Args[1], img)
opencv3.IMWrite(saveFile, img)
}
64 changes: 64 additions & 0 deletions examples/savevideo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// What it does:
//
// This example uses the VideoCapture class to capture video from a connected webcam,
// and save it to a video file on disk
//
// How to run:
//
// savevideo [camera ID] [video file]
//
// go run ./examples/savevideo.go 0 testvideo.mp4
//
// +build example

package main

import (
"fmt"
"os"
"strconv"

opencv3 ".."
)

func main() {
if len(os.Args) < 3 {
fmt.Println("How to run:\n\tsavevideo [camera ID] [video file]")
return
}

deviceID, _ := strconv.Atoi(os.Args[1])
saveFile := os.Args[2]

webcam := opencv3.NewVideoCapture()
defer webcam.Delete()

writer := opencv3.NewVideoWriter()
defer writer.Delete()

if ok := webcam.OpenDevice(deviceID); !ok {
fmt.Printf("error opening device: %v\n", deviceID)
}

img := opencv3.NewMat()
defer img.Delete()

if ok := webcam.Read(img); !ok {
fmt.Printf("cannot read device %d\n", deviceID)
return
}

writer.OpenWithMat(saveFile, 25, img)

for i := 0; i < 100; i++ {
if ok := webcam.Read(img); !ok {
fmt.Printf("cannot read device %d\n", deviceID)
return
}
if img.Empty() {
continue
}

writer.Write(img)
}
}
4 changes: 2 additions & 2 deletions videoio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ void VideoWriter_Open(VideoWriter vw, const char* name, double fps, int width,
}

void VideoWriter_OpenWithMat(VideoWriter vw, const char* name, double fps,
MatVec3b img) {
Mat img) {
vw->open(name, CV_FOURCC('M', 'J', 'P', 'G'), fps, img->size(), true);
}

int VideoWriter_IsOpened(VideoWriter vw) {
return vw->isOpened();
}

void VideoWriter_Write(VideoWriter vw, MatVec3b img) {
void VideoWriter_Write(VideoWriter vw, Mat img) {
*vw << *img;
}
13 changes: 7 additions & 6 deletions videoio.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,28 +82,29 @@ func (vw *VideoWriter) Delete() {
vw.p = nil
}

// Open a video writer.
// Open a VideoWriter with a specific output file.
func (vw *VideoWriter) Open(name string, fps float64, width int, height int) {
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
C.VideoWriter_Open(vw.p, cName, C.double(fps), C.int(width), C.int(height))
}

// OpenWithMat opens video writer.
func (vw *VideoWriter) OpenWithMat(name string, fps float64, img MatVec3b) {
// OpenWithMat opens a VideoWriter with a specific output file
// using the dimensions from a specific Mat.
func (vw *VideoWriter) OpenWithMat(name string, fps float64, img Mat) {
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
C.VideoWriter_OpenWithMat(vw.p, cName, C.double(fps), img.p)
}

// IsOpened returns the video writer opens a file or not.
// IsOpened checks if the VideoWriter is open and ready to be written to.
func (vw *VideoWriter) IsOpened() bool {
isOpend := C.VideoWriter_IsOpened(vw.p)
return isOpend != 0
}

// Write the image to file.
func (vw *VideoWriter) Write(img MatVec3b) {
// Write a single Mat image to the open VideoWriter.
func (vw *VideoWriter) Write(img Mat) {
vw.mu.Lock()
defer vw.mu.Unlock()
C.VideoWriter_Write(vw.p, img.p)
Expand Down
4 changes: 2 additions & 2 deletions videoio.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ void VideoWriter_Delete(VideoWriter vw);
void VideoWriter_Open(VideoWriter vw, const char* name, double fps, int width,
int height);
void VideoWriter_OpenWithMat(VideoWriter vw, const char* name, double fps,
MatVec3b img);
Mat img);
int VideoWriter_IsOpened(VideoWriter vw);
void VideoWriter_Write(VideoWriter vw, MatVec3b img);
void VideoWriter_Write(VideoWriter vw, Mat img);

#ifdef __cplusplus
}
Expand Down

0 comments on commit 17d4745

Please sign in to comment.