| Store | Cart

Re: [C++-sig] Convert OpenCV class reference return value

From: Stefan Seefeld <ste...@seefeld.name>
Tue, 4 Apr 2017 08:28:32 -0400
Hi Chris,

On 03.04.2017 11:58, Chris Schnaufer wrote:
>> I have a C++ library where const references to OpenCV types are> returned. Now I’m porting the interface to Python and have hit upon> what looks to be an old issue; there doesn’t appear to be a way to> have boost.python intrinsically convert a const reference OpenCV type> to a Python object (tuple or numpy). My guess is that I’m missing> something, but maybe not. So I’m providing more details>>  >> Right now I’m writing wrapper classes/functions to handle the> interface but would prefer to define a conversion of the types in a> way that boost.python can just do the conversion without individual> class/function wrappers explicitly declared for everything.>

Boost.Python was designed specifically to be fully unintrusive, i.e. to
allow Python bindings to be added to C++ APIs without requiring any
changes to the API being wrapped. Hand-written wrapper types shouldn't
be needed, as generating them is precisely what Boost.Python automatizes.

>  >> For example, if a method returns a member variable of type cv::Vec3d> as ‘const cv::Vec3d&’, I would like to declare one “const cv:Vec3d& to> Python” converter and have that be used/specified where needed; and> another for Python to const cv:Vec3d&. Sans individual wrapper> classes/functions.

Yes, of course !

>  Going over the library I’ve come closest with one of: 1) declare> things in such a way as to get a [paraphrasing] “unable to convert> pointer to const cv::Vec3d&() type” error [in> as_to_python_function.hpp – so close!], or 2) use> return_value_policy<copy_const_ref> which Python subsequently can’t> convert.>

I think you should spend a moment to fully understand these errors. (It
might help if you could provide the full errors here, instead of
paraphrasing them, so I can help with that. The first looks suspicious
as it suggests that you want to convert a function pointer (returning a
const reference to Vec3d). The second error simply tells you that you
need a "return-value policy" (see for example
http://boostorg.github.io/python/doc/html/tutorial/tutorial/functions.html#tutorial.functions.call_policies).

>  >> I think declaring something along the lines of “class_<cv::Vec3d>…”> might work but then I’m redefining the OpenCV class which I don’t like> to do since my library shouldn’t make OpenCV types visible in Python.>

If you don't expose cv::Vec3d to Python then you can't expect function
arguments (and return values) to be automatically converted between
Python and C++.
(But I'm a little confused by your phrasing: Something like
`class_<cv::Vec3d>` doesn't "redefine" anything, and it doesn't make a
C++ type "visible" in Python.
Rather, it generates a Python type to/from the given C++ type can be
converted automatically. You have full control over all of its
attributes, from its name to its members and methods down to how the
Python ("wrapper") object stores the C++ object, i.e. whether by-value,
or as a shared pointer, or something else.

>  >> I am new to boost.python so I’m probably wrong, but it appears the> best solution would be to have as_to_python_function.cpp split into> reference and copy-constructor classes to handle the appropriate> differences. Right now the same class handles both cases.>>  >> Any help is appreciated.>>  >> I am including a simple example:>

(I'm not sure what you want to show with your example. It would be
better to make it minimally functional to demonstrate what you are
trying to do, or what doesn't work as you'd expect.)

Best,
        Stefan

-- 

      ...ich hab' noch einen Koffer in Berlin...

_______________________________________________
Cplusplus-sig mailing list
Cplu...@python.org
https://mail.python.org/mailman/listinfo/cplusplus-sig

Recent Messages in this Thread
Chris Schnaufer Apr 03, 2017 03:58 pm
Stefan Seefeld Apr 04, 2017 12:28 pm
Messages in this thread