#include <opencv2/opencv.hpp> using namespace std;
using namespace CV;
int main (int argc, char** argv) {videocapture capture ("2.avi");
if (!capture.isopened ()) {cout<< "No camera or video input!\n" <<endl;
return-1;
} Mat frame;
Mat Gray;
Mat gray_dilate1;
Mat Gray_dilate2;
Mat Gray_dilate3;
Mat BACKGROUND,FOREGROUND,FOREGROUND_BW; Mat Mid_filer; Photo//---------------------------------------------------------------------with median filter//Get video width, height, frame rate, total frame number int Frameh = Capture.get (cv_cap_prop_frame_height); Gets the frame high int framew = Capture.get (cv_cap_prop_frame_width); Gets the frame width int fps = capture.get (cv_cap_prop_fps); Gets the frame rate int numframes = Capture.get (Cv_cap_prop_frame_count);
Gets the entire frame number int num=numframes;
printf ("vedio ' \nwidth =%d\t height =%d\n Video ' s fps =%d\t Nums =%d", Framew, Frameh, FPS, numframes); //---------------------------------------------------------------------Mat Frame_0,frame_1;//mat m (3, 5, CV_32FC1, 1);
---------------------------------------------------------------------while (1) {capture>>frame;
Imshow ("Frame_resize", frame);
Cvtcolor (Frame,gray, Cv_rgb2gray); -----------------------------------------------------------------------------------//Select the previous frame as the background (when you read the first frame,
First frame as background) if (num==numframes) {background=gray.clone ();
Frame_0=background;
else {background=frame_0; }//------------------------------------------------------------------------------------Absdiff (gray,background
, foreground)//////////Imshow ("foreground", foreground) by frame-difference method;
Mat element = Getstructuringelement (Morph_rect,size (5,5)); Threshold (foreground, FOREGROUND_BW, 30, 255, 0);//Two The value is usually set to 255//threshold (foreground, FOREGROUND_BW, 0, 255, C V_thresh_binary | Cv_thresh_otsu);
The Dajing method is used to adaptively take the threshold value imshow ("FOREGROUND_BW", FOREGROUND_BW); Medianblur (foreground_bw,mid_filer,3);
Median filtering method Imshow ("Mid_filer", Mid_filer);
Dilate (mid_filer,gray_dilate1,element);
Imshow ("Gray_dilate1", gray_dilate1);
Dilate (gray_dilate1,gray_dilate2,element);
Imshow ("Gray_dilate2", Gray_dilate2);
Dilate (gray_dilate2,gray_dilate3,element);
Imshow ("Gray_dilate3", gray_dilate3);
Frame_0=gray.clone ();
num--;
char C = waitkey (33);
if (c ==27) break;
if (num < 1) return-1;
}
}The experimental results are as follows: