zoukankan      html  css  js  c++  java
  • OpenCV 学习笔记(13)图像转换成视频

    关键

    1参数里的分辨率是图像本身的分辨率,而不是指定生成的视频分辨率。如果要修改分辨率,要么后期软件处理,要么读图的时候resize

    2要正常退出,不要强制退出。

    3生成的只能是avi格式。

    #include <iostream>
    #include <string>
    #include "opencv2/highgui/highgui.hpp"
    #include "opencv2/core/core.hpp"
    #include "opencv2/opencv.hpp"
    
    using namespace std;
    using namespace cv;
    void  Video_To_Image(string filename);
    void  Image_To_Video();
    int imagetovideo();
    int main()
    {
    	//string video_name = "test.avi";//注意,使用string时,若不用using namespace std,需要使用std::string
    	//Video_To_Image(video_name);
    	Image_To_Video();
    	//imagetovideo();
    	return 0;
    }
    
    
    void Video_To_Image(string filename)
    {
    	cout << "---------------Video_To_Image-----------------" << endl;
    	cv::VideoCapture capture(filename);
    	if (!capture.isOpened())
    	{
    		cout << "open video error";
    	}
    	/*CV_CAP_PROP_POS_MSEC – 视频的当前位置(毫秒)
    	CV_CAP_PROP_POS_FRAMES – 视频的当前位置(帧)
    	CV_CAP_PROP_FRAME_WIDTH – 视频流的宽度
    	CV_CAP_PROP_FRAME_HEIGHT – 视频流的高度
    	CV_CAP_PROP_FPS – 帧速率(帧 / 秒)*/
    	int frame_width = (int)capture.get(CV_CAP_PROP_FRAME_WIDTH);
    	int frame_height = (int)capture.get(CV_CAP_PROP_FRAME_HEIGHT);
    	float frame_fps = capture.get(CV_CAP_PROP_FPS);
    	int frame_number = capture.get(CV_CAP_PROP_FRAME_COUNT);//总帧数
    	cout << "frame_width is " << frame_width << endl;
    	cout << "frame_height is " << frame_height << endl;
    	cout << "frame_fps is " << frame_fps << endl;
    
    	int num = 0;//统计帧数
    	cv::Mat img;
    	string img_name;
    	char image_name[20];
    	cv::namedWindow("MyVideo", CV_WINDOW_AUTOSIZE);
    	while (true)
    	{
    		cv::Mat frame;
    		//从视频中读取一个帧
    		bool bSuccess = capture.read(frame);
    		if (!bSuccess)
    		{
    			cout << "不能从视频文件读取帧" << endl;
    			break;
    		}
    		//在MyVideo窗口上显示当前帧
    		imshow("MyVideo", frame);
    		//保存的图片名
    		//sprintf(const_cast<char*>(img_name.data()), "%s%d%s", "image", ++num, ".jpg");//保存的图片名
    		sprintf(image_name, "%s%d%s", "image", ++num, ".jpg");//保存的图片名
    		img_name = image_name;
    		imwrite(img_name, frame);//保存保存一帧图片
    		if (cv::waitKey(30) == 27 || num == frame_number)
    		{
    			cout << "按下ESC键" << endl;
    			break;
    		}
    	}
    	capture.release();//这句话貌似不需要
    }
    
    
    
    void Image_To_Video()
    {
    	cout << "---------------Video_To_Image-----------------" << endl;
    	
    	string s_image_name;
    	cv::VideoWriter writer;
    	int isColor = 1;//不知道是干啥用的
    	int frame_fps = 25;
    	int frame_width = 1920;   //必须是图像真实的分辨率,这不是指定生成视频的分辨率
    	int frame_height = 1080;
    
    	string video_name = "out.avi";
    	//CV_FOURCC('M', 'J', 'P', 'G')   CV_FOURCC('D', 'I', 'V', 'X')
    	writer = VideoWriter(video_name, CV_FOURCC('D', 'I', 'V', 'X'), frame_fps, Size(frame_width, frame_height), isColor);
    	cout << "frame_width is " << frame_width << endl;
    	cout << "frame_height is " << frame_height << endl;
    	cout << "frame_fps is " << frame_fps << endl;
    	cv::namedWindow("image to video", CV_WINDOW_AUTOSIZE);
    	int num = 50;//输入的图片总张数
    	int i = 1;
    	Mat img;
    
    
    	if (!writer.isOpened())
    	{
    		cout << "Error : fail to open video writer
    " << endl;
    		return ;
    	}
    
    
    	while (i <= num)
    	{
    		string path = "F:/dongdong/1学习资料/1论文/2发表/稠密轨迹跟踪/最新论文/代码/0数据/光流跟踪/result/test2/2GUI/1/";
    		s_image_name = path + std::to_string(i++) + ".jpg";
    		img = imread(s_image_name);//读入图片
    		if (!img.data)//判断图片调入是否成功
    		{
    			cout << "Could not load image file...
    " << endl;
    			
    			break;
    		}
    		//imshow();
    		//写入
    		
    		writer << img;
    
    		imshow("image to video", img);
    		waitKey(1);
    
    		if (cv::waitKey(30) == 27 || i == num)
    		{
    			cout << "按下ESC键" << endl;
    			cvReleaseVideoWriter;
    			break;
    		}
    	}
    	cvReleaseVideoWriter;
    }
    

      

  • 相关阅读:
    Oracle 多表查询优化
    FZU 2150 Fire Game
    HDU 1058 Humble Numbers
    UVA 11624 Fire!
    POJ 1321 棋盘问题
    线性表的基本操作
    POJ 3414 Pots(广搜输出路径)
    HDU 1495 非常可乐
    POJ 1847 Tram
    POJ 3159 Candies(查分约束)
  • 原文地址:https://www.cnblogs.com/kekeoutlook/p/11334229.html
Copyright © 2011-2022 走看看