1 | initial version |
Refer to the Python documentation/tutorial.
When dealing with documentations in general, you want to understand the underlying use then figure out a way of doing it in another language. For this case for instance, in the C++ API you have to define the type. But in Python, this is not the case. This achieves the same task as the C++ version.
image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
Now the contours are stored inside contours
2 | No.2 Revision |
Refer to the Python documentation/tutorial.
When dealing with documentations in general, you want to understand the underlying use then figure out a way of doing it in another language. For this case for instance, in the C++ API you have to define the type. But in Python, this is not the case. This achieves the same task as the C++ version.
image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
Now the contours are stored inside contours
EDIT I
A couple things.
With that said, here's a small code snippet that addresses your problem and the comments should be self-explanatory.
# findContours returns a tuple
# The first index of this tuple is the image
# Second one holds the actual contours
# The last one gives the respective hierarchy of the contours
image, cnts, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
# from all the contours, extract the largest based off of the area
c = max(cnts, key=cv2.contourArea)
# this will show you that it is an n-dimensional array
print 'Type of c', type(c)
print type(c[0]) # recall that this is a n-dimensional array so we
# have accessed the first row
print c[0][0] # for this row, extract the first column.
# Here is were you have accessed the x, y coordinates.
# Normally, you want this to be stored as a tuple
# to preserve that beautiful (x, y) notation
point = tuple(c[0][0])
x, y = point # there are your coordinates
# you could also do point[0], point[1] to access the x and y respectively.
print x, y
Happy coding!
Cheers :)