赞
踩
1.基本原理
读取当前帧的视频与背景相减,设立阈值进行二值化,提取前景。
2.背景获取
BS算法
-图像分割(GMM-高斯混合模型)
优点: 抑制光线干扰
-机器学习 (KNN-k个最近邻)
3.opencv实现
- #include<opencv2/opencv.hpp>
- #include <iostream>
- using namespace cv;
- using namespace std;
-
- int main(int argc,char** argv) {
- VideoCapture capture;
-
- capture.open("E:/C++/test6/video/红外视频/IR.avi");
- //VideoCapture capture(0);
- if (!capture.isOpened()) {
-
- printf("could not load video data...\n");
- return -1;
- }
- double fps = capture.get(CV_CAP_PROP_FPS);
- printf("FPS:%f",fps);
- VideoWriter writer("E:/C++/test6/video/红外视频/visit.avi",-1,15,Size(640,480),true);
- Mat frame,gray,binary;
- Mat bsmaskMOG2;
- namedWindow("video_demo",CV_WINDOW_AUTOSIZE);
- namedWindow("MOG2", CV_WINDOW_AUTOSIZE);
- Ptr<BackgroundSubtractor>pMOG2 = createBackgroundSubtractorMOG2();
- while(capture.read(frame)){
- cvtColor(frame, gray, COLOR_BGR2GRAY);
- //threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU);
- //bitwise_not(frame, frame);
- //bitwise_(frame);
- imshow("video_demo", gray);
- pMOG2->apply(frame,bsmaskMOG2);
- imshow("MOG2", bsmaskMOG2);
-
-
- //writer.write(frame);
- char c = waitKey(100);
- if (c == 27) {
- break;
- }
- }
- capture.release();
- waitKey(0);
- return 0;
-
- }
4.实验结果
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。