如何在c++文件中使用opencv函数并将其与Python绑定
How to use opencv functions in C++ file and bind it with Python?
我想传递一个图像从python脚本到c++代码的opencv计算。为了将两者结合起来,我遵循了这一点。绑定工作得很好,但是当我使用任何opencv内置函数时,它会给我错误。
Traceback(最近一次调用):文件"/home/prashant/Desktop/example.py",第1行导入PBCVT #您的模块,以及您编译的动态库文件的名称(无扩展名)ImportError:/usr/local/lib/python2.7/dist-packages/pbcvt。so: undefined symbol: _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
这可能是你必须编辑CMakeLists.txt在第27行find_package(OpenCV COMPONENTS core REQUIRED)
和添加更多的组件,如find_package(OpenCV COMPONENTS core imgproc highgui REQUIRED)
相关文章:
- 将自由函数绑定为类成员函数
- 有没有办法将重载的类函数绑定到函数对象?
- 如何将类 1 的 std::函数绑定到类 2 的函数?
- 如何将 STL 队列推送函数绑定到 std::函数?
- 如何在 C# 中将带有参数的函数绑定到包中
- 如何仅使用 c/python API 将 c++ 成员函数绑定到 python?
- 将非静态函数绑定到回调时出现问题
- 意外输出..函数绑定在虚拟表中的发生方式
- 将类实例函数绑定到 v8::FunctionTemplate
- 是否可以将匿名 lambda 函数绑定到对象以允许 lambda 中的代码访问对象的成员?
- 使用JNI将C 类成员函数绑定到Java
- 将静态成员函数绑定为回调
- 如何使用 boost::bind 将静态成员函数绑定到 boost::function
- 如何将C++函数绑定到xaml文本块属性
- boost::使用成员函数绑定回调的帮助
- 如何将成员函数绑定到 C++14 中的对象
- std::函数绑定到成员函数
- 将带有unique_ptr参数的函数绑定到 std::function<void()>
- 将常规函数绑定到 std::function
- std::将成员函数绑定到 nullptr 处的实例,导致看似随机的 this 指针