diff --git a/core.cpp b/core.cpp index 9f9d8f9b..2215ac61 100644 --- a/core.cpp +++ b/core.cpp @@ -10,6 +10,10 @@ void Mat_Delete(Mat m) { delete m; } +int Mat_Empty(Mat m) { + return m->empty(); +} + // MatVec3b MatVec3b MatVec3b_New() { return new cv::Mat_(); diff --git a/core.go b/core.go index 3cc30add..b1a91ef0 100644 --- a/core.go +++ b/core.go @@ -37,6 +37,12 @@ func (m *Mat) Delete() { m.p = nil } +// Empty determines if the Mat is empty or not. +func (m *Mat) Empty() bool { + isEmpty := C.Mat_Empty(m.p) + return isEmpty != 0 +} + // CMatVec3b is an alias for C pointer. type CMatVec3b C.MatVec3b diff --git a/core.h b/core.h index efe4ed26..f1b2f329 100644 --- a/core.h +++ b/core.h @@ -41,6 +41,7 @@ typedef struct RawData { Mat Mat_New(); void Mat_Delete(Mat m); + int Mat_Empty(Mat m); MatVec3b MatVec3b_New(); struct ByteArray MatVec3b_ToJpegData(MatVec3b m, int quality); diff --git a/examples/capture.go b/examples/capture.go index 1e920385..54247fcf 100644 --- a/examples/capture.go +++ b/examples/capture.go @@ -16,7 +16,7 @@ func main() { } // streaming, capture from webcam - buf := opencv3.NewMatVec3b() + buf := opencv3.NewMat() defer buf.Delete() fmt.Printf("start reading camera device: %v\n", deviceID) for { diff --git a/examples/capwindow.go b/examples/capwindow.go new file mode 100644 index 00000000..d34ebe0a --- /dev/null +++ b/examples/capwindow.go @@ -0,0 +1,37 @@ +package main + +import ( + "fmt" + + opencv3 ".." +) + +func main() { + deviceID := 0 + webcam := opencv3.NewVideoCapture() + defer webcam.Delete() + + if ok := webcam.OpenDevice(int(deviceID)); !ok { + fmt.Printf("error opening device: %v\n", deviceID) + return + } + + window := opencv3.NewWindow("Capture") + + // streaming, capture from webcam + img := opencv3.NewMat() + defer img.Delete() + fmt.Printf("start reading camera device: %v\n", deviceID) + for { + if ok := webcam.Read(img); !ok { + fmt.Printf("cannot read device %d\n", deviceID) + return + } + if img.Empty() { + continue + } + + window.IMShow(img) + opencv3.WaitKey(1) + } +} diff --git a/videoio.cpp b/videoio.cpp index a1e8c0db..e6573dde 100644 --- a/videoio.cpp +++ b/videoio.cpp @@ -29,7 +29,7 @@ int VideoCapture_IsOpened(VideoCapture v) { return v->isOpened(); } -int VideoCapture_Read(VideoCapture v, MatVec3b buf) { +int VideoCapture_Read(VideoCapture v, Mat buf) { return v->read(*buf); } diff --git a/videoio.go b/videoio.go index 2e169467..bb4ccc91 100644 --- a/videoio.go +++ b/videoio.go @@ -54,9 +54,9 @@ func (v *VideoCapture) IsOpened() bool { return isOpened != 0 } -// Read set frame to argument MatVec3b, returns `false` when the video capture +// Read set frame to argument Mat, returns `false` when the video capture // cannot read frame. -func (v *VideoCapture) Read(m MatVec3b) bool { +func (v *VideoCapture) Read(m Mat) bool { return C.VideoCapture_Read(v.p, m.p) != 0 } diff --git a/videoio.h b/videoio.h index 5f7628b2..e20ee83a 100644 --- a/videoio.h +++ b/videoio.h @@ -24,7 +24,7 @@ int VideoCapture_OpenDevice(VideoCapture v, int device); void VideoCapture_Release(VideoCapture v); void VideoCapture_Set(VideoCapture v, int prop, int param); int VideoCapture_IsOpened(VideoCapture v); -int VideoCapture_Read(VideoCapture v, MatVec3b buf); +int VideoCapture_Read(VideoCapture v, Mat buf); void VideoCapture_Grab(VideoCapture v, int skip); // VideoWriter