我正在使用以下代码来检测模式。为什么会抛出类型错误?
# loop over the contours
对于 cnts 中的 c:
# 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)
这个按摩错误
cv2.circle(frame1, (cX, cY), 7, (255, 255, 255), -1)
类型错误:需要整数参数,但得到了 float
最佳答案
(cX, cY)
是一个 OpenCV 点。它代表x-y坐标,换句话说就是像素位置。如果您调用的函数说它需要一个整数,那么它需要一个整数。无论您认为它应该期待什么。
cv2.moments()
返回 10 个 float 的字典。如果您想使用它返回的值作为坐标点,那么您需要以某种方式将它们转换为整数。
关于python-2.7 - 类型错误 : integer argument expected, 得到 float python 2.7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44363967/