Skip to content

Commit

Permalink
Can capture video Mat buffers and display them in a Window
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Sep 20, 2017
1 parent 6599469 commit 768da9b
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 5 deletions.
4 changes: 4 additions & 0 deletions core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_<cv::Vec3b>();
Expand Down
6 changes: 6 additions & 0 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions core.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion examples/capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
37 changes: 37 additions & 0 deletions examples/capwindow.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
2 changes: 1 addition & 1 deletion videoio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions videoio.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion videoio.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 768da9b

Please sign in to comment.