本文实例讲述了python图像处理之简单画板实现方法。分享给大家供大家参考,具体如下:
python图像处理也是依赖opencv的python接口实现的,python语言简单易懂,简洁明了。本次实现画板涂鸦,一个是在里面画矩形,还有画线。其他也都可以扩展,本案例只做例程,思路是对鼠标事件的处理,以及滚动条调节颜色处理。鼠标事件就包含有左键按下,以及释放事件的处理。
import cv2import numpy as np# null functiondef nothing(x): passdrawing = falsemode = trueix,iy = -1,-1def 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 chanelsimg = np.zeros((660,660,3),np.uint8)#create windowcv2.namedwindow('image')#create track bar, range for 0~255cv2.createtrackbar('r','image',0,255,nothing)cv2.createtrackbar('g','image',0,255,nothing)cv2.createtrackbar('b','image',0,255,nothing)#set mouse ackcv2.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 sourcescv2.destroyallwindows()
最后的效果图如下:
相关学习推荐:python视频教程
以上就是python图像处理之简单画板实现方法的详细内容。