2017-06-05 80 views
1

我正在使用以下代码来检测模式。为什么会抛出TypeError?TypeError:预期的整数参数,得到浮点python 2.7

# loop over the contours 

对C中的CNT:

# 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) 

类型错误:整数参数预期,得到浮

+0

opencv想要[circle center](http://docs.opencv.org/3.0-beta/modules/imgproc/doc/drawing_functions.html)具有整数坐标 – slawekwin

+0

我模仿程序的人,在他的程序中就可以。但我不。 我觉得上面的程序是合适的 –

回答

2

(cX, cY)是OpenCV的点。它代表x-y坐标,换句话说就是像素位置。如果你正在调用的函数表示它期望有一个整数,那么它期望一个整数。无论你认为它应该期待什么。

cv2.moments()返回10个花车的字典。如果你想使用它返回的值作为坐标点,那么你将需要以某种方式将它们转换为整数。

相关问题