I created a C++ class, which uses OpenCV. One method returns a cv::Mat
which I want to "get" from inside a python file.
#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";
cv::Mat image = imread("1.jpg", CV_LOAD_IMAGE_COLOR);
if (!image.data)
std::cout << "ERROR LOADING!";
else
return image;
}
extern "C" {
webcam* webcam_new() { return new webcam(); }
cv::Mat frame(webcam* wc) { wc->nextFrame(); }
}
I wrapped the C++ file so that it is possible to use from python.
But I can't figure out how to get the cv::Mat
?
from cv2 import cv
from ctypes import cdll
import numpy
import ctypes
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)
The result is a = None
.
I verified whether the file is readable from C++ (I checked with imshow
), and it is.
What am I doing wrong?