TypeError: Scalar value for argument 'color' is not numeric [closed]
While drawing a filled rectangle,i tried using a dynamic color set by another function,but i get the error "TypeError: Scalar value for argument 'color' is not numeric" what could be the problem. Here is my code snippet below:
color = (20,60,80) # default
def pick_color(event,x,y,flags,param):
global color
if event == cv2.EVENT_LBUTTONDOWN:
pixel = img[y,x]
print color
color = tuple(pixel)
print color
def change_image(self):
global img, color
def draw_rectangle(event,x,y,flags,param):
global refPt,color
if event == cv2.EVENT_LBUTTONDOWN:
refPt = [(x,y)]
elif event == cv2.EVENT_LBUTTONUP:
refPt.append((x,y))
print color
cv2.rectangle(image,refPt[0],refPt[1],color,cv2.FILLED) # MY ERROR EMANATES FROM HERE
print "here"
print color
cv2.imshow("image", image)
img = np.zeros((300,512,3), np.uint8)
cv2.namedWindow("color",cv2.WINDOW_NORMAL)
cv2.setMouseCallback("color",pick_color)
cv2.createTrackbar('R','color',0,255,nothing)
cv2.createTrackbar('G','color',0,255,nothing)
cv2.createTrackbar('B','color',0,255,nothing)
switch = '0 : OFF \n1 : ON'
cv2.createTrackbar(switch,'color',0,1,nothing)
image = cv2.imread("/home/nonso/Desktop/sdcard/DCIM/Camera/nonso.jpg")
clone = image.copy()
cv2.namedWindow("image",cv2.WINDOW_NORMAL)
cv2.setMouseCallback("image",draw_rectangle)
while(1):
cv2.imshow('image',image)
cv2.imshow('color',img)
k = cv2.waitKey(1) & 0xFF
if k == 27:
break
if k == ord("r"):
image = clone.copy()
r = cv2.getTrackbarPos('R','color')
g = cv2.getTrackbarPos('G','color')
b = cv2.getTrackbarPos('B','color')
s = cv2.getTrackbarPos(switch,'color')
if s == 0:
img[:] = 0
else:
img[:] = [b,g,r]
cv2.destroyAllWindows()
you're overcomplicating it. it should be simply:
(if you wrap it into another tuple, you get something like: ([1,2,3]) , which is not acceptable for the drawing call)
Thanks again @berak
I just tried out the suggestion you gave @berak,but i still get the same error
Ok i got it fixed,i think it has to do with the type returned when you make a
color=img[x,y]
assignment,when trying to call the the draw_rectangle callback,the data type was not compatible with what is needed,so i had to do thisAnd why was this answer closed in the first place?
I had the same issue. I found out the problem for anyone else looking at this.
The problem is the values in color are not integers. So you can just do this: