TypeError: integer argument expected, got float python 2.7

Viewed 1546

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 Answers

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.

Related