创建与opencv函数一起使用的numpy数组(轮廓)

时间:2016-07-27 09:57:49

标签: python opencv

我是python的新手,我不知道如何创建可以在opencv函数中使用的numpy数组。 我有两个向量定义如下:

X=np.array(x_list)
Y=np.array(y_list)

结果是:

[ 250.78  250.23  249.67 ...,  251.89  251.34  250.78]
[ 251.89  251.89  252.45 ...,  248.56  248.56  251.89]

我想创建在ex中使用的opencv轮廓。 cv2.contourArea(contour)。我读了Checking contour area in opencv using python但是无法正确编写轮廓numpy数组。最好的方法是什么?

1 个答案:

答案 0 :(得分:0)

这里是一些示例代码,它首先检查从测试图像计算出的轮廓的尺寸,并制作测试数组并且也是成功的。我希望这能帮到你!

import cv2
import numpy as np

img = cv2.imread('output6.png',0) #read in a test image
ret,thresh = cv2.threshold(img,127,255,0)
im2,contours,hierarchy = cv2.findContours(thresh, 1, 2)

cnt = contours[0]

print cnt.shape #this contour is a 3D numpy array
print cv2.contourArea(cnt) #the function prints out the area happily

#######Below is the bit you asked about

contour = np.array([[[0,0]], [[10,0]], [[10,10]], [[5,4]]]) #make a fake array
print cv2.contourArea(contour) #also compatible with function