Opencv-目标检测

作者:安澈澈^
案列:© Fu Xianjun. All Rights Reserved.

目标检测
可提取视频中自己所感兴趣的部分

import cv2
import numpy as np

OPENCV_OBJECT_TRACKERS = {
    "csrt": cv2.TrackerCSRT_create,
    "kcf": cv2.TrackerKCF_create,
    "boosting": cv2.TrackerBoosting_create,
    "mil": cv2.TrackerMIL_create,
    "tld": cv2.TrackerTLD_create,
    "medianflow": cv2.TrackerMedianFlow_create,
    "mosse": cv2.TrackerMOSSE_create
}
#2.实例化追踪器对象
trackers = cv2.MultiTracker_create()
#3.视频基本处理方法
vs = cv2.VideoCapture("name.mp4")
while True:
    frame = vs.read()
    frame = frame[1]
    if frame is None:
        break
    (h,w) = frame.shape[:2]
    width=600
    r = width/float(w)
    dim = (width,int(h*r))
    frame = cv2.resize(frame,dim,interpolation = cv2.INTER_AREA)
    #4.追踪结果与区域绘制
    (success,boxes) = trackers.update(frame)
    for box in boxes:
        (x,y,w,h)= [int(v) for v in box]
        cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)
    cv2.imshow("Frame",frame)
    #5.选择目标ROI
    key = cv2.waitKey(100)& 0xFF
    if key==ord("s"):
        box = cv2.selectROI("Frame",frame,fromCenter=False,
                           showCrosshair=True)
        tracker = OPENCV_OBJECT_TRACKERS["csrt"]()
        trackers.add(tracker,frame,box)
    elif key ==27:
        break
#6.关闭视频
vs.release()
cv2.destroyAllWindows()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值