Ctypes passing Mat object

Hey guys,
I’m trying to receive a mat object from cpp program, which I wrote for interacting with a camera. The principle is to share the memory between 2 programs which makes everything cleaner and more efficient. So far there is no problem having the image copied to the address sent by the python inside the cpp (imshow in cpp is precisely the original footage). Yet, once back to python, the matrix seems lost even those the pointer is pointing to the same location. With imwrite in python, a random pixelated picture is saved. And there is also the problem of converting numpy array to cv matrix.

The address is generated in python first with numpy.zeros((h,w,3),dtype=numpy.uint8) and linkage between python and cpp is done with ctypes python library.

The 3 problems in this case is:

  1. Is it possible to convert numpy array to cv mat on the fly? (without saving it locally then read it)./Or generate an empty Mat object.
  2. Is there any better way to do this linkage?
  3. Might there be some problem with protecting the memory to make sure the image does not loss after copying from cpp program?

clearly, a misconception …

you have some options.

  • investigate OpenCV’s wrapper generation to learn how OpenCV translates between cv::Mat and numpy arrays
  • use Cython to write glue code

beware though. I say this not knowing anything about your situation. you haven’t given anything debuggable, or given the context required to question your design decisions.

Thanks crackwitz,
I’m just trying to recode an existing program with the same procedure and key functions because some of the old code’s packages are obsolete or incompatible with the new environment the project will be in. Thanks for the help, and I’ll see if it’s worth rewriting this part of the code or starting from scratch.