【发布时间】:2016-09-08 12:37:02
【问题描述】:
我想将图像从 python 脚本传递到 c++ 代码以进行 opencv 计算。为了绑定这两个我关注了this。绑定工作正常但是当我使用任何 opencv 内置函数时,它给了我错误。
Traceback(最近一次调用最后一次): 文件“/home/prashant/Desktop/example.py”,第 1 行,在 import pbcvt # 你的模块,也是你编译的动态库文件的名称,不带扩展名 ImportError:/usr/local/lib/python2.7/dist-packages/pbcvt.so:未定义符号:_ZN2cv8cvtColorERKNS_11_InputArrayERKNS_12_OutputArrayEii
我正在使用 opencv 3.1 和 python 2.7。非常感谢任何帮助/指导。`
代码供参考。 Python 文件。
import pbcvt
import numpy as np
import cv2
a = cv2.imread("/home/prashant/Documents/opencv-practice/screenshot.png")
c = pbcvt.dot(a)
cv2.imshow("gray image",c)
cv2.waitKey(0)
cv2.destroyAllWindows()
C++ 代码:
#include <pyboostcvconverter/pyboostcvconverter.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
namespace pbcvt {
using namespace std;
using namespace boost::python;
cv::Mat dot(PyObject *image) {
cv::Mat matImage, greyMat;
matImage = pbcvt::fromNDArrayToMat(image);
cv::cvtColor(matImage, greyMat, CV_BGR2GRAY);
return greyMat;
}
cv::Mat dot2(cv::Mat leftMat, cv::Mat rightMat) {
auto c1 = leftMat.cols, r2 = rightMat.rows;
if (c1 != r2) {
PyErr_SetString(PyExc_TypeError,
"Incompatible sizes for matrix multiplication.");
throw_error_already_set();
}
cv::Mat result = leftMat * rightMat;
return result;
}
#if (PY_VERSION_HEX >= 0x03000000)
static void *init_ar() {
#else
static void init_ar(){
#endif
Py_Initialize();
import_array();
return NUMPY_IMPORT_ARRAY_RETVAL;
}
BOOST_PYTHON_MODULE (pbcvt) {
//using namespace XM;
init_ar();
//initialize converters
to_python_converter<cv::Mat,
pbcvt::matToNDArrayBoostConverter>();
pbcvt::matFromNDArrayBoostConverter();
//expose module-level functions
def("dot", dot);
def("dot2", dot2);
}
} //end namespace pbcvt
【问题讨论】: