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

发布时间:2023-04-25 11:44:02 作者:zzz
来源:亿速云 阅读:84

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

理论

实现

让用户选择处理视频文件或图像序列。在此示例中,将使用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

代码分析

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

# create Background Subtractor objects  KNN or MOG2
if args.algo == 'MOG2':
    backSub = cv2.createBackgroundSubtractorMOG2()
else:
    backSub = cv2.createBackgroundSubtractorKNN()
capture = cv2.VideoCapture(args.input)
if not capture.isOpened:
    print('Unable to open: ' + args.input)
    exit(0)
# update the background model
    fgMask = backSub.apply(frame)
 # 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怎么使用背景分离方法这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. python怎么操作小程序云数据库实现增删改查功能
  2. 利用C#守护Python进程的方法是什么

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

python opencv

上一篇:怎么使用python OpenCV实现图像特征匹配

下一篇:Python连接oracle的问题如何解决

相关阅读

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

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