#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <stdio.h>

#define threshold_diff1 25 //设置简单帧差法阈值
#define threshold_diff2 25 //设置简单帧差法阈值

using namespace cv;
using namespace std;

int main(int argc,unsigned char* argv[])
{
    Mat img_src1,img_src2,img_src3;//3帧法需要3帧图片
    Mat img_dst,gray1,gray2,gray3;
    Mat gray_diff1,gray_diff2;//存储2次相减的图片
    Mat gray_diff11,gray_diff12;
    Mat gray_diff21,gray_diff22;
    Mat gray;//用来显示前景的
    bool pause=false;

    VideoCapture vido_file("/home/danoo/b60.avi");//在这里改相应的文件名
    namedWindow("foreground",0);
    for (;;)
    {
    if(!false)
    {
        vido_file >>img_src1;
        cvtColor(img_src1,gray1,CV_BGR2GRAY);

        waitKey(33);
        vido_file >>img_src2;
        cvtColor(img_src2,gray2,CV_BGR2GRAY);
        imshow("video_src",img_src2);//

        waitKey(33);
        vido_file >>img_src3;
        cvtColor(img_src3,gray3,CV_BGR2GRAY);

        Sobel(gray1,gray1,  CV_8U,1,0,3,0.4,128);
        Sobel(gray2,gray2,  CV_8U,1,0,3,0.4,128);
        Sobel(gray3,gray3,  CV_8U,1,0,3,0.4,128);

        subtract(gray2,gray1,gray_diff11);//第二帧减第一帧
        subtract(gray1,gray2,gray_diff12);
        add(gray_diff11,gray_diff12,gray_diff1);
        subtract(gray3,gray2,gray_diff21);//第三帧减第二帧
        subtract(gray2,gray3,gray_diff22);
        add(gray_diff21,gray_diff22,gray_diff2);

        for(int i=0;i<gray_diff1.rows;i++)
        for(int j=0;j<gray_diff1.cols;j++)
        {
            if(abs(gray_diff1.at<unsigned char>(i,j))>=threshold_diff1)//这里模板参数一定要用unsigned char,否则就一直报错
            gray_diff1.at<unsigned char>(i,j)=255;            //第一次相减阈值处理
            else gray_diff1.at<unsigned char>(i,j)=0;

            if(abs(gray_diff2.at<unsigned char>(i,j))>=threshold_diff2)//第二次相减阈值处理
            gray_diff2.at<unsigned char>(i,j)=255;
            else gray_diff2.at<unsigned char>(i,j)=0;
        }
        bitwise_and(gray_diff1,gray_diff2,gray);

        dilate(gray,gray,Mat());erode(gray,gray,Mat());

        imshow("foreground",gray);
    }
    if( cvWaitKey(33) >= 0 )
        break;
    }
    return 0;
}