您的位置:首页 > 运维架构

opencv高斯背景建模

2016-03-31 19:09 393 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: