opencv鼠标事件

鼠标左键为例讲解鼠标事件

#以鼠标左键为例
import cv2
def MouseEventDo(event,x,y,flags,param):
    global txtDir
    if event==cv2.EVENT_FLAG_LBUTTON:#如果鼠标左键发生
        print(txtDir)
        with open(txtDir, 'w') as f:
            f.write(str(int(x*2.5))+' '+ str(int(2.5*y)))

for picNum in picNumList:
    print(picNum)
    txtDir = rootTxt+'\\'+str(picNum)+'.txt'
    imgDir = rootImage+'\\'+str(picNum)+'.0.jpg'


    img = cv2.imread(imgDir)
    img = cv2.resize(img, (0, 0), fx=0.4, fy=0.4, interpolation=cv2.INTER_NEAREST)
    cv2.imshow(str(picNum), img)

    cv2.setMouseCallback(str(picNum), MouseEventDo)

    key = cv2.waitKey()
    if key==32:
        cv2.destroyAllWindows()

 

posted @ 2020-09-04 16:11  yunshangyue  阅读(207)  评论(0编辑  收藏  举报