zoukankan      html  css  js  c++  java
  • 视频分解图片原理;图片合成视频原理

    (1)加载视频

    (2)读取视频信息

    (3)解码视频,拿到单帧信息

    帧率:视频每秒钟展示多少张图片;帧率高我们看到的图片才是连续的;帧率低我们看到的图片就相当于非连续的;

    宽度;高度

    接下来是视频保存成图片;用i计数,保存图片个数;

    flag,frame=cap.read()

    第一个参数flag的值为True或False,代表有没有读到图片

    第二个参数是frame,是当前截取一帧的图片。

    filename是图片保存名字;

    iwrite保存图片;

    (4)展示,保存图片

    /************************************************************************
    * @ Project Creation time:2018/5/22
    * @ Function:从文件中获取视频分解成图片
    ************************************************************************/
    #include<opencv2/opencv.hpp>
    #include "iostream"
    using namespace cv;
    using namespace std;
    
    
    int main()
    {
    	Mat frame;
    	char outfile[50];
    	VideoCapture cap("E:\VS2015Opencv\vs2015\project\video\01.avi");
    	if (!cap.isOpened())
    	{
    		cout << "打开视频失败!" << endl;
    		return -1;
    	}
    
    	int totalFrame = cap.get(CV_CAP_PROP_FRAME_COUNT);//<获取视频总帧数
    	for (int i = 1; i <= totalFrame; i++)
    	{
    		cap >> frame;
    		if (frame.empty())
    		{
    			cout << "图片为空!" << endl;
    			break;
    		}
    		sprintf(outfile, "E://VS2015Opencv//vs2015//project//视频处理//pic//%d.png", i);
    		imwrite(outfile, frame);
    		imshow("video", frame);
    		waitKey(15);
    	}
    	cap.release();
    	destroyAllWindows();
    	return 0;
    }
    

      

     图片合成视频原理

    写入视频;

    上面使用Python方法;但是原理一样;

    #include<opencv2opencv.hpp>
    #include<iostream>
    
    using namespace std;
    using namespace cv;
    
    int main()
    {
    
    	VideoWriter video("test.avi", CV_FOURCC('X', 'V', 'I', 'D'), 27.0, Size(1280, 720));
    
    	String img_path = "E://VS2015Opencv//vs2015//project//视频处理//pic//";
    	vector<String> img;
    
    	glob(img_path, img, false);
    
    	size_t count = img.size();
    	for (size_t i = 0; i < count; i++)
    	{
    		stringstream str;
    		str << i << ".png";
    		Mat image = imread(img_path + str.str());
    		if (!image.empty())
    		{
    			resize(image, image, Size(1280, 720));
    			video << image;
    			cout << "正在处理第" << i << "帧" << endl;
    		}
    	}
    	cout << "处理完毕!" << endl;
    	waitKey(15);
    }
    

      

    参考:学习OpenCV笔记(二)——图片与视频相互转换

    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    #include <cv.h>
    #include <highgui.h>
    #define NUM_FRAME 24560 //只处理前300帧,根据需要修改
    
    void Video_to_image(char* filename)
    {
    	printf("------video to image ------
    ");
    	//读入视频文件
    	CvCapture* pCapture = NULL;
    	if (!(pCapture = cvCaptureFromFile("pedestrain.avi")))
    	{
    		printf("Can not open video file
    ");
    		return;
    	}
    
    	//逐帧读取文件
    	IplImage* pFrame = NULL;
    	int i = 0;
    	char image_name[50];
    	while (i < NUM_FRAME)
    	{
    		pFrame = cvQueryFrame(pCapture);
    		sprintf(image_name, "%s%.5d%s", "image", ++i, ".jpg");//保存的图片名
    		cvSaveImage(image_name, pFrame);
    		pFrame = NULL;
    		//	cvReleaseImage(&pFrame); 
    	}
    	printf("--------video to image over----------
    ");
    
    	//	cvReleaseImage(&pFrame);      //释放时出错?!
    	cvReleaseCapture(&pCapture);
    }
    
    void Image_to_video()
    {
    	int i = 0;
    	IplImage* img = 0;
    	char image_name[50];
    	printf("-------- image to video --------
    ");
    
    	CvVideoWriter *writer = 0;
    	int isColor = 1;
    	int fps = 25;
    	int frameW = 640;
    	int frameH = 480;
    
    	//创建视频写入器
    	//	writer=cvCreateVideoWriter("out.avi",CV_FOURCC('X','V','I','D'),fps,cvSize(frameW,frameH),isColor);
    	writer = cvCreateVideoWriter("out.avi", CV_FOURCC('P', 'I', 'M', '1'), fps, cvSize(frameW, frameH), isColor);
    	printf("video height : %d
    video width : %d
    fps : %d
    ", frameH, frameW, fps);
    
    	//创建视频播放窗口
    	//	cvNamedWindow( "mainWin", CV_WINDOW_AUTOSIZE );
    	while (i<NUM_FRAME)
    	{
    		//		sprintf(image_name, "%s%.5d%s", "image", ++i, ".jpg");
    		sprintf(image_name, "%s%.5d%s", "AAAOUTPUT_", ++i, ".jpg.jpg");
    		img = cvLoadImage(image_name);
    		if (!img)  //图片不存在则跳过继续处理下一帧
    		{
    			//			printf("Could not load image file...
    ");
    			//			img = 0;			
    			continue;
    		}
    		cvShowImage("mainWin", img);
    		char key = cvWaitKey(20);
    		cvWriteFrame(writer, img);
    		//img = NULL;
    		cvReleaseImage(&img);
    
    	}
    	printf("--------image to video over---------
    ");
    	cvReleaseVideoWriter(&writer);
    	cvDestroyWindow("mainWin");
    	return;
    }
    
    int main(int argc, char *argv[])
    {
    	//	char *filename = argv[1];
    	char filename[50] = "pedestrain.avi";
    
    	//	Video_to_image(filename); 
    	Image_to_video();
    	return 0;
    }
    

      

  • 相关阅读:
    cacti汇总流量图-将数据合并后作图
    Linux下查看磁盘与目录的容量——df、du
    Nagios
    Linux下的压缩和解压缩命令——jar
    Linux下的压缩和解压缩命令——tar
    Linux下的压缩和解压缩命令——compress/uncompress
    Linux下的压缩和解压缩命令——gzip/gunzip
    Linux下的压缩和解压缩命令——bzip2/bunzip2
    Linux下的压缩和解压缩命令——zip/unzip
    Nagios安装
  • 原文地址:https://www.cnblogs.com/fcfc940503/p/11338082.html
Copyright © 2011-2022 走看看