您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“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”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。