opencv 打开视频或摄像头并按格式保存图片,读取图片

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv.hpp>
#include <iostream>
#include <stdio.h>
using namespace cv;
using namespace std;


//打开并保存视频的图片
int videoProcess(VideoCapture& capture)
{
	int n = 0;
	char filename[200];
	char numImage[200] = "images";
	string window_name = "KINGDOM OF PLANTS";
	std::cout << "按下【Space】空格键可以截图" << std::endl;
	std::cout << "【Esc】和【q】键 - 退出程序" << endl;
	namedWindow(window_name, CV_WINDOW_KEEPRATIO); //resizable	window;
	cv::Mat frame;
	for (;;)
	{
		capture >> frame;
		if (frame.empty())break;
		putText(frame, "Progress are not made by contented people", Point(10, 80), 1, 2, CV_RGB(255, 0, 0));
		//显示了保存了几张图片
		putText(frame, numImage, Point(10, 150), 3, 2, CV_RGB(0, 0, 255));
		cv::imshow(window_name, frame);
		char key = (char)waitKey(10);
		switch (key)
		{
		case 'q':
		case 'Q':
		case 27:
			//[1]escape key
			return 0;
		case ' ': //[2]Save an image
			sprintf(filename, "F:\\downloads\\%.3d.png", n++);
			sprintf(numImage, "%.2d.png", n);
			imwrite(filename, frame);
			cout << "\n\t>保存了 " << filename << "文件到工程目录下" << endl;
			break;
		default:
			break;
		}
	}
	return 0;
}

//读取上面按格式保存的图片
//define Width and Height
const int width = 600;
const int height = 800;
int readImages()
{
	Mat src;
	int i;
	string src_image_name = "F:\\downloads\\";
	string src_image_name1 = src_image_name;
	char num[3], num1[3] = "00", num2[5] = ".png";
	vector<string > images;
	for (i = 0; i <= 3; i++)
	{
		//图片的名字
		_itoa(i, num, 10);
		src_image_name += num1;
		src_image_name += num;
		src_image_name += num2;
		images.push_back(src_image_name);
		//输出图片的名字
		cout << src_image_name << endl;
		//从指定文件夹读入图片
		src = imread(src_image_name);
		if (!src.data)//判断是否成功
		{
			cout << "failed read images" << endl;
			getchar();
			return -1;
		}
		imshow(src_image_name, src);
		src_image_name = src_image_name1;
		//waitKey();
		waitKey(1000);
	}
}

int main(int argc, char** argv)
{
	argv++;
	VideoCapture capture("Kingdom.of.Plants.EP03.mkv"); //从文件载入视频
	//VideoCapture capture(*argv); //从文件载入视频
		//VideoCapture capture(0); //从摄像头载入视频
	if (!capture.isOpened())
	{
		std::cerr << "Failed to open a video device or video file!\n" << endl;
		return 1;
	}
	//函数应该加判断提高鲁棒性
	 videoProcess(capture);
	 readImages();

	 return 1;
}

  • 2
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值