Ask Your Question
0

Accessing ALL points of a contour

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

anandvd gravatar image

image description

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

image = cv2.imread(args["image"])
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5,5), 0)
thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]
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 flag offensive close merge delete

Comments

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.

berak gravatar imageberak ( 2017-07-26 05:45:54 -0500 )edit

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)

anandvd gravatar imageanandvd ( 2017-07-26 07:00:24 -0500 )edit

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

berak gravatar imageberak ( 2017-07-26 07:11:26 -0500 )edit

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.

anandvd gravatar imageanandvd ( 2017-07-26 07:58:46 -0500 )edit

2 answers

Sort by ยป oldest newest most voted
1

answered 2017-07-26 10:28:56 -0500

berak gravatar image

not an answer to your question, but i think, you should go back to logPolar or such:

image description

 logPolar(A,B,Point(A.cols/2,A.rows/2), 70, INTER_LINEAR );

image description

linearPolar(A,B,Point(A.cols/2,A.rows/2), A.cols/2-20, INTER_LINEAR );

image description

edit flag offensive delete link more
1

answered 2017-07-26 07:23:07 -0500

updated 2017-07-26 07:24:13 -0500

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.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

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

Seen: 1,499 times

Last updated: Jul 26 '17