GMM背景建模与前景提取

#include 
#include 
#include 
#include 
#include 

#include 
#include 
#include 

using namespace std;
using namespace cv; 

int main()
{
    string file = "/home/zhanghan/itemloss/myself/video/2.mp4";
    VideoCapture cap(file);

    Mat frame,frameGray;
    Mat foreground,fgmask;
    Mat bgimg;

    Ptr mog = createBackgroundSubtractorMOG2();
                                                                                      
    bool success = true;
    int num = 1;
    while(success)
    {   
        if(!cap.read(frame))
            success = false;
        mog->apply(frame,fgmask,-1);
        mog->getBackgroundImage(bgimg);
        string name = "cap_" + to_string(num) + ".jpg";
        imwrite(name,fgmask);
        ++num;
    }   
    return 0;
}

2.

#ifndef _DETECTOR_H
#define _DETECTOR_H
#include 
#include 
#include 
#include 
#include 

#include 

using namespace std;
using namespace cv;

class Detector{

public:
	Detector();
	~Detector();
	void SetThresh(const int& area);
	bool Detect(const Mat& img);
private:
	Ptr mog;
	bool alarm = false;
	Mat fgmask;
	int thresh;
	int num;
};
#endif

 

3.

#include "detect.hpp"
using namespace cv;

Detector::Detector()
{
	mog = createBackgroundSubtractorMOG2();
	num = 0;
};
Detector::~Detector(){};

void Detector::SetThresh(const int& area)
{
	thresh = area;
};

bool Detector::Detect(const Mat& img)
{
	Ptr mog;
	mog->apply(img,fgmask,-1);
	num += 1;
	string name = "cap_" + to_string(num) + ".jpg";
	imwrite(name,fgmask);
};

 

 

4.

#!/usr/bin env sh
#g++ gmm.cpp -std=c++11 \
#	-L/usr/lib/x86_64-linux-gnu/ \
#	-I/usr/include/opencv/ \
#	-I/usr/include/opencv2/ \
#	 -o gmm

g++ gmm.cpp -std=c++11 \
	-I/usr/include/opencv \
	-I/usr/include/opencv2 \
	-L/usr/lib/x86_64-linux-gnu/ \
	-lopencv_highgui -lopencv_imgproc -lopencv_core \
	-o gmm

 

你可能感兴趣的:(深度学习)