詳解python??OpenCV如何使用背景分離方法
目標
在本章中,將學(xué)習(xí):
- 背景分離(Background Subtraction)
- OpenCv函數(shù)
cv2.VideoCapture
,cv2.BackgroundSubtractor
理論
- 背景分離(BS)是一種通過使用靜態(tài)相機來生成前景掩碼(包含屬于場景中的移動對象像素的二進制圖像)的常用技術(shù)
- 顧名思義,BS計算前景掩碼,在當(dāng)前幀與背景模型之間執(zhí)行減法運算,其中包含場景的靜態(tài)部分,考慮到所觀察場景的特征,可以將其視為背景的所有內(nèi)容。
- 背景建模包括兩個主要步驟:
- 1.背景初始化
- 2.背景更新 第一步,計算背景的初始模型,在第二步中,更新模型以適應(yīng)場景中可能的變化
實現(xiàn)
讓用戶選擇處理視頻文件或圖像序列。在此示例中,將使用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
對象將用于生成前景掩碼。在此示例中,使用了默認參數(shù),但是也可以在create
函數(shù)中聲明特定的參數(shù)。
# 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)
- 每幀都用于計算前景掩碼和更新背景。如果要更改用于更新背景模型的學(xué)習(xí)率,可以通過將參數(shù)傳遞給
apply
方法來設(shè)置特定的學(xué)習(xí)率
# update the background model fgMask = backSub.apply(frame)
- 當(dāng)前幀編號可以從
cv2.Videocapture
對象中提取,并在當(dāng)前幀的左上角沖壓。使用白色矩形來突出顯示黑色框架號
# 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))
- 顯示當(dāng)前的輸入幀和結(jié)果
# show the current frame and the fg masks cv2.imshow('Frame', frame) cv2.imshow('FG Mask', fgMask)
結(jié)果
- frame
- 程序的輸出將作為MOG2方法的以下內(nèi)容(灰色區(qū)域被檢測到的陰影):
- 程序的輸出將視為knn方法的以下內(nèi)容(灰色區(qū)域被檢測到的陰影)
附加資源
- cv2.VideoCapture
- cv2.BackgroundSubtractor
- docs.opencv.org/4.1.2/d1/dc…
- Background Models Challenge (BMC) website
- A Benchmark Dataset for Foreground/Background Extraction
以上就是OpenCV如何使用背景分離方法的詳細內(nèi)容,更多關(guān)于OpenCV背景分離的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python爬蟲 線程池創(chuàng)建并獲取文件代碼實例
這篇文章主要介紹了python爬蟲 線程池創(chuàng)建并獲取文件代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-09Python3實現(xiàn)旋轉(zhuǎn)數(shù)組的3種算法小結(jié)
旋轉(zhuǎn)數(shù)組是一種常見的數(shù)據(jù)結(jié)構(gòu)問題,通常是指一個有序數(shù)組經(jīng)過旋轉(zhuǎn)后,使得所有元素逆序排列,本文主要介紹了Python3實現(xiàn)旋轉(zhuǎn)數(shù)組的3種算法小結(jié),感興趣的可以了解一下2023-12-12Python可視化學(xué)習(xí)之seaborn調(diào)色盤
seaborn是在matplotlib基礎(chǔ)上封裝的,所以matplotlib的調(diào)色盤seaborn都可以使用。本文系統(tǒng)介紹seaborn調(diào)色盤,相較于matplotlib,有諸多不同,需要的可以參考一下2022-02-02python實現(xiàn)發(fā)送QQ郵件(可加附件)
這篇文章主要為大家詳細介紹了python實現(xiàn)發(fā)送QQ郵件,可添加附件功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-12-12集調(diào)試共享及成本控制Prompt工具PromptLayer使用指南
這篇文章主要介紹了集調(diào)試共享及成本控制Prompt工具PromptLayer使用指南,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03