Opencv 自带提取前景(背景建模)
来源:互联网 发布:青花大罐淘宝 编辑:程序博客网 时间:2024/05/22 11:40
官方教程源码稍加修改:
读入视频(图片序列)->bei
//opencv#include "opencv2/imgcodecs.hpp"#include "opencv2/imgproc.hpp"#include "opencv2/videoio.hpp"#include <opencv2/highgui.hpp>#include <opencv2/video.hpp>//C#include <stdio.h>//C++#include <iostream>#include <sstream>using namespace cv;using namespace std;// Global variablesMat frame; //current frameMat fgMaskMOG2; //fg mask fg mask generated by MOG2 methodPtr<BackgroundSubtractor> pMOG2; //MOG2 Background subtractorint keyboard; //input from keyboardvoid help();void processVideo(char* videoFilename);void processImages(char* firstFrameFilename);int main(int argc, char* argv[]){ //print help information //help(); //check for the input parameter correctness/* if(argc != 3) { cerr <<"Incorret input list" << endl; cerr <<"exiting..." << endl; return EXIT_FAILURE; }*/ //create GUI windows namedWindow("Frame"); namedWindow("FG Mask MOG 2"); //create Background Subtractor objects pMOG2 = createBackgroundSubtractorMOG2(); //MOG2 approach processVideo("测试视频.avi"); //destroy GUI windows destroyAllWindows(); return EXIT_SUCCESS;}void processVideo(char* videoFilename) { //create the capture object VideoCapture capture(videoFilename); if(!capture.isOpened()){ //error in opening the video input cerr << "Unable to open video file: " << videoFilename << endl; exit(EXIT_FAILURE); } //read input data. ESC or 'q' for quitting while( (char)keyboard != 'q' && (char)keyboard != 27 ){ //read the current frame if(!capture.read(frame)) { cerr << "Unable to read next frame." << endl; cerr << "Exiting..." << endl; exit(EXIT_FAILURE); } //update the background model pMOG2->apply(frame, fgMaskMOG2); //get the frame number and write it on the current frame 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));//方块中写入帧数 //show the current frame and the fg masks imshow("Frame", frame);//medianBlur(fgMaskMOG2, fgMaskMOG2, 9);//中值滤波160309//erode(fgMaskMOG2, fgMaskMOG2, NULL);//dilate(fgMaskMOG2, fgMaskMOG2,NULL);imshow("FG Mask MOG 2", fgMaskMOG2);if ((char)keyboard != 's'){imwrite("src.jpg", frame);IplImage imgTmp = fgMaskMOG2;IplImage *fgMaskMOG2_ip = cvCloneImage(&imgTmp);cvSaveImage("dst.jpg", fgMaskMOG2_ip);cvReleaseImage(&fgMaskMOG2_ip);} //get the input from the keyboard keyboard = waitKey( 30 ); } //delete capture object capture.release();}
说明:
1.pMOG2->apply(frame, fgMaskMOG2);
apply可以添加第三个参数:Every frame is used both for calculating the foreground mask and for updating the background. If you want to change the learning rate used for updating the background model, it is possible to set a specific learning rate by passing a third parameter to the 'apply' method.
learningRate The value between 0 and 1 that indicates how fast the background model is learnt. Negative parameter value makes the algorithm to use some automatically chosen learning rate. 0 means that the background model is not updated at all, 1 means that the background model is completely reinitialized from the last frame.
CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1) = 0;
2.我用的720的监控视频,速度有些慢,如果对没帧进行缩小可能会好些。
效果:视频中某帧
~~代码可执行,有问题留言~~
- Opencv 自带提取前景(背景建模)
- 背景建模/背景减除/前景提取之背景建模库
- opencv3 背景建模提取前景图像
- 背景建模和前景提取 (2017研究生数模竞赛D题)
- OpenCv自带的前景检测方法
- [opencv]目标追踪之前景建模(定期背景更新模型)
- 前景提取、去除背景(selectROI+Grabcut)
- 背景减除-提取前景
- OpenCV-前景提取
- 背景建模与前景检测
- 前景检测算法_4(opencv自带GMM)
- 平均背景法提取前景
- 背景差分法实现前景提取
- ViBe(Visual Background extractor)背景建模或前景检测
- 运动检测(前景检测)(入门学习背景建模)
- ViBe(Visual Background extractor)背景建模或前景检测
- ViBe(Visual Background extractor)背景建模或前景检测
- 背景建模(前景检测)算法入门 上篇
- C# 委托实现窗口之间通信
- HDU1166 敌兵布阵(线段树 单点更新 区间查询)
- lua编程 全局变量 环境 模块
- POJ 2352 HDOJ 1541 Stars(树状数组)
- 正则表达式
- Opencv 自带提取前景(背景建模)
- JS正则表达式验证身份证号码
- 常规面试题-1.基础知识
- Python与anaconda3互相影响的解决方法
- c++第一次上机-1
- 41. First Missing Positive
- Android Adapter深入理解与优化
- 文件读取
- ireport 中文乱码无法显示