python-opencv-鼠标事件-画框圈定目标

参考链接: https://blog.csdn.net/lql0716/article/details/54174293

# -*- coding: utf-8 -*-

import cv2
import numpy as np

current_pos = None
tl = None
br = None

#鼠标事件
def get_rect(im, title='get_rect'):   #   (a,b) = get_rect(im, title='get_rect')
    mouse_params = {'tl': None, 'br': None, 'current_pos': None,
        'released_once': False}

    cv2.namedWindow(title)
    cv2.moveWindow(title, 100, 100)

    def onMouse(event, x, y, flags, param):

        param['current_pos'] = (x, y)

        if param['tl'] is not None and not (flags & cv2.EVENT_FLAG_LBUTTON):
            param['released_once'] = True

        if flags & cv2.EVENT_FLAG_LBUTTON:
            if param['tl'] is None:
                param['tl'] = param['current_pos']
            elif param['released_once']:
                param['br'] = param['current_pos']

    cv2.setMouseCallback(title, onMouse, mouse_params)
    cv2.imshow(title, im)

    while mouse_params['br'] is None:
        im_draw = np.copy(im)

        if mouse_params['tl'] is not None:
            cv2.rectangle(im_draw, mouse_params['tl'],
                mouse_params['current_pos'], (255, 0, 0))
        cv2.imshow(title, im_draw)
#        _ = cv2.waitKey(10)
        cv2.waitKey(0)

    cv2.destroyWindow(title)

    tl = (min(mouse_params['tl'][0], mouse_params['br'][0]),
        min(mouse_params['tl'][1], mouse_params['br'][1]))
    br = (max(mouse_params['tl'][0], mouse_params['br'][0]),
        max(mouse_params['tl'][1], mouse_params['br'][1]))

    #返回矩形框坐标
    return (tl, br)  #tl=(y1,x1), br=(y2,x2)


#读取摄像头/视频,然后用鼠标事件画框 
def readVideo(pathName, skipFrame):  #pathName为视频文件路径,skipFrame为视频的第skipFrame帧
#    cap = cv2.VideoCapture(0)    #读取摄像头
#    if not cap.isOpened():  #如果未发现摄像头,则按照路径pathName读取视频文件
#        cap = cv2.VideoCapture(pathName)    #读取视频文件,如pathName='D:/test/test.mp4'
    cap = cv2.VideoCapture(pathName) #读取视频
    c = 1
    while(cap.isOpened()):
        ret, frame = cap.read()
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        if(c>=skipFrame):
            mask = np.zeros(gray.shape, dtype=np.uint8)  #掩码操作,该矩阵与图片大小类型一致,为初始化全0像素值,之后对其操作区域赋值为1即可
            if(c==skipFrame):
                (a,b) = get_rect(frame, title='get_rect')  #鼠标画矩形框
                img01, img02 = frame, frame
                gray01, gray02 = gray, gray
            else:
                img1, img2 = prev_frame, frame
                gray1, gray2 = prev_frame, frame
            cv2.imshow('frame', frame)
        c = c + 1
        prev_gray = gray
        prev_frame = frame
        if cv2.waitKey(1) & 0xFF == ord('q'):    #点击视频窗口,按q键退出
            break
    cap.release()
    cv2.destroyAllWindows()


video=readVideo("E:\\demo.mp4",1)

   


----------
如果想要读取一张图片并手动画框(做目标跟踪时,经常需要第一帧的目标框),直接调用get_rect函数就可以啦~
img=cv2.imread("F:\\demo\\1.jpg")
(a,b)=get_rect(img,title='get_rect')  
print(a,b)
cv2.destroyAllWindows()
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值