python-2.7 - 类型错误 : integer argument expected, 得到 float python 2.7

标签 python-2.7 opencv3.0

我正在使用以下代码来检测模式。为什么会抛出类型错误?

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

相关文章:

c++ - 在 eclipse 上使用 opencv 3.0 时出现链接错误

python - 在 vim 中安装 YouCompleteMe 时遇到 python 问题

python - 使用 matplotlib 绘制两种不同的 SVM 方法有问题吗?

python - 根据列表中的值过滤数据框

Python readline() 在尝试读取大型(~ 13GB)csv 文件时失败

python - 无法在 Ubuntu 16.04(主机)上使用 OpenCV 中的集成网络摄像头

python-2.7 - 获取 celery 组结果

c++ - opencv 错误 : GTK+ 2. 检测到 x 符号。不支持在同一进程中使用 GTK+ 2.x 和 GTK+ 3

python - Opencv:霍夫线不显示

numpy - python opencv2,错误的摄像头界面,仅cam light