Opencv 自带提取前景(背景建模)

官方教程源码稍加修改:

读入视频(图片序列)->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 variables
Mat frame; //current frame
Mat fgMaskMOG2; //fg mask fg mask generated by MOG2 method
Ptr<BackgroundSubtractor> pMOG2; //MOG2 Background subtractor
int keyboard; //input from keyboard
void 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的监控视频,速度有些慢,如果对没帧进行缩小可能会好些。

效果:视频中某帧



~~代码可执行,有问题留言~~

  • 1
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Opencv背景提取是指通过使用Opencv库中的相关函数和方法来将图像中的前景背景进行分离或提取的技术方法。 Opencv提供了多种背景提取的方法,其中最常用的是通过建模的方法来进行背景提取。具体步骤如下: 1. 读取图像:首先使用Opencv的函数读取待处理的图像。 2. 背景建模:选取一段时间内的连续帧作为背景建模帧,通过计算这些帧的像素平均值或中值来建立初始的背景模型。 3. 前景检测:对于每一帧图像,与背景模型进行比较,将与背景有明显差异的像素标记为前景像素。 4. 前景分割:通过对前景像素进行分割,将前景背景中分离出来。 5. 背景更新:由于背景可能会发生变化,需要对背景模型进行动态更新,以适应场景的变化。 Opencv提供了多种背景建模算法,如Gaussian Mixture Model(GMM)、K-nearest neighbor (KNN)以及自适应混合高斯背景建模等。这些算法根据场景的复杂度和需要的精度选择合适的方法。 背景提取在很多应用中都非常有用,比如视频监控、行人检测、运动分析等。通过背景提取,可以将关注点集中在前景信息上,从而提高图像分析和处理的效果。 总之,Opencv背景提取是通过建立背景模型和前景检测来实现对图像中前景背景的分离的技术方法,通过选择合适的背景建模算法和更新策略,可以根据具体需求实现高质量的背景提取

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值