Search code examples
python-3.xc++11boost-pythonmatnumpy-ndarray

How to convert a ndarray into opencv::Mat using Boost.Python?


I am reading an image in Python and passing that numpy array to C++ using Boost.Python and receiving that in ndarray.

I need to convert the same into cv::Mat to perform operations in OpenCV C++.

How do I do that?


Solution

  • Finally I found the solution for that from the documentations:

    We Have to receive the numpy array as numeric::array in C++ and have to do the following steps to easily convert the numpy into cv::mat efficiently.

    void* img_arr = PyArray_DATA((PyObject*)arr.ptr());
    

    And we need to pass this void ptr to the cv::Mat Constructor with other parameters required.

    Mat image(rows, cols , CV_8UC3, img_arr);
    
    1. int parameter: Expects the no. of rows
    2. int parameter: Expects the no. of cols
    3. Type parameter : Expects the type of image.
    4. Void Pointer Parameter: Expects the image data.

    And this resolves the problem!!!!.