opencv如何实现鼠标动作GUI
这篇文章主要为大家展示了“opencv如何实现鼠标动作GUI”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“opencv如何实现鼠标动作GUI”这篇文章吧。
创新互联公司网站建设由有经验的网站设计师、开发人员和项目经理组成的专业建站团队,负责网站视觉设计、用户体验优化、交互设计和前端开发等方面的工作,以确保网站外观精美、成都网站制作、成都网站设计易于使用并且具有良好的响应性。
显示opencv所支持的事件:
import cv2 as cv events = [i for i in dir(cv) if 'EVENT' in i]print(events)# 打印结果如下:""" [ 'EVENT_FLAG_ALTKEY', 'EVENT_FLAG_CTRLKEY', 'EVENT_FLAG_LBUTTON', 'EVENT_FLAG_MBUTTON', 'EVENT_FLAG_RBUTTON', 'EVENT_FLAG_SHIFTKEY', 'EVENT_LBUTTONDBLCLK', 'EVENT_LBUTTONDOWN', 'EVENT_LBUTTONUP', 'EVENT_MBUTTONDBLCLK', 'EVENT_MBUTTONDOWN', 'EVENT_MBUTTONUP', 'EVENT_MOUSEHWHEEL', 'EVENT_MOUSEMOVE', 'EVENT_MOUSEWHEEL', 'EVENT_RBUTTONDBLCLK', 'EVENT_RBUTTONDOWN', 'EVENT_RBUTTONUP' ] """
控制台输出结果:
Windows PowerShell 版权所有 (C) Microsoft Corporation。保留所有权利。 尝试新的跨平台 PowerShell https://aka.ms/pscore6 加载个人及系统配置文件用了 879 毫秒。 (base) PS C:\Users\chenxuqi\Desktop\News4cxq\test4cxq\opencv4cxq> conda activate ssd4pytorch2_2_0 (ssd4pytorch2_2_0) PS C:\Users\chenxuqi\Desktop\News4cxq\test4cxq\opencv4cxq> & 'D:\Anaconda3\envs\ssd4pytorch2_2_0\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2020.12.424452561\pythonFiles\lib\python\debugpy\launcher' '57123' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\test4cxq\opencv4cxq\test7.py' ['EVENT_FLAG_ALTKEY', 'EVENT_FLAG_CTRLKEY', 'EVENT_FLAG_LBUTTON', 'EVENT_FLAG_MBUTTON', 'EVENT_FLAG_RBUTTON', 'EVENT_FLAG_SHIFTKEY', 'EVENT_LBUTTONDBLCLK', 'EVENT_LBUTTONDOWN', 'EVENT_LBUTTONUP', 'EVENT_MBUTTONDBLCLK', 'EVENT_MBUTTONDOWN', 'EVENT_MBUTTONUP', 'EVENT_MOUSEHWHEEL', 'EVENT_MOUSEMOVE', 'EVENT_MOUSEWHEEL', 'EVENT_RBUTTONDBLCLK', 'EVENT_RBUTTONDOWN', 'EVENT_RBUTTONUP'] (ssd4pytorch2_2_0) PS C:\Users\chenxuqi\Desktop\News4cxq\test4cxq\opencv4cxq>
绘制蓝色小圆点:
import numpy as npimport cv2 as cv# mouse callback functiondef draw_circle(event,x,y,flags,param):if event == cv.EVENT_LBUTTONDBLCLK:cv.circle(img,(x,y),10,(255,0,0),-1)# Create a black image, a window and bind the function to windowimg = np.zeros((512,512,3), np.uint8)cv.namedWindow('image-LinMaZi') # 创建一个窗口,并指定名字cv.setMouseCallback('image-LinMaZi',draw_circle) # 为指定的窗口设定鼠标句柄while True:cv.imshow('image-LinMaZi',img)if cv.waitKey(20) & 0xFF == 27: # 按下Esc键退出程序breakcv.destroyAllWindows()
程序运行结果截图展示:
绘制矩形或者圆形:
import numpy as npimport cv2 as cv drawing = False # true if mouse is pressed # 用来指明当前鼠标左键是否保持按下mode = True # if True, draw rectangle. Press 'm' to toggle to curveix,iy = -1,-1 # 按下左键时鼠标的位置# mouse callback functiondef draw_circle_rectangle(event,x,y,flags,param):global ix,iy,drawing,modeif event == cv.EVENT_LBUTTONDOWN:drawing = Trueix,iy = x,yelif event == cv.EVENT_MOUSEMOVE:if drawing == True:if mode == True:cv.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)else:cv.circle(img,(x,y),5,(0,0,255),-1)elif event == cv.EVENT_LBUTTONUP:drawing = Falseif mode == True:cv.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)else:cv.circle(img,(x,y),5,(0,0,255),-1)elif event == cv.EVENT_RBUTTONDOWN:mode = not mode img = np.zeros((512,512,3), np.uint8)cv.namedWindow('LinMaZi')cv.setMouseCallback('LinMaZi',draw_circle_rectangle)while True:cv.imshow('LinMaZi',img)k = cv.waitKey(1) & 0xFFif k == ord('m') or k == ord('M'):mode = not modeelif k == 27:breakcv.destroyAllWindows()
程序运行结果截图展示:
以上是“opencv如何实现鼠标动作GUI”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!
文章名称:opencv如何实现鼠标动作GUI
网页网址:http://azwzsj.com/article/jgeics.html