TypeError: Scalar value for argument 'color' is not numeric [closed]

asked 2018-02-25 00:19:38 -0600

Nonso gravatar image

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()
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by eshirima
close date 2018-02-25 09:10:24.833576

Comments

you're overcomplicating it. it should be simply:

color = img[y,x]

(if you wrap it into another tuple, you get something like: ([1,2,3]) , which is not acceptable for the drawing call)

berak gravatar imageberak ( 2018-02-25 02:56:53 -0600 )edit

Thanks again @berak

Nonso gravatar imageNonso ( 2018-02-25 04:00:39 -0600 )edit

I just tried out the suggestion you gave @berak,but i still get the same error

Nonso gravatar imageNonso ( 2018-02-25 14:20:49 -0600 )edit

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 this

def draw_rectangle(event,x,y,flags,param):
    global refPt,drawing,color
    if event == cv2.EVENT_LBUTTONDOWN:
        refPt = [(x,y)]
        drawing = True
    elif event == cv2.EVENT_LBUTTONUP:
        color = np.array((np.asscalar(np.int16(color[0])),np.asscalar(np.int16(color[1])),np.asscalar(np.int16(color[2]))))#HERE
        print color
        refPt.append((x,y))
        drawing = False
        cv2.rectangle(image,refPt[0],refPt[1],color,cv2.FILLED)
        # print color
        # print type(color
Nonso gravatar imageNonso ( 2018-02-26 01:01:34 -0600 )edit

And why was this answer closed in the first place?

Nonso gravatar imageNonso ( 2018-02-26 01:02:01 -0600 )edit

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:

 def function(bla):
    ...
    color = tuple([int(x) for x in color]) 
    ..
Akai gravatar imageAkai ( 2018-11-09 19:05:30 -0600 )edit