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/