您好,登錄后才能下訂單哦!
這篇文章主要講解了“python OpenCV怎么使用背景分離方法”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“python OpenCV怎么使用背景分離方法”吧!
背景分離(BS)是一種通過使用靜態(tài)相機(jī)來生成前景掩碼(包含屬于場景中的移動對象像素的二進(jìn)制圖像)的常用技術(shù)
顧名思義,BS計算前景掩碼,在當(dāng)前幀與背景模型之間執(zhí)行減法運(yùn)算,其中包含場景的靜態(tài)部分,考慮到所觀察場景的特征,可以將其視為背景的所有內(nèi)容。
背景建模包括兩個主要步驟:
1.背景初始化
2.背景更新 第一步,計算背景的初始模型,在第二步中,更新模型以適應(yīng)場景中可能的變化
讓用戶選擇處理視頻文件或圖像序列。在此示例中,將使用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
對象將用于生成前景掩碼。在此示例中,使用了默認(rèn)參數(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)
感謝各位的閱讀,以上就是“python OpenCV怎么使用背景分離方法”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對python OpenCV怎么使用背景分離方法這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。