exceptions.TypeError:src不是一个numpy数组,也不是一个标量 [英] exceptions.TypeError: src is not a numpy array, neither a scalar

查看:151
本文介绍了exceptions.TypeError:src不是一个numpy数组,也不是一个标量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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需要一个灰度图像作为参数,而不是表示文件名的字符串.因此,替换为:

cv2.threshold requires a gray-scale image for an argument, not a string representing a filename. Thus, replace:

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.

In the original code, the string img is passed as an argument to threshold. In the revised code, the argument to threshold is instead a gray-scale image, imgray.

这篇关于exceptions.TypeError:src不是一个numpy数组,也不是一个标量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆