【OpenCV】80 视频分析—背景消除与前景ROI提取

80 视频分析—背景消除与前景ROI提取

代码

import numpy as np
import cv2 as cv

cap = cv.VideoCapture('../images/vtest.avi')
fgbg = cv.createBackgroundSubtractorMOG2(
    history=500, varThreshold=100, detectShadows=False)

def process(image, opt=1):
    mask = fgbg.apply(frame)
    line = cv.getStructuringElement(cv.MORPH_RECT, (1, 5), (-1, -1))
    mask = cv.morphologyEx(mask, cv.MORPH_OPEN, line)
    cv.imshow("mask", mask)
    # 轮廓提取, 发现最大轮廓
    contours, hierarchy = cv.findContours(mask, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
    for c in range(len(contours)):
        area = cv.contourArea(contours[c])
        if area < 100:
            continue
        rect = cv.minAreaRect(contours[c])
        cv.ellipse(image, rect, (0, 255, 0), 2, 8)
        cv.circle(image, (np.int32(rect[0][0]), np.int32(rect[0][1])), 2, (255, 0, 0), 2, 8, 0)
    return image, mask

while True:
    ret, frame = cap.read()
    cv.imshow('input', frame)
    result, m_ = process(frame)
    cv.imshow('result', result)
    k = cv.waitKey(50)&0xff
    if k == 27:
        cv.imwrite("D:/result.png", result)
        cv.imwrite("D:/mask.png", m_)
        break
cap.release()
cv.destroyAllWindows()

实验结果

在这里插入图片描述

解释

通过视频中的背景进行建模,实现背景消除,生成mask图像,通过对mask二值图像分析实现对前景活动对象ROI区域的提取,是很多视频监控分析软件常用的手段之一,该方法很实时!整个步骤如下:

  1. 初始化背景建模对象GMM
  2. 读取视频一帧
  3. 使用背景建模消除生成mask
  4. 对mask进行轮廓分析提取ROI
  5. 绘制ROI对象

所有内容均来源于贾志刚老师的知识星球——OpenCV研习社,本文为个人整理学习,已获得贾老师授权,有兴趣、有能力的可以加入贾老师的知识星球进行深入学习。
在这里插入图片描述

发布了111 篇原创文章 · 获赞 0 · 访问量 1670

猜你喜欢

转载自blog.csdn.net/liu_taiting/article/details/104961412
今日推荐