Background类提取前景物体然后保存为图片
示例代码:
#include<opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/background_segm.hpp>
#include <time.h>
#include <Windows.h>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
VideoCapture capture("E:/图片/bike.avi");
if (!capture.isOpened())
return 0;
Mat frame;
Mat foreground;
namedWindow("Extracted Foreground");
BackgroundSubtractorMOG mog;
bool stop(false);
int i = 0;
while (!stop)
{
if (!capture.read(frame))
break;
mog(frame,foreground,0.01);
threshold(foreground,foreground,128,255,THRESH_BINARY_INV);
/*变量的定义*/
char image_name[13];
sprintf(image_name, "%s%d%s", "image", ++i, ".jpg"); //保存的图片名
imwrite( image_name, foreground); //保存一帧图片
//
imshow("Extracted Foreground",foreground);
if (waitKey(10)>=0)
stop= true;
}
waitKey();
}
运行结果: