Can save an image from Mat to disk

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram
2017-09-22 20:32:49 +02:00
parent b8fac27a0c
commit 3dddcbe528
5 changed files with 60 additions and 2 deletions

View File

@@ -61,7 +61,7 @@ func main() {
### Ubuntu/Linux ### Ubuntu/Linux
You can use `make` to install OpenCV 3.3 with the handy `Makefile` included with this repo. If you already have installed it, you do not need to do so again. Also the installation performed by the `Makefile` may remove options that you have already installed. You can use `make` to install OpenCV 3.3 with the handy `Makefile` included with this repo. If you already have installed it, you do not need to do so again. The installation performed by the `Makefile` is minimal, so it may remove OpenCV options if you have already installed it some other way.
#### Install required packages #### Install required packages
@@ -115,7 +115,7 @@ The showinfo.go program should output the following:
go-opencv3 version: 0.0.1 go-opencv3 version: 0.0.1
opencv lib version: 3.3.0 opencv lib version: 3.3.0
### Other Linux installations ### Other Linux installations
One way to find out thelocations for your includes and libs is to use the `pkg-config` tool like this: One way to find out thelocations for your includes and libs is to use the `pkg-config` tool like this:

42
examples/saveimage.go Normal file
View File

@@ -0,0 +1,42 @@
// +build example
//
// Do not build by default.
//
// This example uses the VideoCapture class to capture a frame from a connected webcam,
// then save it to a file on disk
//
// how to run:
// go run ./examples/saveimage.go filename.jpg
//
package main
import (
"fmt"
"os"
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)
}
img := opencv3.NewMat()
defer img.Delete()
if ok := webcam.Read(img); !ok {
fmt.Printf("cannot read device %d\n", deviceID)
return
}
if img.Empty() {
fmt.Printf("no image on device %d\n", deviceID)
return
}
opencv3.IMWrite(os.Args[1], img)
}

View File

@@ -5,3 +5,8 @@ Mat Image_IMRead(const char* filename, int flags) {
cv::Mat img = cv::imread(filename, flags); cv::Mat img = cv::imread(filename, flags);
return new cv::Mat(img); return new cv::Mat(img);
} }
bool Image_IMWrite(const char* filename, Mat img) {
return cv::imwrite(filename, *img);
}

View File

@@ -16,3 +16,11 @@ func IMRead(name string, flags int) Mat {
return Mat{p: C.Image_IMRead(cName, C.int(flags))} return Mat{p: C.Image_IMRead(cName, C.int(flags))}
} }
// IMWrite writes a Mat to an image file
func IMWrite(name string, img Mat) bool {
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
return bool(C.Image_IMWrite(cName, img.p))
}

View File

@@ -1,6 +1,8 @@
#ifndef _OPENCV3_IMGCODECS_H_ #ifndef _OPENCV3_IMGCODECS_H_
#define _OPENCV3_IMGCODECS_H_ #define _OPENCV3_IMGCODECS_H_
#include <stdbool.h>
#ifdef __cplusplus #ifdef __cplusplus
#include <opencv2/opencv.hpp> #include <opencv2/opencv.hpp>
extern "C" { extern "C" {
@@ -9,6 +11,7 @@ extern "C" {
#include "core.h" #include "core.h"
Mat Image_IMRead(const char* filename, int flags); Mat Image_IMRead(const char* filename, int flags);
bool Image_IMWrite(const char* filename, Mat img);
#ifdef __cplusplus #ifdef __cplusplus
} }