aku putri
aku putri

Reputation: 101

TypeError: integer argument expected, got float python 2.7

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

Upvotes: 2

Views: 1571

Answers (2)

ankit desai
ankit desai

Reputation: 1

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.

Upvotes: 0

BoarGules
BoarGules

Reputation: 16942

(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.

Upvotes: 3

Related Questions