mirror of
https://github.com/hybridgroup/gocv
synced 2025-08-25 08:41:04 +08:00
Can save an image from Mat to disk
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
@@ -61,7 +61,7 @@ func main() {
|
||||
|
||||
### 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
|
||||
|
||||
|
42
examples/saveimage.go
Normal file
42
examples/saveimage.go
Normal 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)
|
||||
}
|
@@ -5,3 +5,8 @@ Mat Image_IMRead(const char* filename, int flags) {
|
||||
cv::Mat img = cv::imread(filename, flags);
|
||||
return new cv::Mat(img);
|
||||
}
|
||||
|
||||
bool Image_IMWrite(const char* filename, Mat img) {
|
||||
return cv::imwrite(filename, *img);
|
||||
}
|
||||
|
||||
|
@@ -16,3 +16,11 @@ func IMRead(name string, flags int) Mat {
|
||||
|
||||
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))
|
||||
}
|
||||
|
@@ -1,6 +1,8 @@
|
||||
#ifndef _OPENCV3_IMGCODECS_H_
|
||||
#define _OPENCV3_IMGCODECS_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
#include <opencv2/opencv.hpp>
|
||||
extern "C" {
|
||||
@@ -9,6 +11,7 @@ extern "C" {
|
||||
#include "core.h"
|
||||
|
||||
Mat Image_IMRead(const char* filename, int flags);
|
||||
bool Image_IMWrite(const char* filename, Mat img);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
Reference in New Issue
Block a user