Accessing ALL points of a contour I made the exterior contour using the following:

def contoursConvexHull(contours):
pts = []
for i in range(0, len(contours)):
for j in range(0, len(contours[i])):
pts.append(contours[i][j])

pts = np.array(pts)
result = cv2.convexHull(pts)
return result

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5,5), 0)
thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)
imageCanny = cv2.Canny(blurred, 100, 200, 3)

img, contours, hierarchy = cv2.findContours(imageCanny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

ConvexHullPoints = contoursConvexHull(contours)
cv2.polylines(image, [ConvexHullPoints], True, (0,255,255), 2)

and accessed the center of the contour using the following:

c = max(contours, key = cv2.contourArea)
M = cv2.moments(c)
if M["m00"] != 0:
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
print (cX,cY)
else:
cX, cY = 0,0
print (cX,cY)

My questions: 1. Since my image is not continuous, am I right in using convexHull and polylines() to draw the contour? 2. If I am right in using the above functions, how can I access ALL the points on the convexHull contour? Currently, if I use

cpts = []
cpts.append(ConvexHullPoints)
cpts = np.array(cpts)
print (cpts)

to see the points, I only get an array of 35 points.

edit retag close merge delete

your convex hull just has a few points (the "corners", if you want so.) looking at it, i'd even expect less than 35, more like 12 points.

why do you think, there should be more ? again, it's the hull, not a contour.

Thanks for the comment! What confuses me, is that since all points on the image are not connected, how can I make a contour surrounding just the image? (similar to what the hull gives, but a contour)

sorry, i don't understand, what you're trying to achieve here.

I am trying to see all the black doublets in a straight line. I have tried using toPolar() but it gives me a distorted image like this. Since this approach didn't work well, I am now trying to extract each doublet (which will be contained in a "pizza slice" like shape, centered at the center of the image) and place them next to each other.

Sort by » oldest newest most voted

not an answer to your question, but i think, you should go back to logPolar or such: logPolar(A,B,Point(A.cols/2,A.rows/2), 70, INTER_LINEAR ); linearPolar(A,B,Point(A.cols/2,A.rows/2), A.cols/2-20, INTER_LINEAR ); more

In your findContours call, notice the flag cv2.CHAIN_APPROX_SIMPLE. This is used by OpenCV to approximate some points that can be reduced, and avoid outputting every single contour point.

If you want OpenCV to not do this, change that to cv2.CHAIN_APPROX_NONE. The resulting list will contain every contour point, instead of the reduced approximation.

more

Stats

Asked: 2017-07-26 05:23:19 -0500

Seen: 1,499 times

Last updated: Jul 26 '17