创建的轮廓的轮廓面积

创建的轮廓的轮廓面积

本文介绍了使用 opencv 计算由 matplotlib 创建的轮廓的轮廓面积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要计算由等高线限制的区域.我使用 matplotlib 来获取等高线的顶点,但我无法将它们转换为 openCV 中的 contourArea 方法的有效输入:

I need to calculate the area limited by a contour line. I use matplotlib to get the vertices of the contour line, but I am not able to convert them into a valid input for contourArea method in openCV:

Z = z_func(X, Y, Ql, k[i,j], B)
cs = plt.contour(X, Y, Z,[IncT])
v = cs.collections[0].get_paths()[0].vertices
xy = []
for vv in v:
    xy.append(vv[0])
cnt = np.array(xy)
area = cv2.contourArea(cnt)

我收到此错误:......\opencv-2.4.9.1\modules\imgproc\src\contours.cpp:1904: 错误:(-215)contour.checkVector(2) >= 0 &&(contour.depth() == CV_32F || contour.depth() == CV_32S) 在函数 cv::contourArea

I get this error:......\opencv-2.4.9.1\modules\imgproc\src\contours.cpp:1904: error: (-215) contour.checkVector(2) >= 0 && (contour.depth() == CV_32F || contour.depth() == CV_32S) in function cv::contourArea

:读取一行时的EOF

有人可以帮我吗?提前致谢!!!

Could anyone help me? Thanks in advance!!!

推荐答案

最后,我没有使用 cv2.contourArea 方法.只获取顶点的 Y 分量,将它们的绝对值相加并乘以网格大小:

Finally, I didn't use cv2.contourArea method. Only get the Y component of vertices, sum their absolute values and multiply by the grid size:

x = arange(-1.0,10.0,0.05)
y = arange(-1.0,1.0,0.05)

X,Y = meshgrid(x, y) # grid of point

cs = plt.contour(X, Y, Z,[IncT])
p = cs.collections[0].get_paths()[0]
v = p.vertices
y = v[:,1]
s[i,j]=sum(abs(y))*0.05

这篇关于使用 opencv 计算由 matplotlib 创建的轮廓的轮廓面积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 06:13