opencv(4)鼠标操作

2020-09-19  本文已影响0人  TZX_0710

学习opencv的setMouseCallBack()函数

import cv2 as cv
import numpy as np


# 鼠标的回调函数
def draw_circie(event, x, y, flags, param):
    if event == cv.EVENT_LBUTTONDBLCLK:
        cv.circle(img, (x, y), 100, (255, 0, 0), -1)


# 创建一个黑色图像
img = np.zeros((512, 512, 3), np.uint8)
# 面板
cv.namedWindow("xx")
# 绑定鼠标
cv.setMouseCallback("xx", draw_circie)
while True:
    cv.imshow("xx", img)
    if cv.waitKey(25) == 27:
        break
cv.destroyWindow("images")

鼠标点击绘制

import numpy as np
import cv2 as cv


# 鼠标回调函数
def draw_circle(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_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)


drawing = False  # 如果按下鼠标,则为真
mode = True  # 如果为真,绘制矩形。按 m 键可以切换到曲线
ix, iy = -1, -1

img = np.zeros((512, 512, 3), np.uint8)

cv.namedWindow('image')
cv.setMouseCallback('image', draw_circle)
while True:
    cv.imshow("image", img)
    if cv.waitKey(25) == 27:
        break
cv.destroyAllWindows()
上一篇 下一篇

猜你喜欢

热点阅读