您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“opencv如何實(shí)現(xiàn)鼠標(biāo)動作GUI”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“opencv如何實(shí)現(xiàn)鼠標(biāo)動作GUI”這篇文章吧。
顯示opencv所支持的事件:
import cv2 as cv events = [i for i in dir(cv) if 'EVENT' in i]print(events)# 打印結(jié)果如下:""" [ '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' ] """
控制臺輸出結(jié)果:
Windows PowerShell 版權(quán)所有 (C) Microsoft Corporation。保留所有權(quán)利。 嘗試新的跨平臺 PowerShell https://aka.ms/pscore6 加載個人及系統(tǒng)配置文件用了 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>
繪制藍(lán)色小圓點(diǎn):
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') # 創(chuàng)建一個窗口,并指定名字cv.setMouseCallback('image-LinMaZi',draw_circle) # 為指定的窗口設(shè)定鼠標(biāo)句柄while True:cv.imshow('image-LinMaZi',img)if cv.waitKey(20) & 0xFF == 27: # 按下Esc鍵退出程序breakcv.destroyAllWindows()
程序運(yùn)行結(jié)果截圖展示:
繪制矩形或者圓形:
import numpy as npimport cv2 as cv drawing = False # true if mouse is pressed # 用來指明當(dāng)前鼠標(biāo)左鍵是否保持按下mode = True # if True, draw rectangle. Press 'm' to toggle to curveix,iy = -1,-1 # 按下左鍵時鼠標(biāo)的位置# 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()
程序運(yùn)行結(jié)果截圖展示:
以上是“opencv如何實(shí)現(xiàn)鼠標(biāo)動作GUI”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。