diff --git a/examples/saveimage.go b/examples/saveimage.go index 7bccf829..0072707c 100644 --- a/examples/saveimage.go +++ b/examples/saveimage.go @@ -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 @@ -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) } @@ -39,5 +49,5 @@ func main() { return } - opencv3.IMWrite(os.Args[1], img) + opencv3.IMWrite(saveFile, img) } diff --git a/examples/savevideo.go b/examples/savevideo.go new file mode 100644 index 00000000..e7cde8c0 --- /dev/null +++ b/examples/savevideo.go @@ -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) + } +} diff --git a/videoio.cpp b/videoio.cpp index e6573dde..c585835e 100644 --- a/videoio.cpp +++ b/videoio.cpp @@ -54,7 +54,7 @@ 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); } @@ -62,6 +62,6 @@ int VideoWriter_IsOpened(VideoWriter vw) { return vw->isOpened(); } -void VideoWriter_Write(VideoWriter vw, MatVec3b img) { +void VideoWriter_Write(VideoWriter vw, Mat img) { *vw << *img; } diff --git a/videoio.go b/videoio.go index bb4ccc91..6c60d64d 100644 --- a/videoio.go +++ b/videoio.go @@ -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) diff --git a/videoio.h b/videoio.h index e20ee83a..be28293e 100644 --- a/videoio.h +++ b/videoio.h @@ -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 }