1

I created a c++ class, which uses OpenCV. One method returns a cv::Mat which I want to "get" from inside a python file.

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();}
}

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?

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

The result is. a = None

From the cpp file I first checked that the file is read. And it is. (checked with "imshow")

What am I doing wrong??

Thanks!

GPPK
  • 6,260
  • 4
  • 31
  • 56
Easyrider
  • 3,099
  • 4
  • 20
  • 31
  • 2
    for working interop between python and native, you can refer to this. http://stackoverflow.com/questions/19031836/get-background-model-from-backgroundsubtractormog2-in-python – Zaw Lin Feb 10 '15 at 11:00
  • You saved my day! Thank you very much! – Easyrider Feb 10 '15 at 13:48

0 Answers0