gpt4 book ai didi

python - 使用python检查opencv中的轮廓区域

转载 作者:太空狗 更新时间:2023-10-29 18:24:40 26 4
gpt4 key购买 nike

我尝试在新的 python api (cv2) 中使用 checkContour() 函数,如果我使用 findContours 创建要检查的轮廓,它确实有效,例如

contours, hierarchy = cv2.findContours(imgGray, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cv2.contourArea(contours[0])

但是,当我自己创建轮廓时,以下代码不起作用

contour = numpy.array([[0,0], [10,0], [10,10], [5,4]])
area = cv2.contourArea(contour)

并返回“错误:(-215) contour.checkVector(2) >= 0 && (contour.depth() == CV_32F || contour.depth() == CV_32S) in function contourArea”

当我变成

contour = numpy.array([[0,0], [10,0], [10,10], [5,4]], dtype=numpy.int32)

我收到“错误:(-210) 由于函数 cvPointSeqFromMat 中的元素类型不合适,矩阵无法转换为点序列”

如何根据文档在 C++ 中编写以下代码

vector<Point> contour;
contour.push_back(Point2f(0, 0));
contour.push_back(Point2f(10, 0));
contour.push_back(Point2f(10, 10));
contour.push_back(Point2f(5, 4));

double area0 = contourArea(contour);

在最新的 python API (2.3) 中工作?

最佳答案

这个应该可以工作:

contour = numpy.array([[[0,0]], [[10,0]], [[10,10]], [[5,4]]])
area = cv2.contourArea(contour)

关于python - 使用python检查opencv中的轮廓区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8369547/

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