Python下使用Trackbar實(shí)現(xiàn)繪圖板
本次實(shí)驗(yàn)利用到了cv2中的createTrackbar和getTrackbarPos函數(shù)實(shí)現(xiàn)一個(gè)繪圖板的功能,用戶(hù)可以選擇矩形或是畫(huà)筆模式,并設(shè)置調(diào)色板中的值來(lái)選擇顏色,再選擇畫(huà)筆大小,進(jìn)行繪圖。除此之外,還可以用橡皮擦進(jìn)行擦除,模式同樣也分為矩形和畫(huà)筆。
下面是具體的代碼:
import cv2import numpy as npdrawing = Falsemode = Trueix, iy = -1, -1def nothing(x): passdef draw_circle(event,x,y,flags,param): r = cv2.getTrackbarPos(’R’,’image’) g = cv2.getTrackbarPos(’G’,’image’) b = cv2.getTrackbarPos(’B’,’image’) color = (b,g,r) s = cv2.getTrackbarPos(’eraser’,’image’) if s == 1: color = (255,255,255) thin = cv2.getTrackbarPos(’thin’,’image’) global ix,iy,drawing,mode if event == cv2.EVENT_LBUTTONDOWN: drawing = True ix,iy = x,y elif event == cv2.EVENT_MOUSEMOVE and flags == cv2.EVENT_FLAG_LBUTTON: if drawing == True: if mode == True: cv2.rectangle(img, (ix,iy),(x,y),color,-1) else: cv2.circle(img,(x,y),thin,color,-1) elif event == cv2.EVENT_LBUTTONUP: drawing == Falseimg = np.zeros((512,512,3), np.uint8)img[:] = 255cv2.namedWindow(’image’)cv2.createTrackbar(’R’,’image’,0,255,nothing)cv2.createTrackbar(’G’,’image’,0,255,nothing)cv2.createTrackbar(’B’,’image’,0,255,nothing)cv2.createTrackbar(’eraser’,’image’,0,1,nothing)cv2.createTrackbar(’thin’,’image’,1,50,nothing)cv2.setMouseCallback(’image’, draw_circle)while(1): cv2.imshow(’image’,img) k = cv2.waitKey(1) & 0xFF if k == ord(’m’): mode = not mode elif k == 27: break
下面是運(yùn)行的結(jié)果:
1.運(yùn)行初始界面
2.選擇顏色分別進(jìn)行矩形繪圖和畫(huà)筆繪圖,此圖中的畫(huà)筆的大小為1
3.此時(shí)的畫(huà)筆大小為15
4.使用大小為15的橡皮擦擦除面板(選擇了畫(huà)筆模式的橡皮擦)
5.使用大小為4的橡皮擦擦除面板(選擇了畫(huà)筆模式的橡皮擦)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)2. CSS可以做的幾個(gè)令你嘆為觀止的實(shí)例分享3. PHP session反序列化漏洞超詳細(xì)講解4. ASP基礎(chǔ)入門(mén)第二篇(ASP基礎(chǔ)知識(shí))5. ASP基礎(chǔ)知識(shí)Command對(duì)象講解6. Spring注入Date類(lèi)型的三種方法總結(jié)7. ASP動(dòng)態(tài)網(wǎng)頁(yè)制作技術(shù)經(jīng)驗(yàn)分享8. Xml簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理9. 解析原生JS getComputedStyle10. ASP實(shí)現(xiàn)加法驗(yàn)證碼
