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
答案 0 :(得分:2)
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
。