我创建了一个使用OpenCV的c ++类。一种方法返回一个cv :: Mat我想要"得到"来自python文件。
webcam.cpp
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
//#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
class webcam{
private:
cv::VideoCapture webcamframe;
cv::Mat frame;
public:
webcam();
cv::Mat nextFrame();
};
webcam::webcam(){
std::cout << "Init of webcam module \n";
}
cv::Mat webcam::nextFrame(){
std::cout << "Asking for a new camera frame. \n";
Mat image;
image = imread("1.jpg", CV_LOAD_IMAGE_COLOR);
if(!image.data){
std::cout << "ERROR LOADING!";
} else {
return image;
/* namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image );
waitKey(0); */
}
}
extern "C" {
webcam* webcam_new(){ return new webcam(); }
cv::Mat frame(webcam* wc) {wc->nextFrame();}
}
我包装了c ++文件,以便可以在python中使用。 但我无法弄清楚如何获得cv :: Mat?
Pythonfile:
from cv2 import cv
import numpy
import ctypes
from ctypes import cdll
lib = cdll.LoadLibrary('./webcam.so')
class camCon(object):
def __init__(self):
self.obj = lib.webcam_new()
def frame(self):
lib.frame(self.obj)
cameraConnection = camCon()
pyMat = cv.CreateMat(640, 480, 0)
pyMat = cameraConnection.frame()
pyMat.Set()
a = numpy.asarray(pyMat)
print "data", a
结果是。 a = None
从cpp文件我首先检查了文件被读取。它是。 (用&#34; imshow&#34;检查)
我做错了什么?
谢谢!