#-*-coding:utf-8-*-ImportCv2ImportNumPy as NPdefNothing (x):Passimg= Np.zeros ((300, 512, 3), np.uint8) Cv2.namedwindow ('Image') Cv2.createtrackbar ('R','Image', 0, 255, Nothing) Cv2.createtrackbar ('G','Image', 0, 255, Nothing) Cv2.createtrackbar ('B','Image', 0, 255, nothing) switch='0:0ff\n1:0n'cv2.createtrackbar (Switch,'Image', 0, 1, nothing) whileTrue:cv2.imshow ('Image', IMG) k= Cv2.waitkey (1) & 0xFFifK = = 27: BreakR= Cv2.gettrackbarpos ('R','Image') G= Cv2.gettrackbarpos ('G','Image') b= Cv2.gettrackbarpos ('B','Image') s= cv2.gettrackbarpos (Switch,'Image') ifs = =0:img[:]=0Else: img[:]=[B, G, r]cv2.destroyallwindows ()
draw with color palette colors
#-*-coding:utf-8-*-ImportCv2ImportNumPy as NP#True When the mouse is pressedDrawing =False#If mode is true to draw the rectangle, press M to draw the curvemode =Trueix, iy=-1,-1defNothing (x):Pass#To create a callback functiondefDraw (event, X, Y, Flags, param): R= Cv2.gettrackbarpos ('R','Image') G= Cv2.gettrackbarpos ('G','Image') b= Cv2.gettrackbarpos ('B','Image') Color=(b, G, R)GlobalIX, iy, drawing, Mode#returns the starting point coordinates when the left button is pressed ifevent = =Cv2. Event_lbuttondown:drawing=True IX, iy=x, y#when the mouse is pressed and moved, the drawing can be viewed for movement, flag is pressed elifevent = = Cv2. Event_mousemove andFlags = =Cv2. Event_flag_lbutton:ifDrawing = =True:ifmode = =True:cv2.rectangle (IMG, (ix, IY), (x, y), color,-1) Else: Cv2.circle (IMG, (x, y),3, color,-1) #Stop painting When Mouse is released elifevent = =Cv2. Event_lbuttonup:drawing=falseimg= Np.zeros ((512, 512, 3), np.uint8) Cv2.namedwindow ('Image') Cv2.createtrackbar ('R','Image', 0, 255, Nothing) Cv2.createtrackbar ('G','Image', 0, 255, Nothing) Cv2.createtrackbar ('B','Image', 0, 255, Nothing) cv2.setmousecallback ('Image', Draw) whileTrue:cv2.imshow ('Image', IMG) k= Cv2.waitkey (1) & 0xFFifK = = Ord ('m'): Mode= notModeelifK = = 27: Break
Python OpenCV Palette with sliders