From ea376b34cd2d770d0e1088366d754ed4b8a7b860 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Wed, 20 Sep 2017 04:19:42 +0200 Subject: [PATCH] Can load a file into a Mat, and display it in the Window Signed-off-by: deadprogram --- core.cpp | 4 ++++ core.go | 13 +++++++++++++ core.h | 4 ++++ examples/window.go | 9 ++++++++- highgui.cpp | 2 +- highgui.go | 2 +- highgui.h | 2 +- imgcodecs.cpp | 7 +++++++ imgcodecs.go | 18 ++++++++++++++++++ imgcodecs.h | 17 +++++++++++++++++ 10 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 imgcodecs.cpp create mode 100644 imgcodecs.go create mode 100644 imgcodecs.h diff --git a/core.cpp b/core.cpp index bf866d2d..ddb435cc 100644 --- a/core.cpp +++ b/core.cpp @@ -1,6 +1,10 @@ #include "core.h" #include +Mat Mat_New() { + return new cv::Mat(); +} + MatVec3b MatVec3b_New() { return new cv::Mat_(); } diff --git a/core.go b/core.go index 1e52cb07..d7310118 100644 --- a/core.go +++ b/core.go @@ -18,6 +18,19 @@ const ( CvCapPropFps = 5 ) +// CMat is an alias for C pointer. +type CMat C.Mat + +// Mat is a bind of `cv::Mat +type Mat struct { + p C.Mat +} + +// NewMat returns a new Mat. +func NewMat() Mat { + return Mat{p: C.Mat_New()} +} + // CMatVec3b is an alias for C pointer. type CMatVec3b C.MatVec3b diff --git a/core.h b/core.h index 9185d9a3..d76f12ee 100644 --- a/core.h +++ b/core.h @@ -27,9 +27,11 @@ typedef struct RawData { } Rects; #ifdef __cplusplus + typedef cv::Mat* Mat; typedef cv::Mat_* MatVec3b; typedef cv::Mat_* MatVec4b; #else + typedef void* Mat; typedef void* MatVec3b; typedef void* MatVec4b; #endif @@ -37,6 +39,8 @@ typedef struct RawData { void Rects_Delete(struct Rects rs); void DrawRectsToImage(MatVec3b img, struct Rects rects); + Mat Mat_New(); + MatVec3b MatVec3b_New(); struct ByteArray MatVec3b_ToJpegData(MatVec3b m, int quality); void MatVec3b_Delete(MatVec3b m); diff --git a/examples/window.go b/examples/window.go index adea4329..3362ad2e 100644 --- a/examples/window.go +++ b/examples/window.go @@ -1,15 +1,22 @@ +// how to use +// go run ./examples/window.go /home/ron/Pictures/mcp23017.jpg +// package main import ( + "os" "time" opencv3 ".." ) func main() { - opencv3.NewWindow("Hello") + filename := os.Args[1] + window := opencv3.NewWindow("Hello") + img := opencv3.IMRead(filename, 1) for { + window.IMShow(img) opencv3.WaitKey(1) time.Sleep(100 * time.Microsecond) } diff --git a/highgui.cpp b/highgui.cpp index 219cc8c4..6a9e59f8 100644 --- a/highgui.cpp +++ b/highgui.cpp @@ -9,7 +9,7 @@ void Window_Delete(const char* winname) { cv::destroyWindow(winname); } -void Window_IMShow(const char* winname, MatVec3b mat) { +void Window_IMShow(const char* winname, Mat mat) { cv::imshow(winname, *mat); } diff --git a/highgui.go b/highgui.go index 282c1293..3576e1bd 100644 --- a/highgui.go +++ b/highgui.go @@ -33,7 +33,7 @@ func (w *Window) Delete() { } // IMShow takes an image Mat and displays it in the Window -func (w *Window) IMShow(img MatVec3b) { +func (w *Window) IMShow(img Mat) { cName := C.CString(w.name) defer C.free(unsafe.Pointer(cName)) diff --git a/highgui.h b/highgui.h index fb38e773..cbf221c0 100644 --- a/highgui.h +++ b/highgui.h @@ -11,7 +11,7 @@ extern "C" { // Window void Window_New(const char* winname, int flags); void Window_Delete(const char* winname); -void Window_IMShow(const char* winname, MatVec3b mat); +void Window_IMShow(const char* winname, Mat mat); int Window_WaitKey(int); diff --git a/imgcodecs.cpp b/imgcodecs.cpp new file mode 100644 index 00000000..3467c756 --- /dev/null +++ b/imgcodecs.cpp @@ -0,0 +1,7 @@ +#include "imgcodecs.h" + +// Image +Mat Image_IMRead(const char* filename, int flags) { + cv::Mat img = cv::imread(filename, flags); + return new cv::Mat(img); +} diff --git a/imgcodecs.go b/imgcodecs.go new file mode 100644 index 00000000..046b09a8 --- /dev/null +++ b/imgcodecs.go @@ -0,0 +1,18 @@ +package opencv3 + +/* +#include +#include "imgcodecs.h" +*/ +import "C" +import ( + "unsafe" +) + +// IMRead reads an image file into a Mat +func IMRead(name string, flags int) Mat { + cName := C.CString(name) + defer C.free(unsafe.Pointer(cName)) + + return Mat{p: C.Image_IMRead(cName, C.int(flags))} +} diff --git a/imgcodecs.h b/imgcodecs.h new file mode 100644 index 00000000..751b7bd2 --- /dev/null +++ b/imgcodecs.h @@ -0,0 +1,17 @@ +#ifndef _OPENCV3_IMGCODECS_H_ +#define _OPENCV3_IMGCODECS_H_ + +#ifdef __cplusplus +#include +extern "C" { +#endif + +#include "core.h" + +Mat Image_IMRead(const char* filename, int flags); + +#ifdef __cplusplus +} +#endif + +#endif //_OPENCV3_IMGCODECS_H_