[C++-sig] Boost Python wrapper for a c++ class that uses Opencv 2.3

Kevin Hughes k.hughes at queensu.ca
Wed Sep 21 15:31:20 CEST 2011


I am trying to create a python library from a class which uses opencv 2.3. I
want to be able to pass numpy array's into the class where they will be
converted into cv::Mat's processed then converted back to numpy array's and
returned.

Here is a simple test class I am working on to get this working before
wrapping my own class. Currently I am just trying to receive a numpy array
concert to a cv::Mat, process it and then write it to file. After this is
working I will work on returning the processed array to python.

Here is the simple class:

foo.h :

#include <opencv2/core/core.hpp>

 class Foo {

    public:

        Foo();

        ~Foo();

        cv::Mat image;

        void bar( cv::Mat in );
};

foo.cpp :

  #include "foo.h"

  Foo::Foo(){}

  Foo::~Foo(){}

  void Foo::bar( cv::Mat in) {

      image = in;

      cv::Canny( image, image, 50, 100 );

      cv::imwrite("image.png", image);

  }

And here is where I have attempted to wrap this class using boost::python (I
am using code from the opencv source for the the numpy to mat conversion)

wrap_foo.cpp

#include <boost/python.hpp>
#include <numpy/arrayobject.h>

#include <opencv2/core/core.hpp>

#include "foo.h"

using namespace cv;
namespace bp = boost::python;

//// Wrapper Functions
void bar(Foo& f, bp::object np);

//// Converter Functions

cv::Mat convertNumpy2Mat(bp::object np);

//// Wrapper Functions
void bar(Foo& f, bp::object np)
{

    Mat img = convertNumpy2Mat(np);

    f.bar(img);

    return;
}


//// Boost Python Class

BOOST_PYTHON_MODULE(lib)
{

    bp::class_<Foo>("Foo")

        .def("bar", bar)

        ;
}


//// Converters

cv::Mat convertNumpy2Mat(bp::object np)
{

   Mat m;

   numpy_to_mat(np.ptr(),m);

   return m;
}

The numpy_to_mat function is from the opencv source
(modules/python/src2/cv2.cpp). The full file has the function below what I
wrote above. This code compiles with bjam just fine but the when I import
into python it crashes. The error is this: libFoo.so: undefined symbol:
_ZN2cv3Mat10deallocateEv. I have tried a number of different things but I
can't get this to work.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20110921/920e799c/attachment-0001.html>


More information about the Cplusplus-sig mailing list