0

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?

Greenonline
  • 1,330
  • 8
  • 23
  • 31
  • this might be of help https://stackoverflow.com/questions/22736593/what-is-the-easiest-way-to-convert-ndarray-into-cvmat, https://github.com/yati-sagade/opencv-ndarray-conversion. – Eswar Oct 22 '18 at 08:50

1 Answers1

1

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!!!!.

Milind Deore
  • 2,887
  • 5
  • 25
  • 40