我想用boost::python
包装我的C++OpenCV代码,为了学习如何做到这一点,我尝试了一个玩具示例,其中
-
我使用Boost.Numbery项目为我提供
boost::numpy::ndarray
。 -
要包装的C++函数
square()
采用boost::numpy::ndarray
,并通过对其中的每个元素进行平方来对其进行适当的修改。 -
导出的Python模块名称称为
test
。 -
square()
C++函数作为导出模块中的square
名称导出。 -
我不使用bjam,因为IMO它太复杂了,无论如何都不适合我。我用的是好的旧
make
。
现在,这是代码:
// 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);
}
这是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
使用此方案,我可以键入make
,然后创建test.so
。但当我尝试导入它时,
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]:
这是一个链接器错误,我似乎无法修复。有人能帮我了解一下发生了什么事吗你有没有(链接到)已经集成了OpenCV、numpy和Boost.Python的代码,而没有Py++之类的东西。
好的,我解决了这个问题。这是一个简单的问题,但昏昏欲睡的大脑和几份bjam
让我忽略了它。在Makefile
中,我忘了放-lboost_numpy
,它将Boost.Numbery库链接到我的库。因此,修改后的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 -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