使用python在opencv中检查轮廓区域 [英] Checking contour area in opencv using python

查看:205
本文介绍了使用python在opencv中检查轮廓区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I try to use checkContour() function in new python api (cv2) and it do works if I create contours to be checked using findContours e.g.

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

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

however when I create contour on my own the following code doesn't work

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

并在函数轮廓区域中返回错误:(-215)outline.checkVector(2)> = 0&(amp.contour.depth()== CV_32F ||轮廓.depth()== CV_32S)"

and returns "error: (-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中的元素类型不合适,矩阵无法转换为点序列"

I got "error: (-210) The matrix can not be converted to point sequence because of inappropriate element type in function cvPointSeqFromMat"

如何从文档中使用C ++编写以下代码

How to make the following code in C++ from documentation

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)中工作吗?

work in latest python API (2.3)?

推荐答案

此方法应该有效:

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

这篇关于使用python在opencv中检查轮廓区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆