Python OpenCV Palette with sliders

Source: Internet
Author: User

#-*-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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.