BigBoy1337
BigBoy1337

Reputation: 5023

TypeError: Scalar value for argument 'color' is not numeric in openCV

Here is my code

im = cv2.imread('luffy.jpg')
gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,127,255,0)

contours,h = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

for cnt in contours:
    moment = cv2.moments(cnt)
    c_y = moment['m10']/(moment['m00']+0.01)
    c_x = moment['m01']/(moment['m00']+0.01)
    centroid_color = im[c_x,c_y]
    centroid_color = np.array((centroid_color[0],centroid_color[1],centroid_color[2]))
    print type(centroid_color)
    cv2.fillPoly(im,cnt,centroid_color)

I am getting that error on the last line where I try and pass centroid_color into the color argument. It is <type 'numpy.ndarray'> and I have been able to successfully pass this data type into cv2.fillPoly as the color in other instances, so I am not sure why it is having a problem here.

Upvotes: 3

Views: 20710

Answers (2)

Omid Attarnezhad
Omid Attarnezhad

Reputation: 151

centroid_color = tuple ([int(x) for x in centroid_color])

Upvotes: 8

JD Forster
JD Forster

Reputation: 175

fillPoly() expects an iterable, i.e. put the cnt in brackets. I believe it is a duplicate of https://stackoverflow.com/a/17582850/5818240.

Moreover, your centroid color variable contains strings. You need to convert them to integers.

centroid_color = np.array((int(centroid_color[0]),int(centroid_color[1]),int(centroid_color[2])))

Upvotes: 4

Related Questions