使用opencv计算轮廓区域,以获得matplotlib创建的轮廓

时间:2014-09-25 13:43:53

标签: python opencv matplotlib contour area

我需要计算轮廓线限制的面积。我使用matplotlib来获取轮廓线的顶点,但我无法将它们转换为openCV中contourArea方法的有效输入:

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&&函数cv :: contourArea中的(contour.depth()== CV_32F || contour.depth()== CV_32S)

:读取行时的EOF

有人能帮帮我吗?在此先感谢!!!

1 个答案:

答案 0 :(得分:0)

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

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