歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> OpenCV 基於混合高斯模型GMM的運動目標檢測

OpenCV 基於混合高斯模型GMM的運動目標檢測

日期:2017/3/1 10:02:55   编辑:Linux編程

OpenCV的video module中包含了幾種較為常用的背景減除方法,其中混合高斯模型(Gaussian of Mixture Models, GMM)方法效果較好。

常用的目標檢測方法:1)幀間差分;2)背景減除;

其中背景減除方法的關鍵在於建立一個魯棒的背景模型(背景圖像),常用的建立背景模型方法有:

1)均值法;2)中值法;3)滑動平均濾波法;4)單高斯;5)混合高斯模型;6)codebook,等。

混合高斯模型的原理:

每個像素的R、G、B三個通道像素值的變化分別由一個混合高斯模型分布來刻畫。這樣的好處在於,同一個像素位置處可以呈現多個模態的像素值變化(例如水波紋,晃動的葉子等)。

GMM的出處:Adaptive background mixture models for real-time tracking (1999年由Chris Stau er提出)

OpenCV版本:2.4.2

下面的代碼實現了基於GMM的運動目標檢測,同時能夠消除運動陰影; (基於文獻:Improved adaptive Gausian mixture model for background subtraction)

// 基於混合高斯模型的運動目標檢測
// Author: http://blog.csdn.net/icvpr


#include <iostream>
#include <string>

#include <opencv2/opencv.hpp>


int main(int argc, char** argv)
{
std::string videoFile = "../test.avi";

cv::VideoCapture capture;
capture.open(videoFile);

if (!capture.isOpened())
{
std::cout<<"read video failure"<<std::endl;
return -1;
}


cv::BackgroundSubtractorMOG2 mog;

cv::Mat foreground;
cv::Mat background;

cv::Mat frame;
long frameNo = 0;
while (capture.read(frame))
{
++frameNo;

std::cout<<frameNo<<std::endl;

// 運動前景檢測,並更新背景
mog(frame, foreground, 0.001);

// 腐蝕
cv::erode(foreground, foreground, cv::Mat());

// 膨脹
cv::dilate(foreground, foreground, cv::Mat());

mog.getBackgroundImage(background); // 返回當前背景圖像

cv::imshow("video", foreground);
cv::imshow("background", background);


if (cv::waitKey(25) > 0)
{
break;
}
}


return 0;
}

實驗結果:

當前幀圖像

當前背景圖像

前景圖像

經過腐蝕和膨脹處理後的前景圖像

(白色為運動目標區域;灰色為陰影區域;黑色為背景)

Copyright © Linux教程網 All Rights Reserved