Reputation: 41
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
Upvotes: 2
Views: 23505
Reputation: 113814
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)
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
.
Upvotes: 3