当前位置:   article > 正文

C/C++代码封装为Python模块(含OpenCV-Mat转换)_pbcvt

pbcvt

准备资料

Ubuntu16.04或者其他版本

Python2.x或者Python3.x

boost_1_69_0或者其他版本 // https://www.boost.org/users/history/version_1_69_0.html

OpenCV3.4.3或者其他版本 // https://www.opencv.org/releases.html

pyboostcvconverter(大佬写的python boost opencv的转换器) // https://github.com/Algomorph/pyboostcvconverter

配置环境

安装Python

sudo apt install python3-dev build-essential

安装C++ boost

cd boost_1_69_0

./bootstrap.sh --with-python=python3.5 # python3.6->3.6

./b2

sudo ./b2 install

cmake pyboostcvconverter

在pyboostcvconverter目录下新建build文件夹

运行cmake-gui

where is source code: pyboostcvconverter根目录

where to build the binaries: pyboostcvconverter根目录刚新建的build目录

Configure && Generate

到此,cmake完成。就可以开始转换封装了。

开始封装

在pyboostcvconverter/src目录下有4个文件:

pyboost_cv2_converter.cpp  pyboost_cv3_converter.cpp  pyboost_cv4_converter.cpp  python_module.cpp

到此就可以修改python_module.cpp来增加自己的C/C++代码(这边增加两个C++函数封装供python调用)代码如下:

cv::Mat to_gray(cv::Mat srcMat); // 转灰度

cv::Mat to_binary(cv::Mat srcMat, int t, int max);// 二值化

  1. #define PY_ARRAY_UNIQUE_SYMBOL pbcvt_ARRAY_API
  2. #include <boost/python.hpp>
  3. #include <pyboostcvconverter/pyboostcvconverter.hpp>
  4. #include <opencv2/opencv.hpp>
  5. namespace pbcvt {
  6. using namespace boost::python;
  7. /**
  8. * @brief Example function. Basic inner matrix product using explicit matrix conversion.
  9. * @param left left-hand matrix operand (NdArray required)
  10. * @param right right-hand matrix operand (NdArray required)
  11. * @return an NdArray representing the dot-product of the left and right operands
  12. */
  13. PyObject *dot(PyObject *left, PyObject *right) {
  14. cv::Mat leftMat, rightMat;
  15. leftMat = pbcvt::fromNDArrayToMat(left);
  16. rightMat = pbcvt::fromNDArrayToMat(right);
  17. auto c1 = leftMat.cols, r2 = rightMat.rows;
  18. // Check that the 2-D matrices can be legally multiplied.
  19. if (c1 != r2) {
  20. PyErr_SetString(PyExc_TypeError,
  21. "Incompatible sizes for matrix multiplication.");
  22. throw_error_already_set();
  23. }
  24. cv::Mat result = leftMat * rightMat;
  25. PyObject *ret = pbcvt::fromMatToNDArray(result);
  26. return ret;
  27. }
  28. /**
  29. * @brief Example function. Simply makes a new CV_16UC3 matrix and returns it as a numpy array.
  30. * @return The resulting numpy array.
  31. */
  32. PyObject* makeCV_16UC3Matrix(){
  33. cv::Mat image = cv::Mat::zeros(240,320, CV_16UC3);
  34. PyObject* py_image = pbcvt::fromMatToNDArray(image);
  35. return py_image;
  36. }
  37. //
  38. /**
  39. * @brief Example function. Basic inner matrix product using implicit matrix conversion.
  40. * @details This example uses Mat directly, but we won't need to worry about the conversion in the body of the function.
  41. * @param leftMat left-hand matrix operand
  42. * @param rightMat right-hand matrix operand
  43. * @return an NdArray representing the dot-product of the left and right operands
  44. */
  45. cv::Mat dot2(cv::Mat leftMat, cv::Mat rightMat) {
  46. auto c1 = leftMat.cols, r2 = rightMat.rows;
  47. if (c1 != r2) {
  48. PyErr_SetString(PyExc_TypeError,
  49. "Incompatible sizes for matrix multiplication.");
  50. throw_error_already_set();
  51. }
  52. cv::Mat result = leftMat * rightMat;
  53. return result;
  54. }
  55. /**
  56. * \brief Example function. Increments all elements of the given matrix by one.
  57. * @details This example uses Mat directly, but we won't need to worry about the conversion anywhere at all,
  58. * it is handled automatically by boost.
  59. * \param matrix (numpy array) to increment
  60. * \return
  61. */
  62. cv::Mat increment_elements_by_one(cv::Mat matrix){
  63. matrix += 1.0;
  64. return matrix;
  65. }
  66. // Edit by kaychan 2019-03-05
  67. cv::Mat to_gray(cv::Mat srcMat) { // 在此添加你的C/C++代码
  68. cv::Mat gray;
  69. cv::cvtColor(srcMat, gray, COLOR_BGR2GRAY);
  70. return gray;
  71. }
  72. cv::Mat to_binary(cv::Mat srcMat, int t, int max) {
  73. cv::Mat thresh;
  74. cv::threshold(srcMat, thresh, t, max, 0);
  75. return thresh;
  76. }
  77. #if (PY_VERSION_HEX >= 0x03000000)
  78. static void *init_ar() {
  79. #else
  80. static void init_ar(){
  81. #endif
  82. Py_Initialize();
  83. import_array();
  84. return NUMPY_IMPORT_ARRAY_RETVAL;
  85. }
  86. BOOST_PYTHON_MODULE (pbcvt) {
  87. //using namespace XM;
  88. init_ar();
  89. //initialize converters
  90. to_python_converter<cv::Mat,pbcvt::matToNDArrayBoostConverter>();
  91. matFromNDArrayBoostConverter();
  92. //expose module-level functions
  93. def("dot", dot);
  94. def("dot2", dot2);
  95. def("makeCV_16UC3Matrix", makeCV_16UC3Matrix);
  96. //from PEP8 (https://www.python.org/dev/peps/pep-0008/?#prescriptive-naming-conventions)
  97. //"Function names should be lowercase, with words separated by underscores as necessary to improve readability."
  98. def("increment_elements_by_one", increment_elements_by_one);
  99. def("to_gray", to_gray); // 在此声明你的C/C++代码
  100. def("to_binary", to_binary);
  101. }
  102. } //end namespace pbcvt

代码编写完成后,回到pyboostcvconverter目录,运行:

cmake .

make

这时候就会在当前目录下生成pbcvt.cpython-36m-x86_64-linux-gnu.so,就可以使用python import这个模块,然后使用python调用C/C++函数。

开始调用

到此可以使用python调用刚才编写的C/C++代码了,如下图:

可以看到,to_gray,to_binary函数已经调用成功了。到此,可以根据自己的需求进行封装调用了···

更多

在Windows下cmake pyboostcvconverter:

打开cmd运行bootstrap.bat

b2 --with-python

b2 install

打开cmake添加entry:add entry:BOOST_ROOT=your_boost_root

到此就可以利用VS编译了。

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/555663
推荐阅读
相关标签
  

闽ICP备14008679号