鼠标回调函数:
def setMouseCallback( windowName, #窗口名称 onMouse, #鼠标响应处理函数 param=None) #处理函数的ID
event鼠标事件:
event: EVENT_LBUTTONDBLCLK = 7 左键双击 EVENT_LBUTTONDOWN = 1 左键点击 EVENT_LBUTTONUP = 4 左键释放 EVENT_MBUTTONDBLCLK = 9 中间释放 EVENT_MBUTTONDOWN = 3 中间点击 EVENT_MBUTTONUP = 6 中间释放 EVENT_MOUSEHWHEEL = 11 滚轮事件 EVENT_MOUSEMOVE = 0 滑动 EVENT_MOUSEWHEEL = 10 滚轮事件 EVENT_RBUTTONDBLCLK = 8 右键双击 EVENT_RBUTTONDOWN = 2 右键点击 EVENT_RBUTTONUP = 5 右键释放 flags: EVENT_FLAG_ALTKEY = 32 按Alt不放事件 EVENT_FLAG_CTRLKEY = 8 按Ctrl不放事件 EVENT_FLAG_LBUTTON = 1 左键拖拽 EVENT_FLAG_MBUTTON = 4 中键拖拽 EVENT_FLAG_RBUTTON = 2 右键拖拽 EVENT_FLAG_SHIFTKEY = 16 按Shift不放事件
示例
在图像上用鼠标点击左键拖拽画一矩形,并输出矩形信息:
import cv2 def draw_rectangle(event,x,y,flags,param): global ix, iy if event==cv2.EVENT_LBUTTONDOWN: ix, iy = x, y print("point1:=", x, y) elif event==cv2.EVENT_LBUTTONUP: print("point2:=", x, y) print("width=",x-ix) print("height=", y - iy) cv2.rectangle(img, (ix, iy), (x, y), (0, 255, 0), 2) img = cv2.imread("max.png") #加载图片 cv2.namedWindow('image') cv2.setMouseCallback('image', draw_rectangle) while(1): cv2.imshow('image', img) if cv2.waitKey(20) & 0xFF == 27: break cv2.destroyAllWindows()
输出:
point1:= 254 64 point2:= 708 569 width= 454 height= 505
以上这篇opencv3/python 鼠标响应操作详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。