Python,OpenCV鼠标事件进行矩形、圆形的绘制(随机颜色、随机半径)
生活随笔
收集整理的這篇文章主要介紹了
Python,OpenCV鼠标事件进行矩形、圆形的绘制(随机颜色、随机半径)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Python,OpenCV鼠標事件進行矩形、圓形的繪制(隨機顏色、隨機半徑)
- 1. 效果圖
- 2. 源碼
- 參考
這篇博客將介紹鼠標事件,并介紹鼠標事件矩形、圓形的繪制;
- 所有的鼠標事件(左鍵按下、左鍵釋放、右鍵按下、右鍵釋放、左鍵雙擊等);
- 繪制隨機半徑、隨機顏色圓形;
- 繪制矩形、切換按鍵繪制圓形;
1. 效果圖
鼠標事件,雙擊左鍵繪制圓形效果圖如下:
鼠標事件,雙擊左鍵繪制隨機顏色、隨機半徑的圓形效果圖如下:
鼠標事件,雙擊左鍵繪制矩形,m鍵切換,雙擊左鍵繪制圓形效果圖如下:
2. 源碼
# Python,OpenCV鼠標事件進行矩形、圓形的繪制(隨機顏色、隨機半徑)
# USAGE
# python mouse_as_paint_brushimport cv2
import numpy as np# 查看所有鼠標事件
events = [i for i in dir(cv2) if 'EVENT' in i]
print(events)img = Nonedrawing = False # true if mouse is pressed
mode = True # if True, draw rectangle. Press 'm' to toggle to curve
ix, 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()
參考
- https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_mouse_handling/py_mouse_handling.html#mouse-handling
總結
以上是生活随笔為你收集整理的Python,OpenCV鼠标事件进行矩形、圆形的绘制(随机颜色、随机半径)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cv2.threshholding()简
- 下一篇: Python,OpenCV图像金字塔cv