#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main() {
VideoCapture capture;
capture.open("D:/b.mp4");
if (!capture.isOpened()) {
printf("could not load video");
return -1;
}
Mat frame;
Mat bsmaskMOG2;
Mat bsmaskKNN;
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
namedWindow("input_video",WINDOW_AUTOSIZE);
namedWindow("MOG2", WINDOW_AUTOSIZE);
Ptr<BackgroundSubtractor>pMOG2 = createBackgroundSubtractorMOG2();
Ptr<BackgroundSubtractor>pKNN = createBackgroundSubtractorKNN();
while (capture.read(frame)) {
imshow("input_video", frame);
pMOG2->apply(frame, bsmaskMOG2);
pKNN->apply(frame, bsmaskKNN);
morphologyEx(bsmaskMOG2, bsmaskMOG2, MORPH_OPEN, kernel, Point(-1, -1));
imshow("MOG2", bsmaskMOG2);
imshow("KNN", bsmaskKNN);
char c = waitKey(100);
if (c == 27) {
break;
}
}
capture.release();
waitKey();
return 0;
}
|