Python exceptions.TypeError: src 不是一个 numpy 数组,也不是一个标量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28536794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
exceptions.TypeError: src is not a numpy array, neither a scalar
提问by Anjali Changlani
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
I get the error
我收到错误
src is not a numpy array, neither a scalar
回答by John1024
cv2.threshold
requires a gray-scale imagefor an argument, not a string representing a filename. Thus, replace:
cv2.threshold
参数需要灰度图像,而不是表示文件名的字符串。因此,替换:
read_original = cv2.imread(img)
ret,thresh = cv2.threshold(img, 127, 255, 0)
With:
和:
read_original = cv2.imread(img)
imgray = cv2.cvtColor(read_original,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray, 127, 255, 0)
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
.
在原始代码中,字符串img
作为参数传递给threshold
. 在修改后的代码中, 的参数threshold
是灰度图像,imgray
。