Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a numpy ndarray as a color in OpenCV?

I am trying to pass a ndarray into this line: cv2.fillPoly(im, pts=[cnt],color=(centroid_color[0],centroid_color[1],centroid_color[2]))

centroid_color looks like this: [ 0 255 0] and is of type <type 'numpy.ndarray'>

However, I keep getting this error: TypeError: Scalar value for argument 'color' is not numeric.

How would I convert this properly?

edit: my current updated code that still gets the same error:

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]))
    r = lambda: random.randint(0,255)
    print type(centroid_color)
    cv2.fillPoly(im,cnt,centroid_color)
like image 500
BigBoy1337 Avatar asked Jan 07 '23 18:01

BigBoy1337


1 Answers

For me the only thing that worked:

self.points = np.int32(np.vstack([
    np.random.uniform(0, bounds[1], 3),
    np.random.uniform(0, bounds[0], 3)
]).T)
color = np.uint8(np.random.uniform(0, 255, 3))

c = tuple(map(int, color))
cv2.fillPoly(img, [self.points], color=c)
like image 134
mirosval Avatar answered Jan 16 '23 01:01

mirosval