Skip to content

Commit

Permalink
Can load a file into a Mat, and display it in the 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 18c6263 commit ea376b3
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 4 deletions.
4 changes: 4 additions & 0 deletions core.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#include "core.h"
#include <string.h>

Mat Mat_New() {
return new cv::Mat();
}

MatVec3b MatVec3b_New() {
return new cv::Mat_<cv::Vec3b>();
}
Expand Down
13 changes: 13 additions & 0 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions core.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,20 @@ typedef struct RawData {
} Rects;

#ifdef __cplusplus
typedef cv::Mat* Mat;
typedef cv::Mat_<cv::Vec3b>* MatVec3b;
typedef cv::Mat_<cv::Vec4b>* MatVec4b;
#else
typedef void* Mat;
typedef void* MatVec3b;
typedef void* MatVec4b;
#endif

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);
Expand Down
9 changes: 8 additions & 1 deletion examples/window.go
Original file line number Diff line number Diff line change
@@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion highgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

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

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


Expand Down
7 changes: 7 additions & 0 deletions imgcodecs.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
18 changes: 18 additions & 0 deletions imgcodecs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package opencv3

/*
#include <stdlib.h>
#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))}
}
17 changes: 17 additions & 0 deletions imgcodecs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef _OPENCV3_IMGCODECS_H_
#define _OPENCV3_IMGCODECS_H_

#ifdef __cplusplus
#include <opencv2/opencv.hpp>
extern "C" {
#endif

#include "core.h"

Mat Image_IMRead(const char* filename, int flags);

#ifdef __cplusplus
}
#endif

#endif //_OPENCV3_IMGCODECS_H_

0 comments on commit ea376b3

Please sign in to comment.