Wrap C++ code for use in Python program

I try to convert my C++ code, based on OpenCV 4, for use in Python programs. I have read


but, when extending C++ functions using CV_EXPORTS_W macro and others, I’ve seen in example, in arguments sometimes we stay cv::Mat, sometimes change it for InputArray or OutputArray. What is the rule?

IMHO Inputarray or outputarray are generic types ( proxy class ). If your algorithm always uses Mat you don’t need it.
Opencv mainteners like InputArray outputArray in opencv pull request (not opencv_contrib)

I would also read

Essentially the bindings generator needs to know if the argument is an input, output or inputoutput array when it generates the bindings. This is so the arguments can be placed correctly in the python call. From memory it goes something like

CV_EXPORTS_W void python_func(InputArray in, OutputArray out, _InputOutputArray inout)

out,inout= python_func(in,[inout])

If you had a function where “out” can only be a Mat then you would use

CV_EXPORTS_W void python_func(InputArray in, CV_OUT Mat out, InputOutputArray inout)
1 Like

Does const in C++ matter in this conversion to Python?

If I set CV_OUT Mat out in python_func argument, would Python understand it?

i don’t know, what you mean here, this has to go into the c++ signature, not into python code using it

the Mat will be added to the list of returned values. if you have a c++ function

CV_EXPORTS_W void foo(int i, CV_OUT Mat mat);

it should generate a python signature like

foo(i) -> mat
1 Like

I’m not sure I understand the question. Do you mean will

CV_EXPORTS_W void python_func(InputArray in, CV_OUT Mat out, InputOutputArray inout)

result in

out,inout= python_func(in,[inout])