cv::BackgroundSubtractorMOG2和cv::bgsegm::BackgroundSubtractorMOG一樣,都是基于高斯混合模型的背景與前景分割算法。
cv::BackgroundSubtractorMOG2是對cv::bgsegm::BackgroundSubtractorMOG的改進,經(jīng)過改進,它實現(xiàn)了自適應高斯混合模型參數(shù)的更新,增強了復雜場景背景檢測的性能。
具體的算法原理可以參見下面兩篇論文:
第一篇:
Zoran Zivkovic and Ferdinand van der Heijden. Efficient adaptive density estimation per image pixel for the task of background subtraction. Pattern recognition letters, 27(7):773–780, 2006.
第二篇:
Zoran Zivkovic. Improved adaptive gaussian mixture model for background subtraction. In Pattern Recognition, 2004. ICPR 2004. Proceedings of the 17th International Conference on, volume 2, pages 28–31. IEEE, 2004.
第一篇論文的摘要如下:
We analyze the computer vision task of pixel-level background subtraction. We present recursive equations that are used to constantly update the parameters of a Gaussian mixture model and to simultaneously select the appropriate number of components for each pixel. We also present a simple non-parametric adaptive density estimation method. The two methods are compared with each other and with some previously proposed algorithms.
翻譯如下:
分析了像素級背景差分的計算機視覺任務。我們提出了遞歸方程,用于不斷更新高斯混合模型的參數(shù),同時為每個像素選擇適當數(shù)量的組件。我們還提出了一種簡單的非參數(shù)自適應密度估計方法。對這兩種方法進行了比較,并與以前提出的一些算法進行了比較。
從第一篇論文的摘要可以看出,類cv::BackgroundSubtractorMOG2能夠自動更新高斯混合模型的參數(shù)。
第二篇論文的摘要如下:
Background subtraction is a common computer vision task. We analyze the usual pixel-level approach. We develop an efficient adaptive algorithm using Gaussian mixture probability density. Recursive equations are used to constantly update the parameters and but also to simultaneously select the appropriate number of components for each pixel.
翻譯如下:
背景減法是一種常見的計算機視覺任務。我們分析了常用的像素級方法。我們開發(fā)了一種利用高斯混合概率密度的高效自適應算法。遞歸方程用于不斷更新參數(shù),但也用于同時為每個像素選擇適當數(shù)量的組件。
從第二篇論文的摘要可以看出,類cv::BackgroundSubtractorMOG2除了能夠自動更新高斯混合模型的參數(shù),而且可以為每個像素選擇適當數(shù)量的組件,重點是在像素級尺度上進行處理。
繼承于基類cv::BackgroundSubtractor的成員函數(shù)apply()和getBackgroundImage()的詳細介紹見博文:https://blog.csdn.net/wenhao_ir/article/details/125007017
其特有的成員函數(shù)如下:
virtual double cv::BackgroundSubtractorMOG2::getBackgroundRatio( ) const
成員函數(shù)getBackgroundRatio()的介紹見博文 https://blog.csdn.net/wenhao_ir/article/details/125010301
virtual double cv::BackgroundSubtractorMOG2::getComplexityReductionThreshold( ) const
成員函數(shù)getComplexityReductionThreshold()用于返回復雜性降低閾值。此參數(shù)定義每一個樣本中支持高斯混合模型某個分量存在需要的樣本數(shù)量。CT=0.05是所有樣本的默認值。通過將CT設置為0,可以得到與標準Stauffer&Grimson算法非常相似的算法。
virtual bool cv::BackgroundSubtractorMOG2::getDetectShadows( ) const
成員函數(shù)getDetectShadows()用于返回陰影檢測標志。如果陰影檢測標志的值為true,那么算法會檢測陰影并對其進行標記。
virtual int cv::BackgroundSubtractorMOG2::getHistory( ) const
成員函數(shù)getHistory()用于返回影響背景模型的歷史幀數(shù)。
virtual int cv::BackgroundSubtractorMOG2::getNMixtures( ) const
成員函數(shù)getNMixtures()用于返回高斯混合模型中高斯分量的個數(shù)。
virtual double cv::BackgroundSubtractorMOG2::getShadowThreshold ( ) const
成員函數(shù)getShadowThreshold()的介紹見博文 https://blog.csdn.net/wenhao_ir/article/details/125007017
virtual int cv::BackgroundSubtractorMOG2::getShadowValue( ) const
成員函數(shù)getShadowThreshold()的介紹見博文 https://blog.csdn.net/wenhao_ir/article/details/125007017
virtual double cv::BackgroundSubtractorMOG2::getVarInit( ) const
成員函數(shù)getVarInit()用于返回每個高斯分量的初始方差。
virtual double cv::BackgroundSubtractorMOG2::getVarMax( ) const
成員函數(shù)getVarMax()用于返回所有高斯分量中的最大方差。
virtual double cv::BackgroundSubtractorMOG2::getVarMin( ) const
成員函數(shù)getVarMin()用于返回所有高斯分量中的最小方差。
virtual double cv::BackgroundSubtractorMOG2::getVarThreshold( ) const
成員函數(shù)getVarThreshold()用于返回返回像素模型匹配的方差閾值。這個參數(shù)是Mahalanobis距離(馬氏距離)平方的主閾值,它用于判斷樣本是否被背景模型很好的描述。它相當于上面論文中的Cthr參數(shù)。
virtual double cv::BackgroundSubtractorMOG2::getVarThresholdGen ( ) const
成員函數(shù)getVarThresholdGen()用于返回是否要為像素新建高斯混合混型組件的方差閾值,對應于論文中的參數(shù)Tg 。如果根據(jù)參數(shù)VarThreshold,樣本不能匹配現(xiàn)有背景模型的話,就得考慮是否要為其新建高斯混合混型組件了,那么什么情況下為其新建高斯混合混型組件呢,就根據(jù)值VarThresholdGe來判斷,如果這個樣本的標準差的3倍大于等VarThreshold,則為其新建高斯混合混型組件,否則,視為前景。顯然,這個值越小,產生的組件越多。較高的VarThreshold雖然組件數(shù)量會較小,但也會導致這些組件過大。該參數(shù)默認值9,來歷為當標準差取3時得到的,標準差為3,則其3倍為9。
virtual void cv::BackgroundSubtractorMOG2::setBackgroundRatio(double ratio)
成員函數(shù)setBackgroundRatio()用于設置背景比率,詳情見對成員函數(shù)getBackgroundRatio()的介紹。
virtual void cv::BackgroundSubtractorMOG2::setComplexityReductionThreshold(double ct)
成員函數(shù)setComplexityReductionThreshold()用于設置值ComplexityReductionThreshold,詳見對成員函數(shù)getComplexityReductionThreshold()的介紹。
virtual void cv::BackgroundSubtractorMOG2::setDetectShadows (bool detectShadows)
成員函數(shù)setDetectShadows()用于設置是否要進行陰影檢測。
virtual void cv::BackgroundSubtractorMOG2::setHistory(int history)
成員函數(shù)setHistory()用于設置影響背景模型的歷史幀數(shù)。
virtual void cv::BackgroundSubtractorMOG2::setNMixtures (int nmixtures)
成員函數(shù)setNMixtures()用于設置高斯混合模型中高斯分量的個數(shù)。
virtual void cv::BackgroundSubtractorMOG2::setShadowThreshold(double threshold)
成員函數(shù)setShadowThreshold()的詳情見博文 https://blog.csdn.net/wenhao_ir/article/details/125007017
virtual void cv::BackgroundSubtractorMOG2::setShadowValue(int value)
成員函數(shù)setShadowValue()的詳情見博文 https://blog.csdn.net/wenhao_ir/article/details/125007017
virtual void cv::BackgroundSubtractorMOG2::setVarInit(double varInit)
成員函數(shù)setVarInit()用于設置參數(shù)VarInit,詳情見對成員函數(shù)getVarInit()的介紹。
virtual void cv::BackgroundSubtractorMOG2::setVarMax(double varMax)
成員函數(shù)setVarMax()用于設置參數(shù)VarMax,詳情見對成員函數(shù)getVarMax()的介紹。
virtual void cv::BackgroundSubtractorMOG2::setVarMin(double varMin)
成員函數(shù)setVarMin()用于設置參數(shù)VarMin,詳情見對成員函數(shù)getVarMin()的介紹。
virtual void cv::BackgroundSubtractorMOG2::setVarThreshold(double varThreshold)
成員函數(shù)setVarThreshold()用于設置參數(shù)VarThreshold,詳情見對成員函數(shù)getVarThreshold()的介紹。
virtual void cv::BackgroundSubtractorMOG2::setVarThresholdGen(double varThresholdGen)
成員函數(shù)setVarThresholdGen()用于設置參數(shù)VarThresholdGen,詳情見對成員函數(shù)getVarThresholdGen()的介紹。
在上示例代碼前,還需要介紹下函數(shù)createBackgroundSubtractorMOG2(),
函數(shù)createBackgroundSubtractorMOG2()用于構建類BackgroundSubtractorMOG2的實例化對象,并返回對象指針。
函數(shù)createBackgroundSubtractorMOG2()的原型如下:
Ptr<BackgroundSubtractorMOG2> cv::createBackgroundSubtractorMOG2(
int history = 500,
double varThreshold = 16,
bool detectShadows = true )
參數(shù)history和varThreshold已經(jīng)在上面介紹過了,所以只說下detectShadows這個參數(shù),這個參數(shù)表示是否對前景進行陰影檢測,默認值為ture,表示啟用前景陰影檢測。
下面上示例代碼:
示例代碼中用到的視頻下載鏈接:https://pan.baidu.com/s/1X08cwwSE4DUvzT0XvHvpvw?pwd=9yyq
//博主微信/QQ 2487872782
//有問題可以聯(lián)系博主交流
//有圖像處理開發(fā)需求也請聯(lián)系博主
//圖像處理技術交流QQ群 271891601
//OpenCV版本:3.0
//VS版本:2013
#include <opencv2/opencv.hpp>
#include <iostream>
#include <sstream>
using namespace cv;
using namespace std;
void processVideo(Ptr<BackgroundSubtractorMOG2> pBackgroundMOG2, string videoFilename)
{
cv::Mat frame, FGMask, BGimgae;
int keyboard = 0;
// 視頻獲取
VideoCapture capture(videoFilename);
if (!capture.isOpened())
exit(EXIT_FAILURE);
// 按下q鍵或esc鍵退出
while ((char)keyboard != 'q' && (char)keyboard != 27)
{
// 讀取當前幀
if (!capture.read(frame))
exit(EXIT_FAILURE);
// 圖像尺寸縮小
cv::resize(frame, frame, cv::Size(), 0.3, 0.3);
// 背景建模參數(shù)設定
//pBackgroundMOG2->setHistory(200);
//pBackgroundMOG2->setDist2Threshold(600);
pBackgroundMOG2->setShadowThreshold(0.5);
pBackgroundMOG2->setShadowValue(10);
// 計算得到前景掩碼圖像
double learningRate = -1;
pBackgroundMOG2->apply(frame, FGMask, learningRate);
// 計算得到背景圖像
pBackgroundMOG2->getBackgroundImage(BGimgae);
// 輸出當前幀號
stringstream ss;
rectangle(frame, cv::Point(10, 2), cv::Point(100, 20), cv::Scalar(255, 255, 255), -1);
ss << capture.get(CAP_PROP_POS_FRAMES);
string frameNumberString = ss.str();
// 左上角顯示幀號
putText(frame, frameNumberString.c_str(), cv::Point(15, 15), FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
// 輸出結果
imshow("當前視頻幀", frame);
imshow("背景圖像", BGimgae);
imshow("前景掩碼圖像", FGMask);
keyboard = waitKey(30);
}
capture.release();
}
int main(int argc, char* argv[])
{
// 實例化背景建模算法類
Ptr<BackgroundSubtractorMOG2> pBackgroundMOG2 = createBackgroundSubtractorMOG2();
string inputPath = "car1.avi";
processVideo(pBackgroundMOG2, inputPath);
return 0;
}
運行結果如下圖所示:
因為是視頻處理,錄了個視頻,以便大家可以更好觀察結果,視頻在線觀看和下載鏈接如下:
鏈接:https://pan.baidu.com/s/1bmDpp0wS_gtIGXA-g2Nt4w?pwd=uaz0文章來源:http://www.zghlxwxcb.cn/news/detail-417738.html
延伸閱讀:
OpenCV3.0中有哪些視頻背景/前景分割(背景建模/前景提取)算法的類,它們各自的算法原理、特點是什么,并附示例代碼
OpenCV4中有哪些視頻背景/前景分割(背景建模/前景提取)算法的類,它們各自的算法原理、特點是什么,并附示例代碼文章來源地址http://www.zghlxwxcb.cn/news/detail-417738.html
到了這里,關于詳解OpenCV的視頻背景/前景分割(背景建模/前景提取)類cv::BackgroundSubtractorMOG2,并利用它實現(xiàn)對道路監(jiān)控視頻前景/背景的提取的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!