edmBernard / pybind11_opencv_numpy

Implementation of cv::Mat conversion to numpy.array for pybind11
Apache License 2.0
198 stars 41 forks source link

How to use this with Mat pointer #20

Closed benknight135 closed 3 years ago

benknight135 commented 3 years ago

I have the following C++ function to try returning a Mat pointer:

void generate_black_image(int x, int y, cv::Mat& dst) {
    cv::Mat image = cv::Mat::zeros(cv::Size(x,y),CV_8UC1);
    dst = image;
}

I tried using the following pybind:

PYBIND11_MODULE(core, m) {
  NDArrayConverter::init_numpy();
  m.def("generate_black_image", &I3DR::generate_black_image);
}

But I'm not sure how to use it in python as it doesn't deal with pointers. I'm not sure how to run this in python but I tried passing the numpy array like this:

blank_image = np.array([])
generate_black_image(200, 200, blank_image)
print("Image created with size {}".format(blank_image.shape))
cv2.imshow("test", blank_image)
cv2.waitKey(0)

But that doesn't populate the numpy array with the data and remains empty.

I am doing it like this rather than a return variable as I am also trying to support C# with extern C so need to use standard C types.

Please let me know what I am doing wrong or if this is possible?

edmBernard commented 3 years ago

I will have to check if it's possible to pass cv::Mat as reference. It can be a limitation of python more than a limitation of pybind11. https://pybind11.readthedocs.io/en/stable/faq.html#limitations-involving-reference-arguments

edmBernard commented 3 years ago

I have check and currently with my binding even in some case return by reference are in fact copied. I'll will try to check if I can use the eigen bind provided by pybind11 to allow correctly these reference.

edmBernard commented 3 years ago

in the Eigen binding provided by pybind11. There enable this by using a Eigen::Ref that encapsulate the reference explained here : https://pybind11.readthedocs.io/en/stable/advanced/cast/eigen.html#pass-by-reference I haven't find equivalent way to do that with opencv mat.

edmBernard commented 3 years ago

I haven't found a proper way to handle this. I'll close the issue for the moment. If you found how we can to this, you are welcome to continue the discussion