#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
void main()
{
VideoCapture cap;
cap.open("C:\\Users\\87035\\Desktop\\安全帽视频\\V90726-094954.mp4"); //打开视频,以上两句等价于VideoCapture cap("E://2.avi");
//cap.open("http://www.laganiere.name/bike.avi");//也可以直接从网页中获取图片,前提是网页有视频,以及网速够快
if (!cap.isOpened())//如果视频不能正常打开则返回
return;
stringstream sss;
string strs;
static int image_num = 0;//0,58,77,93,187,210,351
int fnum = 0;
Mat frame;
while (1)
{
cap >> frame;//等价于cap.read(frame);
if (frame.empty())//如果某帧为空则退出循环
break;
if (fnum % 5 == 0)
{
strs = "C:/helmet/helmet_0726_";
sss.clear();
sss << strs;
if (image_num < 10)
{
sss << "000";
}
if (image_num >= 10&& image_num<100)
{
sss << "00";
}
if (image_num >= 100 && image_num < 1000)
{
sss << "0";
}
sss << image_num;
sss << ".jpg";
sss >> strs;
resize(frame, frame, Size(853, 480));
frame = frame(Rect(frame.cols / 2 - 320, 0, 640, 480));
imwrite(strs, frame);//保存图片
cout << image_num << "images have saved!" << endl;
image_num++;
}
fnum++;
imshow("video", frame);
waitKey(20);//每帧延时20毫秒
}
cap.release();//释放资源
}