有一个独特的问题,我想使用Boost Python将OpenCV的cv2.findContours()中的轮廓传递给用C编写的函数 .

我发现轮廓的类型是什么:

>>> type(contours)
<type 'list'>
>>> type(contours[0])
<type 'numpy.ndarray'>
>>> type(contours[0][0])
<type 'numpy.ndarray'>
>>> type(contours[0][0][0])
<type 'numpy.ndarray'>
>>> type(contours[0][0][0][0])
<type 'numpy.int32'>

这是我的Python代码:

import numpy as np, cv2, py_wrapper
img_file = 'an_image.png'
img = cv2.imread(img_file);
thresh = 100
canny_output = cv2.Canny(img, thresh, thresh*2, 3)
_, contours, hierarchy = cv2.findContours(canny_output, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
py_wrapper.ndarrays(contours)

我的想法是我需要将数据作为列表传递,然后内部可以使用以下内容从Numpy ndarray转换为OpenCV Mat:NumPy ndarray ⇋ OpenCV Mat conversion

和C:

void ndarrays(boost::python::list obj)
{
    NDArrayConverter cvt;
    std::cout << boost::python::len(obj) << std::endl;

    for (std::size_t i=0; i<boost::python::len(obj); ++i)
    {
        PyObject ndarray = boost::python::extract<PyObject>(obj[i]);
    }
}

但是当我运行它时,这就是我得到的:

>>> py_wrapper.ndarrays(contours)
12
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: No registered converter was able to produce a C++ rvalue of type _object from this Python object of type numpy.ndarray

任何建议/见解将不胜感激 . 谢谢 .