本文實例講述了Python影像處理之簡單畫板實作方法。分享給大家供大家參考,具體如下:
Python影像處理也是依賴opencv的Python介面實現的,Python語言簡單易懂,簡潔明了。本次實作畫板塗鴉,一個是在裡面畫矩形,還有畫線。其他也都可以擴展,本案例只做例程,思路是對滑鼠事件的處理,以及滾動條調節顏色處理。滑鼠事件就包含左鍵按下,以及釋放事件的處理。
import cv2 import numpy as np # null function def nothing(x): pass Drawing = False Mode = True IX,IY = -1,-1 def drawCircle(Event,X,Y,Flags,Param): R = cv2.getTrackbarPos('R','Image') G = cv2.getTrackbarPos('G','Image') B = cv2.getTrackbarPos('B','Image') #get color value Color = (B,G,R); 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),3,Color,-1); elif Event == cv2.EVENT_LBUTTONUP: Drawing = False #create image with 3 chanels Img = np.zeros((660,660,3),np.uint8) #create window cv2.namedWindow('Image') #create track bar, range for 0~255 cv2.createTrackbar('R','Image',0,255,nothing) cv2.createTrackbar('G','Image',0,255,nothing) cv2.createTrackbar('B','Image',0,255,nothing) #set mouse ack cv2.setMouseCallback('Image',drawCircle) while(1): cv2.imshow('Image',Img) k = cv2.waitKey(10)&0xFF #switch draw mode if k == ord('m'): Mode = not Mode elif k == 27: break #you must destroy all of sources cv2.destroyAllWindows()
最後的效果圖如下:
################################################################################################相關學習推薦:python影片教學
以上是Python影像處理簡單畫板實作方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!