将numpy.ndarray转换为opencv cv::Mat

Converting numpy.ndarray into opencv cv::Mat

本文关键字:cv Mat opencv numpy ndarray 转换      更新时间:2023-10-16

我有用于OpenCv C++Boost-Python接口。当我从 python 调用C++方法时,我收到此错误:

Boost.Python.ArgumentError: Python argument types in
    Vision.process(Vision, numpy.ndarray, numpy.ndarray)
did not match C++ signature:
    process(python::vision::PythonVision {lvalue}, cv::Mat {lvalue}, cv::Mat {lvalue})

我正在使用python cv2.so 模块。如何将numpy.ndarray转换为cv::mat?

我使用了以下项目:https://github.com/Algomorph/pyboostcvconverter并静态链接到它。

注意(为避免分段错误):

1) PY_ARRAY_UNIQUE_SYMBOL应该在调用import_array的位置定义在包含 的其他地方,请使用NO_IMPORT_ARRAY

#define PY_ARRAY_UNIQUE_SYMBOL PYVISION_ARRAY_API
#include <pyboostcvconverter/pyboostcvconverter.hpp>

2) 从BOOST_PYTHON_MODULE调用init_ar

/**
 * @brief Initialize Numpy Array
 */
static void init_ar( )
{
    // initialize
    Py_Initialize();
    // defined in numpy
    import_array();
}