【python】OpenCV—Tracking(10.2)

文章目录

BackgroundSubtractor

Opencv 有三种背景分割器

  • K-Nearest:KNN

  • Mixture of Gaussian(MOG2)

  • Geometric Multigid(GMG)

借助 BackgroundSubtractor 类,可检测阴影,用阈值排除阴影,从而关注实际特征

createBackgroundSubtractorMOG2

OpenCV图像处理- 视频背景消除与前景ROI提取

API:

cv2.createBackgroundSubtractorMOG2(

int history = 500,

double varThreshold = 16,

bool detectShadows = true

)

参数解释如下:

  • history表示过往帧数,500帧,选择history = 1就变成两帧差
  • varThreshold表示像素与模型之间的马氏距离,值越大,只有那些最新的像素会被归到前景,值越小前景对光照越敏感。
  • detectShadows 是否保留阴影检测,请选择False这样速度快点。
py 复制代码
import cv2
import os
# bs = cv2.createBackgroundSubtractorKNN(detectShadows=True)
bs = cv2.createBackgroundSubtractorMOG2(detectShadows=True)
os.makedirs("frame1", exist_ok=True)
os.makedirs("frame2", exist_ok=True)
os.makedirs("frame3", exist_ok=True)

camera = cv2.VideoCapture('car.mkv')
index = 0
while True:
    ret, frame = camera.read()
    index += 1
    frame_h, frame_w, _ = frame.shape
    fgmask = bs.apply(frame)
    th = cv2.threshold(fgmask.copy(), 244, 255, cv2.THRESH_BINARY)[1]
    dilated = cv2.dilate(th, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)),
                         iterations=2)
    contours, _ = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    for c in contours:
        # if cv2.contourArea(c) > frame_w*0.075 * frame_h*0.075:
        if cv2.contourArea(c) > 1000:
            (x, y, w, h) = cv2.boundingRect(c)
            cv2.rectangle(frame, (x,y), (x+w, y+h), (0, 0, 255), 5)
    cv2.imshow("mog", fgmask)
    cv2.imwrite("./frame1/{}.jpg".format(index), fgmask)
    cv2.imshow("thresh", th)
    cv2.imwrite("./frame2/{}.jpg".format(index), th)
    cv2.imshow("detection", frame)
    cv2.imwrite("./frame3/{}.jpg".format(index), frame)

    if cv2.waitKey(30) & 0xff == ord("q"):
        break

camera.release()
cv2.destroyAllWindows()

做 gif 的时候只设置了播放一次,重复播放需要刷新

createBackgroundSubtractorKNN

py 复制代码
import cv2
import numpy as np
bs = cv2.createBackgroundSubtractorKNN(detectShadows=True)
camera = cv2.VideoCapture('car.mkv')
index = 0
while True:
    ret, frame = camera.read()
    index += 1
    frame_h, frame_w, _ = frame.shape
    fgmask = bs.apply(frame)
    th = cv2.threshold(fgmask.copy(), 244, 255, cv2.THRESH_BINARY)[1]
    dilated = cv2.dilate(th, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)),
                         iterations=2)
    contours, _ = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    for c in contours:
        # if cv2.contourArea(c) > frame_w*0.075 * frame_h*0.075:
        if cv2.contourArea(c) > 1000:
            (x, y, w, h) = cv2.boundingRect(c)
            cv2.rectangle(frame, (x,y), (x+w, y+h), (0, 0, 255), 5)
    cv2.imshow("mog", fgmask)
    cv2.imwrite("./frame1/{}.jpg".format(index), fgmask)
    cv2.imshow("thresh", th)
    cv2.imwrite("./frame2/{}.jpg".format(index), th)
    cv2.imshow("detection", frame)
    cv2.imwrite("./frame3/{}.jpg".format(index), frame)

    if cv2.waitKey(30) & 0xff == ord("q"):
        break

camera.release()
cv2.destroyAllWindows()
相关推荐
迈火34 分钟前
ComfyUI-3D-Pack:3D创作的AI神器
人工智能·gpt·3d·ai·stable diffusion·aigc·midjourney
AntBlack44 分钟前
不当韭菜V1.1 :增强能力 ,辅助构建自己的交易规则
后端·python·pyqt
Moshow郑锴1 小时前
机器学习的特征工程(特征构造、特征选择、特征转换和特征提取)详解
人工智能·机器学习
CareyWYR2 小时前
每周AI论文速递(250811-250815)
人工智能
AI精钢2 小时前
H20芯片与中国的科技自立:一场隐形的博弈
人工智能·科技·stm32·单片机·物联网
whaosoft-1432 小时前
51c自动驾驶~合集14
人工智能
杜子不疼.3 小时前
《Python学习之字典(一):基础操作与核心用法》
开发语言·python·学习
Jinkxs3 小时前
自动化测试的下一站:AI缺陷检测工具如何实现“bug提前预警”?
人工智能·自动化
小幽余生不加糖3 小时前
电路方案分析(二十二)适用于音频应用的25-50W反激电源方案
人工智能·笔记·学习·音视频
柠檬味拥抱4 小时前
优化AI智能体行为:Q学习、深度Q网络与动态规划在复杂任务中的研究
人工智能