python - 异常。TypeError : src is not a numpy array, 都不是标量

标签 python opencv

import cv2
import numpy as np

def imageMoments(img):
    #Single channel(8 bit or floating point 2D array)  
    read_original = cv2.imread(img)

    ret,thresh = cv2.threshold(img, 127, 255, 0)
    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
    cnt = contours[0]

    M = cv2.moments(cnt)
    print M

    cx = int(M[’m10’]/M[’m00’])
    cy = int(M[’m01’]/M[’m00’])
    return

我收到错误

src is not a numpy array, neither a scalar

最佳答案

cv2.threshold 需要一个灰度图像作为参数,而不是表示文件名的字符串。因此,替换:

read_original = cv2.imread(img)
ret,thresh = cv2.threshold(img, 127, 255, 0)

与:

read_original = cv2.imread(img)
imgray = cv2.cvtColor(read_original,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray, 127, 255, 0)

在原始代码中,字符串 img 作为参数传递给 threshold。在修订后的代码中,threshold 的参数改为灰度图像 imgray

关于python - 异常。TypeError : src is not a numpy array, 都不是标量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28536794/

相关文章:

python - celery worker 细节

python - 如何以 ?(a*p+b)/p**2 的形式提取字符串的 a,b 的值

python - 除了 zc.buildout 和/或 virtualenv 之外,还有其他好的替代方法来安装非 python 依赖项吗?

python - 使用 OpenCV 和 Python 提取楼层布局和阈值

python - 如何在模板匹配中使用 cv2.minMaxLoc()

opencv - OpenCV phase() 函数在这种边缘情况下是否正常工作?

python - Keras 输入数据格式

python - 可以与包装函数的参数交互的装饰器

python - 如何在 Raspberry Pi 上使用 TBB 构建 OpenCV?

c++ - 在没有 GUI 的情况下使用 OpenCV 捕获图像(在 Linux 控制台上)