pythonc++type-conversionboost-pythonpyobject

Converting PyObject* to C++ Object


I have a shared library (util.so) that returns the following to python:

cv::Mat toCvMat(PyObject *ndArrayObj) {
    // return cv::Mat
}

I invoke the method from my python script using:

Python:

testMat = util.toCvMat(orig_frame_gray_img)

When I pass this to my other test library (test_library.so), it is passed as PyObject*, how do I access cv::Mat in my test_library.so?:

Python:

test_library.process(testMat)

C++

bool TestLibrary::Process( PyObject* pTestImg)
{
// How to get cv::Mat from pTestImg?
}

At present, my test_library doesn't link with util and they are separate .so files.


Solution

  • Based on http://yati-sagade.github.io/2013/05/11/numpy-boost-python/

    NDArrayConverter cvt;
    cv::Mat m;
    m = cvt.toMat(pTestImg);
    

    The required NDArrayConverter can be found from https://github.com/yati-sagade/opencv-ndarray-conversion