opencv处理视频,resize然后取中间ROI,保存成一张张有序号图像

 

#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();//释放资源
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值