Send cv::Mat from c++ to python

asked 2017-07-04 09:00:52 -0500

PiMac gravatar image

Hi I have created a c++ code that reads GIGE camera image using its sdk and converted it into cv::Mat. Now I have wrapped this code to use it in python but i dont know how to return Mat object to python.

PyObject* some_function(PyObject* self, PyObject* args)

{ //GIGE SDK gets image to hcamera3 object //***OPENCV PART******** // do image processing std::cout << "Acquired image...\n";

    CvSize size;
    size.width = ImageWidth(hCamera3);
    size.height = ImageHeight(hCamera3);

    retval = cvCreateImageHeader(size, ocv_depth(ImageDatatype(hCamera3, 0)), ImageDimension(hCamera3));

    ppixels = nullptr;

    GetLinearAccess(hCamera3, 0, &ppixels, &xInc, &yInc);
    cvSetData(retval, ppixels, yInc);
    //matToReturn = cv::cvarrToMat(retval, true);

    matToReturn = cv::cvarrToMat(retval, true);
    std::cout << matToReturn.size << "SIZE" << std::endl;;
    cv::imwrite("E:/PythonGenie.bmp", matToReturn);

    // stop the grab (kill = true: wait for ongoing frame acquisition to stop)
    result = G2Freeze(hCamera3, true);

    // free camera

return ?????????????????????


I am not sure what to put into return ?? and then how assign it in python. In java it was just passin a Mat object to c++ but here I am lost.

Any help appriciated!

edit retag flag offensive close merge delete


you actually have to pass a numpy array back to python, neither a cv::Mat, nor (HELL, NO !) a c-api IplImage*

berak gravatar imageberak ( 2017-07-05 00:30:10 -0500 )edit

Thanks I will try it and let you know how it went.

PiMac gravatar imagePiMac ( 2017-07-05 03:33:45 -0500 )edit

do i need to convert cv::Mat to numpy and than return numpy from Cpp code.?? are you able to solve this issue..??

vineetkr137 gravatar imagevineetkr137 ( 2018-04-16 08:36:14 -0500 )edit

@vineetkr137, please ask a new question, with your specific situation.

berak gravatar imageberak ( 2018-04-16 08:43:23 -0500 )edit

@berak , I have the same problem mentioned above. created a C++ for pointgrey camera suing sdk. and converted into cv::Mat, wrapped cpp code to use in python. but how do I converted into numpy array so that i can use this image in python code such as cv::imshow...??

vineetkr137 gravatar imagevineetkr137 ( 2018-04-17 06:14:30 -0500 )edit