博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python OpenCV学习笔记之:处理鼠标事件
阅读量:6674 次
发布时间:2019-06-25

本文共 1102 字,大约阅读时间需要 3 分钟。

  hot3.png

# -*- coding: utf-8 -*-# 处理鼠标事件import cv2 as cvimport numpy as npimg = np.zeros((512,512,3),np.uint8)ix,iy = -1,-1drawing = Falsemode = True# 鼠标回调函数def on_mouse_action(event,x,y,flags,param):    global ix,iy,drawing,mode    if event == cv.EVENT_LBUTTONDOWN: # 鼠标左键按下        drawing = True        ix,iy = x,y    elif event == cv.EVENT_LBUTTONDBLCLK: # 鼠标左键双击        cv.circle(img,(x,y),100,(255,0,0),-1)    elif 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 = False        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)cv.namedWindow("image")cv.setMouseCallback("image",on_mouse_action)while True:    cv.imshow("image",img)    k = cv.waitKey(20) & 0xFF    if k == ord('m'):        mode = not mode    elif k == 27:        breakcv.destroyAllWindows()

转载于:https://my.oschina.net/wujux/blog/800315

你可能感兴趣的文章