OpenCV笔记2:鼠标事件实现绘制直线、矩阵、曲线

OpenCV

鼠标事件

  1. 创建窗口
  2. 设置窗口大小
  3. 鼠标事件监听
    • 判断事件更新起始点和终点
    • 绘制线
    • 显示图片
  4. 打开背景图
"""
鼠标事件
down
up
move
"""
import cv2
import numpy as np


WINNAME = 'DRAWBOARD'
st_point = (-1, -1)
end_point = (-1, -1)


def drawLine(event, x, y, flags, param):
    global st_point, end_point
    img_copy = bg_img.copy()
    if event == cv2.EVENT_LBUTTONDOWN:
        print('EVENT_LBUTTONDOWN', event, x, y, flags, param)
        st_point = (x, y)
    if event == cv2.EVENT_MOUSEMOVE and flags:
        end_point = (x, y)
        cv2.line(img_copy, st_point, end_point, color=(0, 0, 255), thickness=2)
        cv2.imshow(WINNAME, img_copy)
    if event == cv2.EVENT_LBUTTONUP:
        print('EVENT_LBUTTONUP', event, x, y, flags, param)
        end_point = (x, y)
        # 绘制线
        # 起始点 终点
        cv2.line(bg_img, st_point, end_point, color=(0, 0, 255), thickness=2)
        cv2.imshow(WINNAME, bg_img)


def drawRectangle(event, x, y, flags, param):
    global st_point, end_point
    img_copy = bg_img.copy()
    if event == cv2.EVENT_LBUTTONDOWN:
        print('EVENT_LBUTTONDOWN', event, x, y, flags, param)
        st_point = (x, y)
    if event == cv2.EVENT_MOUSEMOVE and flags:
        end_point = (x, y)
        cv2.rectangle(img_copy, st_point, end_point, color=(0, 0, 255), thickness=2)
        cv2.imshow(WINNAME, img_copy)
    if event == cv2.EVENT_LBUTTONUP:
        print('EVENT_LBUTTONUP', event, x, y, flags, param)
        end_point = (x, y)
        # 绘制线
        # 起始点 终点
        cv2.rectangle(bg_img, st_point, end_point, color=(0, 0, 255), thickness=2)
        cv2.imshow(WINNAME, bg_img)


def drawPolylines(event, x, y, flags, param):
    global st_point, end_point
    if event == cv2.EVENT_LBUTTONDOWN:
        print('EVENT_LBUTTONDOWN', event, x, y, flags, param)
        st_point = (x, y)
        end_point = st_point
    if event == cv2.EVENT_MOUSEMOVE and flags:
        last_x, last_y = st_point, end_point
        st_point = end_point
        end_point = (x, y)
        print('EVENT_MOUSEMOVE', event, x, y, flags, param)
        pts = np.array([last_x, last_y, st_point, end_point])
        cv2.polylines(bg_img, [pts], isClosed=False, color=(0, 0, 255), thickness=2)
        cv2.imshow(WINNAME, bg_img)
    if event == cv2.EVENT_LBUTTONUP:
        print('EVENT_LBUTTONUP', event, x, y, flags, param)
        last_x, last_y = st_point, end_point
        st_point = end_point
        end_point = (x, y)
        # 绘制线
        # 起始点 终点
        pts = np.array([last_x, last_y, st_point, end_point])
        cv2.polylines(bg_img, [pts], isClosed=False, color=(0, 0, 255), thickness=2)
        cv2.imshow(WINNAME, bg_img)


def callBack(event, x, y, flags, param):
    # drawLine(event, x, y, flags, param)
    # drawRectangle(event, x, y, flags, param)
    drawPolylines(event, x, y, flags, param)


if __name__ == '__main__':
    # 创建窗口
    cv2.namedWindow(WINNAME, cv2.WINDOW_NORMAL)
    # 设置窗口大小
    cv2.resizeWindow(WINNAME, 360, 360)
    # 鼠标事件的监听
    # 参数1 事件监听的窗口名称
    # 参数2 函数名称 用于回调使用
    cv2.setMouseCallback(WINNAME, callBack, param='hello')

    # 打开背景图
    bg_img = cv2.imread('snower.jpg')
    cv2.imshow(WINNAME, bg_img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

鼠标绘制直线

20240220174152_rec_.gif

鼠标绘制矩形

20240220174112_rec_.gif

鼠标绘制曲线

20240220173954_rec_.gif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

饭碗、碗碗香

感谢壮士的慷概解囊!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值