[C++-sig] Convert OpenCV class reference return value
Chris Schnaufer
chris.schnaufer at eagleview.com
Mon Apr 3 11:58:58 EDT 2017
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.
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.
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 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.
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:
#include <boost/python.hpp>
namespace p = boost::python;
class Vec3d
{
public:
Vec3d() {v[0] = v[1] = v[2] = 0.0;}
virtual ~Vec3d() {}
private:
double v[3];
};
class UseVec3d
{
public:
UseVec3d() {}
UseVec3d(const Vec3d& vec) : v(vec) {}
virtual ~UseVec3d() {};
const Vec3d& get_vec() const {return v;}
void set_vec(const Vec3d& vec) {v = vec;}
private:
Vec3d v;
};
BOOST_PYTHON_MODULE(test)
{
p::class_<UseVec3d>("UseVec3d")
.def("get_vec", &UseVec3d::get_vec) // Error's here
;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20170403/64e52cee/attachment.html>
More information about the Cplusplus-sig
mailing list