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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/moonlightpeng/article/details/80933559
#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;
}

展开阅读全文

没有更多推荐了,返回首页