Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create numpy array (contour) to be used with opencv functions

Tags:

python

opencv

I'm new to python and I don't know how to create numpy array that may be used in opencv functions. I've got two vectors defined as follows:

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

and the result is:

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

I want to create opencv contour to be used in ex. cv2.contourArea(contour). I read Checking contour area in opencv using python but cannot write my contour numpy array properly. What's the best way to do that?

like image 503
Biba Avatar asked Sep 14 '25 02:09

Biba


1 Answers

Here's some example code, that first checks the dimensions of a contour calculated from a test image, and makes a test array and has success with that as well. I hope this help you!

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
like image 141
Sam Avatar answered Sep 16 '25 17:09

Sam