How do you exchange OpenCV code to C ++ with Boost :: Python?

I want my C ++ OpenCV code from boost::python

, and to find out how to do it, I tried a toy example where

  • I am using Boost.Numpy project to provide me boost::numpy::ndarray

    .

  • The C ++ function to be wrapped square()

    takes boost::numpy::ndarray

    and modifies it in place, squaring every element in it.

  • The name of the exported Python module is named test

    .

  • The square()

    C ++ function is exported as a name square

    in the exported module.

  • I'm not using bjam because IMO is too complicated and just doesn't work for me no matter what. I use the good old one make

    .

Now here's the code:

// test.cpp
#include <boost/python.hpp>
#include <boost/numpy.hpp>
#include <boost/scoped_array.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>

namespace py = boost::python;
namespace np = boost::numpy;

void square(np::ndarray& array)
{
    if (array.get_dtype() != np::dtype::get_builtin<int>())
    {
        PyErr_SetString(PyExc_TypeError, "Incorrect array data type.");
        py::throw_error_already_set();
    }
    size_t rows = array.shape(0), cols = array.shape(1);
    size_t stride_row = array.strides(0) / sizeof(int), 
           stride_col = array.strides(1) / sizeof(int);
    cv::Mat mat(rows, cols, CV_32S);
    int *row_iter = reinterpret_cast<int*>(array.get_data());
    for (int i = 0; i < rows; i++, row_iter += stride_row)
    {
        int *col_iter = row_iter;
        int *mat_row = (int*)mat.ptr(i);
        for (int j = 0; j < cols; j++, col_iter += stride_col)
        {
            *(mat_row + j) = (*col_iter) * (*col_iter); 
        }
    }

    for (int i = 0; i < rows; i++, row_iter += stride_row)
    {
        int *col_iter = row_iter;
        int *mat_row = (int*)mat.ptr(i);
        for (int j = 0; j < cols; j++, col_iter += stride_col)
        {
            *col_iter = *(mat_row + j);
        }
    }
}


BOOST_PYTHON_MODULE(test)
{
   using namespace boost::python;
   def("square", square);
}

      

And here's the Makefile:

PYTHON_VERSION = 2.7
PYTHON_INCLUDE = /usr/include/python$(PYTHON_VERSION)

BOOST_INC = /usr/local/include
BOOST_LIB = /usr/local/lib
OPENCV_LIB = $$(pkg-config --libs opencv)
OPENCV_INC = $$(pkg-config --cflags opencv)

TARGET = test

$(TARGET).so: $(TARGET).o
        g++ -shared -Wl,--export-dynamic \
        $(TARGET).o -L$(BOOST_LIB) -lboost_python \
        $(OPENCV_LIB) \
        -L/usr/lib/python$(PYTHON_VERSION)/config -lpython$(PYTHON_VERSION) \
        -o $(TARGET).so

$(TARGET).o: $(TARGET).cpp
        g++ -I$(PYTHON_INCLUDE) $(OPENCV_INC) -I$(BOOST_INC) -fPIC -c $(TARGET).cpp

      

With this scheme I can enter make

and test.so

will be created. But when I try to import it,

In [1]: import test
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-73ae3ffe1045> in <module>()
----> 1 import test

ImportError: ./test.so: undefined symbol:        _ZN5boost6python9converter21object_manager_traitsINS_5numpy7ndarrayEE10get_pytypeEv

In [2]: 

      

This is a linker error that I cannot fix. Can anyone help me on what is going on? Do you have (links to) code that already integrates OpenCV, numpy and Boost.Python without things like Py ++ or the like? ...

+3


source to share


1 answer


Ok I fixed that. It was a simple problem, but the sleepy brain and portions bjam

made me ignore it. In Makefile

I forgot to put -lboost_numpy

that links the Boost.Numpy libraries to my lib. So the modified Makefile

looks like this:



PYTHON_VERSION = 2.7
PYTHON_INCLUDE = /usr/include/python$(PYTHON_VERSION)

BOOST_INC = /usr/local/include
BOOST_LIB = /usr/local/lib
OPENCV_LIB = $$(pkg-config --libs opencv)
OPENCV_INC = $$(pkg-config --cflags opencv)

TARGET = test

$(TARGET).so: $(TARGET).o
        g++ -shared -Wl,--export-dynamic \
        $(TARGET).o -L$(BOOST_LIB) -lboost_python -lboost_numpy \
        $(OPENCV_LIB) \
        -L/usr/lib/python$(PYTHON_VERSION)/config -lpython$(PYTHON_VERSION) \
        -o $(TARGET).so

$(TARGET).o: $(TARGET).cpp
        g++ -I$(PYTHON_INCLUDE) $(OPENCV_INC) -I$(BOOST_INC) -fPIC -c $(TARGET).cpp

      

+2


source







All Articles