1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Python OpenCV鼠标事件进行矩形 圆形的绘制(随机颜色 随机半径)

Python OpenCV鼠标事件进行矩形 圆形的绘制(随机颜色 随机半径)

时间:2020-09-21 03:05:50

相关推荐

Python OpenCV鼠标事件进行矩形 圆形的绘制(随机颜色 随机半径)

Python,OpenCV鼠标事件进行矩形、圆形的绘制(随机颜色、随机半径)

1. 效果图2. 源码参考

这篇博客将介绍鼠标事件,并介绍鼠标事件矩形、圆形的绘制;

所有的鼠标事件(左键按下、左键释放、右键按下、右键释放、左键双击等);绘制随机半径、随机颜色圆形;绘制矩形、切换按键绘制圆形;

1. 效果图

鼠标事件,双击左键绘制圆形效果图如下:

鼠标事件,双击左键绘制随机颜色、随机半径的圆形效果图如下:

鼠标事件,双击左键绘制矩形,m键切换,双击左键绘制圆形效果图如下:

2. 源码

# Python,OpenCV鼠标事件进行矩形、圆形的绘制(随机颜色、随机半径)# USAGE# python mouse_as_paint_brushimport cv2import numpy as np# 查看所有鼠标事件events = [i for i in dir(cv2) if 'EVENT' in i]print(events)img = Nonedrawing = False # true if mouse is pressedmode = True # if True, draw rectangle. Press 'm' to toggle to curveix, iy = -1, -1# 定义鼠标回调函数,绘制填充的蓝色圆def draw_circle(event, x, y, flags, param):if event == cv2.EVENT_LBUTTONDBLCLK: # 鼠标左键双击事件cv2.circle(img, (x, y), 50, (255, 0, 0), -1)# 定义鼠标回调函数,绘制随机颜色随机半径的填充圆def draw_random_circle(event, x, y, flags, param):if event == cv2.EVENT_LBUTTONDBLCLK: # 鼠标左键双击事件radius = np.random.randint(1, high=50)color = np.random.randint(0, high=256, size=(3,)).tolist()# print('radius: ', radius, type(radius))# print('color: ', color, type(color))cv2.circle(img, (x, y), radius, color, -1)# 定义鼠标回调函数,绘制填充红色圆,或者绿色填充矩形def draw_circle_rectangle(event, x, y, flags, param):global ix, iy, drawing, modeif event == cv2.EVENT_LBUTTONDOWN:drawing = Trueix, iy = x, yelif event == cv2.EVENT_MOUSEMOVE:if drawing == True:if mode == True:cv2.rectangle(img, (ix, iy), (x, y), (0, 255, 0), -1)else:cv2.circle(img, (x, y), 10, (0, 0, 255), -1)elif event == cv2.EVENT_LBUTTONUP:drawing = Falseif mode == True:cv2.rectangle(img, (ix, iy), (x, y), (0, 255, 0), -1)else:cv2.circle(img, (x, y), 10, (0, 0, 255), -1)def draw_circle_only():# 创建一个黑色背景图,并绑定鼠标回调事件global img # 标明为全局变量img = np.zeros((512, 512, 3), np.uint8)cv2.namedWindow('draw_circle_only image')cv2.setMouseCallback('draw_circle_only image', draw_circle)while (1):cv2.imshow('draw_circle_only image', img)# 按下ESC键退出if cv2.waitKey(20) & 0xFF == 27:breakcv2.destroyAllWindows()def draw_circle_randomly():# 创建一个黑色背景图,并绑定鼠标回调事件global img # 标明为全局变量img = np.zeros((512, 512, 3), np.uint8)cv2.namedWindow('draw_circle_randomly image')cv2.setMouseCallback('draw_circle_randomly image', draw_random_circle)while (1):cv2.imshow('draw_circle_randomly image', img)# 按下ESC键退出if cv2.waitKey(20) & 0xFF == 27:breakcv2.destroyAllWindows()# 通过拖动鼠标来绘制矩形或圆形,鼠标回调函数有两部分,一是画矩形,二是画圆。# 这个具体的例子将非常有助于创建和理解一些交互式应用程序,如对象跟踪、图像分割等。# 按下左键拖动鼠标绘制矩形。按m键切换绘制圆形def draw_circle_and_rectangle():global imgimg = np.zeros((512, 512, 3), np.uint8)cv2.namedWindow('draw_circle_and_rectangle image')cv2.setMouseCallback('draw_circle_and_rectangle image', draw_circle_rectangle)while (1):cv2.imshow('draw_circle_and_rectangle image', img)k = cv2.waitKey(1) & 0xFFif k == ord('m'):global modemode = Falseelif k == 27:breakcv2.destroyAllWindows()draw_circle_only()draw_circle_randomly()draw_circle_and_rectangle()

参考

/3.0-beta/doc/py_tutorials/py_gui/py_mouse_handling/py_mouse_handling.html#mouse-handling

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。