在构建共享python库表单.cpp文件时缺少opencv库

时间:2015-08-01 00:06:17

标签: python c++ opencv boost

我有一个c ++文件,我想从中构建一个共享库(.so文件)。这是.cpp文件:

#include "extract_features.hpp"

boost::python::object hog_feature_extract(boost::python::object &gray,    boost::python::object &pts2d, boost::python::object &pts3d) {
static Mat m_gray, m_pts2d, m_pts3d, m_feature_vector;

// Convert into opencv land...
pyopencv_to(gray.ptr(), m_gray, "info", true);
pyopencv_to(pts2d.ptr(), m_pts2d, "info", true);
pyopencv_to(pts3d.ptr(), m_pts3d, "info", true);

// Function call
hog_feature_extractor::shared_extractor().extract(m_gray, m_pts2d, m_pts3d, m_feature_vector);

// Output
PyObject *p_feature_vector = pyopencv_from(m_feature_vector);
boost::python::object boostPyObjFrame(boost::python::handle<>((PyObject*)p_feature_vector));

return boostPyObjFrame;
}

这是.hpp文件:

#include <boost/python.hpp>
#include "python_helper.hpp"
#include "hog_feature_extractor.hpp"
#include "numpy/ndarrayobject.h"
#include <opencv2/highgui/highgui.hpp>

using namespace boost::python;

boost::python::object hog_feature_extract(boost::python::object &gray,    boost::python::object &pts2d, boost::python::object &pts3d);
void *extract_pyarray(PyObject *x) {
    return PyObject_TypeCheck(x, &PyArray_Type) ? x : 0;
}

BOOST_PYTHON_MODULE(extract_features) {
import_array();
boost::python::converter::registry::insert( &extract_pyarray, type_id<PyArrayObject>( ) );
def("hog_feature_extract", &hog_feature_extract);
def("hog_feature_preprocess", &hog_feature_preprocess);
def("sift_feature_extract", &sift_feature_extract);
}

#pragma GCC visibility pop
#endif

这是我用来构建共享库的命令:

g++ -std=gnu++11 -lstdc++ -fPIC -shared -Wl,-z,defs -o /projects/glass_development/lib/linux/extract_features.so  extract_features/extract_features.cpp processing/sift_feature_extractor.cpp processing/hog_feature_extractor.cpp processing/pre_extraction_helper.cpp helpers/python_helper.cpp processing/base_feature_extractor.cpp -lpython2.6 -lboost_python -I/share/sw/free/boost/1.41.0/include/ -I/share/sw/free/opencv/2.4.9/include/ -I/projects/glass_development/src/native/processing/ -I/projects/glass_development/src/native/helpers/ -I/usr/include/python2.6 

我尝试为其他.cpp文件和库添加所有必需的路径,包括opencv和boost库。但它给了我一些错误,似乎它无法加载opencv库,这里是错误日志的一部分:

/tmp/ccVzk9cx.o: In function `pyopencv_to(_object const*, cv::Mat&, char const*, bool)':
python_helper.cpp:(.text+0xb54): undefined reference to `cv::Mat::Mat(int,  int const*, int, void*, unsigned long const*)'
python_helper.cpp:(.text+0xc04): undefined reference to `cv::_OutputArray::_OutputArray(cv::Mat&)'
python_helper.cpp:(.text+0xc1a): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
python_helper.cpp:(.text+0xc2d): undefined reference to `cv::transpose(cv::_InputArray const&, cv::_OutputArray const&)'

如果我在上面的命令中删除“-Wl,-z,defs”选项,它会构建.so文件而没有任何错误,但是当我想在python脚本中导入它时,它无法加载库。 / p>

我缺少什么?有什么建议吗?

这是

的输出
pkg-config --cflags --libs opencv 

命令:

-I/share/sw/free/opencv/2.4.9/include/opencv 

-I/share/sw/free/opencv/2.4.9/include

/share/sw/free/opencv/2.4.9/lib/libopencv_calib3d.so

/share/sw/free/opencv/2.4.9/lib/libopencv_contrib.so 

/share/sw/free/opencv/2.4.9/lib/libopencv_core.so

/share/sw/free/opencv/2.4.9/lib/libopencv_features2d.so 

/share/sw/free/opencv/2.4.9/lib/libopencv_flann.so 

/share/sw/free/opencv/2.4.9/lib/libopencv_gpu.so 

/share/sw/free/opencv/2.4.9/lib/libopencv_highgui.so 

/share/sw/free/opencv/2.4.9/lib/libopencv_imgproc.so 

/share/sw/free/opencv/2.4.9/lib/libopencv_legacy.so 

/share/sw/free/opencv/2.4.9/lib/libopencv_ml.so 

/share/sw/free/opencv/2.4.9/lib/libopencv_nonfree.so 

/share/sw/free/opencv/2.4.9/lib/libopencv_objdetect.so 

/share/sw/free/opencv/2.4.9/lib/libopencv_ocl.so 

/share/sw/free/opencv/2.4.9/lib/libopencv_photo.so 

/share/sw/free/opencv/2.4.9/lib/libopencv_stitching.so 

/share/sw/free/opencv/2.4.9/lib/libopencv_superres.so 

/share/sw/free/opencv/2.4.9/lib/libopencv_ts.a 

/share/sw/free/opencv/2.4.9/lib/libopencv_video.so 

/share/sw/free/opencv/2.4.9/lib/libopencv_videostab.so 

/usr/lib64/libXext.so /usr/lib64/libX11.so

 /usr/lib64/libICE.so /usr/lib64/libSM.so 

/usr/lib64/libGL.so /usr/lib64/libGLU.so 

-lcufft -lcublas -lnpps -lnppi -lnppc -lcudart -lrt -lpthread -lm -ldl  

1 个答案:

答案 0 :(得分:0)

命令行缺少opencv的链接选项,通过pkg-config添加: $(pkg-config --libs opencv)

添加此选项时,共享库正确编译:

g++ -std=gnu++11 -lstdc++ -fPIC -shared -Wl,-z,defs -o /projects/glass_development/lib/linux/extract_features.so  extract_features/extract_features.cpp processing/sift_feature_extractor.cpp processing/hog_feature_extractor.cpp processing/pre_extraction_helper.cpp helpers/python_helper.cpp processing/base_feature_extractor.cpp -lpython2.6 -lboost_python -I/share/sw/free/boost/1.41.0/include/ -I/share/sw/free/opencv/2.4.9/include/ -I/projects/glass_development/src/native/processing/ -I/projects/glass_development/src/native/helpers/ -I/usr/include/python2.6 $(pkg-config --libs opencv)