我正在尝试使用opencv python接口(interface)cv2确定多边形是否凸出。从我在网上发现的所有内容来看,输入轮廓必须是每个点的float32元组的numpy数组。所以我认为它是这样工作的:

import cv2, numpy
contour = numpy.array( [
(378, 949), (375, 940), (368, 934),
(359, 932), (350, 937), (345, 955),
(351, 962), (359, 966), (368, 964),
(376, 958) ], numpy.float32 )
cv2.isContourConvex(contour)

但是我得到这个错误:
OpenCV Error: Unsupported format or combination of formats (The matrix can not be converted to point sequence because of inappropriate element type) in cvPointSeqFromMat, file /tmp/buildd/opencv-2.3.1/modules/imgproc/src/utils.cpp, line 59
谁能告诉我哪种元素类型合适,以及如何从头开始创建轮廓?

最佳答案

OpenCV希望图像的点是整数元组,只需将其类型更改为int即可:

>>> import cv2
>>> import numpy as np
>>> contour = np.array([(378, 949), (375, 940), (368, 934), (359, 932),
... (350, 937), (345, 955), (351, 962), (359, 966), (368, 964),
... (376, 958) ], dtype=np.int)
>>> cv2.isContourConvex(contour)
 True

08-25 04:37