gpt4 book ai didi

python - 在 python OpenCV (cv2) 中从头开始创建轮廓

转载 作者:行者123 更新时间:2023-12-02 17:13:47 25 4
gpt4 key购买 nike

我正在尝试使用 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

关于python - 在 python OpenCV (cv2) 中从头开始创建轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20928944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com