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

Python 创建要与opencv函数一起使用的numpy数组(轮廓),python,opencv,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] 我想创建opencv轮廓以用于ex.cv2.contourArea(轮廓)。我读了

我是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]

我想创建opencv轮廓以用于ex.
cv2.contourArea(轮廓)
。我读了,但不能正确地写我的轮廓numpy数组。最好的方法是什么?

下面是一些示例代码,它首先检查从测试图像计算出的轮廓的尺寸,并生成一个测试数组,并且在这方面也取得了成功。我希望这对你有帮助

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

看起来cv2轮廓是三维numpy阵列。如果您测试出
轮廓.shape
,您将能够计算出它的尺寸。如果你想写一个兼容的numpy数组,它需要有3个维度。例如
numpy.zeros(1,2,3)
将创建一个1x2x3形状的三维零数组。。。试试看!谢谢你的回答!这是非常有用的,尽管我的问题是,我的输入不是图像,而是从文件中读取的结构轮廓点。在此基础上,我创建了二值图像,其中轮廓点为1,背景为0。在二值图像上应用cv2.findContours可以找到许多轮廓,但我知道应该只有一个。这就是为什么我想从这些点人工创建轮廓,但我不知道如何将它们放入这样的结构中:轮廓=np.array([[0,0]],[[10,0]],[[10,10]],[[5,4]])