混合高斯模型加形态学处理检测运动目标

#include   
#include   
#include   
#include 
#include 
#include 
#include 
#pragma warning(disable:4996)
using namespace std; 
using namespace cv;
int main()
{

    DeleteErrorObject delecterrorobject;
    VideoCapture video("C:\\Users\\ThinkCentre\\Desktop\\数学建模\\附件3-检测前景视频\\campus\\Campus\\input.avi");
    string filenames1 = "C:\\Users\\ThinkCentre\\Desktop\\数学建模\\附件2-典型视频\\campus.avi";
    string filenames2 = "C:\\Users\\ThinkCentre\\Desktop\\数学建模\\附件2-典型视频\\campus.avi";
    VideoWriter outputVideo1(filenames1, CV_FOURCC('M', 'J', 'P', 'G'), 25.0, Size(160, 128), false);
    VideoWriter outputVideo2(filenames2, CV_FOURCC('M', 'J', 'P', 'G'), 25.0, Size(160, 128), false);
    int frame_number,total_frame_number;
    Mat frame, mask, thresholdImage, output, saliencymap, bg;
    Mat pre_img;
    Mat flow_img;
    Mat motion2color;
    Mat detect_bgSubtractor;
    BackgroundSubtractorMOG bgSubtractor(10, 20, 0.8, false);
    //腐蚀膨胀算子
    Mat element_erode = getStructuringElement(MORPH_RECT, Size(3, 3));
    Mat element_dilate = getStructuringElement(MORPH_RECT, Size(5, 5));
    Mat new_mask_img;
    //
    if (!video.isOpened())
    {
        cout << "please try again!" << endl;
        return (-1);
    }
    else
    {
        total_frame_number = int(video.get(CV_CAP_PROP_FRAME_COUNT));
        for (frame_number = 0; frame_number < total_frame_number; frame_number++)
        {
            video >> frame;
            namedWindow("frame");
            imshow("frame", frame);
            //高斯
            bgSubtractor(frame, mask, 0.001);
            bgSubtractor.getBackgroundImage(bg);
            //******************************************************
            //写入视频
            outputVideo1 << mask;
            new_mask_img = mask.clone();
            //******************************************************
            //腐蚀膨胀
            erode(new_mask_img, new_mask_img, element_erode);
            dilate(new_mask_img, new_mask_img, element_dilate);
            outputVideo2 << new_mask_img;
            //******************************************************
            namedWindow("new_mask_img");
            imshow("mask", new_mask_img);
            namedWindow("mask");
            imshow("mask", mask);
            waitKey(60);
        }
    }
    return 0;
}

你可能感兴趣的:(图像处理,C++,opencv,机器学习)