Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

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()