Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Python wrapper for my algorithm which uses Opencv 2.3

I am looking to wrap a c++ class which implements an algorithm I wrote using Opencv 2.3. I am aware that there is python wrappers for opencv as a whole but what I need is to wrap my own code which uses opencv. This seems logical because the lower level of my algorithm will be fast compiled c++ code and I will be free to call it from python and build a system around it.

My class is actually quite simple, it has 4 main methods:

  void train( std::vector<cv::Mat> );
  void save();
  void load();
  bool detect( cv::Mat );

This is essentially the majority of what I need to wrap. The problem is that I don't know how to best go about this. I have looked into ctypes, swig, boost python and pyplusplus. I have not been successful with any of the above to date.

I keep encountering issues with how wrap the opencv object cv::Mat. From python I will be using numpy arrays so I know I need conversion code from a numpy array to a cv::Mat and I have to register it.

I feel like someone else must have tried something like this at one point, if you can help me out it is much appreciated


Just to reiterate the goal: Wrap my c++ class which uses opencv into a python library so that I can use my algorithm from python.

I think I have the conversion somewhat figured out (with the help of the opencv source) but I still won't work from python.

Okay so I have been working with the code linked to in the above post (a conversion from numpy to cv::Mat) and I am still encountering problems. I am going to post my code and hopefully someone more knowledgeable than I can help me out here.

For example here is a 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 and bits of code from the above link:

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 pano_cv library but from playing around with the official opencv source I know that this function is also there to some degree. 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.

Help is most appreciated.

like image 232
Kevin Avatar asked Nov 13 '22 17:11

Kevin


1 Answers

I found some code on google that converts numpy array to cv::mat in c++ using boost::python: link

like image 138
Fábio Diniz Avatar answered Dec 15 '22 00:12

Fábio Diniz