2

I am using the following code to detecion pattern. Why does it throw a TypeError?

# loop over the contours

for c in cnts:

# compute the center of the contour
M = cv2.moments(c)

    cX = (M["m10"] / (M["m00"] + 1e-7))
cY = (M["m01"] / (M["m00"] + 1e-7))

# draw the contour and center of the shape on the image
cv2.drawContours(frame1, [c], -1, (0, 255, 0), 2)
cv2.circle(frame1, (cX, cY), 7, (255, 255, 255), -1)
cv2.putText(frame1, "center", (cX - 20, cY - 20),
    cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)

This massage error

    cv2.circle(frame1, (cX, cY), 7, (255, 255, 255), -1)

TypeError: integer argument expected, got float

2
  • opencv wants circle center to have integer coordinates Commented Jun 5, 2017 at 7:40
  • I imitate the program people, on his program can. but I do not. I think the above program is appropriate Commented Jun 5, 2017 at 7:53

2 Answers 2

3

(cX, cY) is an OpenCV point. It represents x-y coordinates, in other words a pixel position. If the function you are calling says it expects an integer there, then it expects an integer. Whatever you think it should expect.

cv2.moments() returns a dictionary of 10 floats. If you want to use the values that it returns as coordinate points then you will need to convert them to integers somehow.

Sign up to request clarification or add additional context in comments.

Comments

0

This is because your division gives you a float value:

cX = (M["m10"] // (M["m00"] + 1e-7))
cY = (M["m01"] // (M["m00"] + 1e-7))

This will solve your problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.