温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python  OpenCV怎么使用背景分离方法

发布时间:2023-04-25 11:44:02 来源:亿速云 阅读:83 作者:zzz 栏目:开发技术

这篇文章主要讲解了“python  OpenCV怎么使用背景分离方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“python  OpenCV怎么使用背景分离方法”吧!

理论

  • 背景分离(BS)是一种通过使用静态相机来生成前景掩码(包含属于场景中的移动对象像素的二进制图像)的常用技术

  • 顾名思义,BS计算前景掩码,在当前帧与背景模型之间执行减法运算,其中包含场景的静态部分,考虑到所观察场景的特征,可以将其视为背景的所有内容。

  • 背景建模包括两个主要步骤:

    • 1.背景初始化

    • 2.背景更新 第一步,计算背景的初始模型,在第二步中,更新模型以适应场景中可能的变化

实现

让用户选择处理视频文件或图像序列。在此示例中,将使用cv2.BackgroundSubtractorMOG2 生成前景掩码。

from __future__ import print_function
import cv2
import argparse
parser = argparse.ArgumentParser(
            description='This program shows how to use background subtraction methods provided by OpenCV. You can process both videos and images.')
parser.add_argument('--input', type=str, help='Path to a video or a sequence of image.', default='vtest.avi')
parser.add_argument('--algo', type=str, help='Background subtraction method (KNN, MOG2).', default='MOG2')
args = parser.parse_args()
## [create]
# create Background Subtractor objects
if args.algo == 'MOG2':
    backSub = cv2.createBackgroundSubtractorMOG2()
else:
    backSub = cv2.createBackgroundSubtractorKNN()
## [create]
## [capture]
capture = cv2.VideoCapture(args.input)
if not capture.isOpened():
    print('Unable to open: ' + args.input)
    exit(0)
## [capture]
while True:
    ret, frame = capture.read()
    if frame is None:
        break
    ## [apply]
    # update the background model
    fgMask = backSub.apply(frame)
    ## [apply]
    ## [display_frame_number]
    # get the frame number and write it on the current frame
    cv2.rectangle(frame, (10, 2), (100,20), (255,255,255), -1)
    cv2.putText(frame, str(capture.get(cv2.CAP_PROP_POS_FRAMES)), (15, 15),
               cv2.FONT_HERSHEY_SIMPLEX, 0.5 , (0,0,0))
    ## [display_frame_number]
    ## [show]
    # show the current frame and the fg masks
    cv2.imshow('Frame', frame)
    cv2.imshow('FG Mask', fgMask)
    ## [show]
    keyboard = cv2.waitKey(30)
    if keyboard == 'q' or keyboard == 27:
        break

代码分析

分析上面代码的主要部分:

  • cv2.BackgroundSubtractor对象将用于生成前景掩码。在此示例中,使用了默认参数,但是也可以在create函数中声明特定的参数。

# create Background Subtractor objects  KNN or MOG2
if args.algo == 'MOG2':
    backSub = cv2.createBackgroundSubtractorMOG2()
else:
    backSub = cv2.createBackgroundSubtractorKNN()
  • cv2.VideoCapture对象用于读取输入视频或输入图像序列

capture = cv2.VideoCapture(args.input)
if not capture.isOpened:
    print('Unable to open: ' + args.input)
    exit(0)
  • 每帧都用于计算前景掩码和更新背景。如果要更改用于更新背景模型的学习率,可以通过将参数传递给apply方法来设置特定的学习率

# update the background model
    fgMask = backSub.apply(frame)
  • 当前帧编号可以从cv2.Videocapture对象中提取,并在当前帧的左上角冲压。使用白色矩形来突出显示黑色框架号

 # get the frame number and write it on the current frame
    cv2.rectangle(frame, (10, 2), (100,20), (255,255,255), -1)
    cv2.putText(frame, str(capture.get(cv2.CAP_PROP_POS_FRAMES)), (15, 15),
               cv2.FONT_HERSHEY_SIMPLEX, 0.5 , (0,0,0))
  • 显示当前的输入帧和结果

# show the current frame and the fg masks
    cv2.imshow('Frame', frame)
    cv2.imshow('FG Mask', fgMask)

感谢各位的阅读,以上就是“python  OpenCV怎么使用背景分离方法”的内容了,经过本文的学习后,相信大家对python  OpenCV怎么使用背景分离方法这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI