视频分解图片原理;图片合成视频原理
生活随笔
收集整理的這篇文章主要介紹了
视频分解图片原理;图片合成视频原理
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
(1)加載視頻
(2)讀取視頻信息
(3)解碼視頻,拿到單幀信息
幀率:視頻每秒鐘展示多少張圖片;幀率高我們看到的圖片才是連續(xù)的;幀率低我們看到的圖片就相當(dāng)于非連續(xù)的;
寬度;高度
接下來是視頻保存成圖片;用i計數(shù),保存圖片個數(shù);
flag,frame=cap.read()
第一個參數(shù)flag的值為True或False,代表有沒有讀到圖片
第二個參數(shù)是frame,是當(dāng)前截取一幀的圖片。
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);//<獲取視頻總幀數(shù)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<opencv2\opencv.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); }
參考:學(xué)習(xí)OpenCV筆記(二)——圖片與視頻相互轉(zhuǎn)換
#include <stdlib.h> #include <stdio.h> #include <math.h> #include <cv.h> #include <highgui.h> #define NUM_FRAME 24560 //只處理前300幀,根據(jù)需要修改void Video_to_image(char* filename) {printf("------video to image ------\n");//讀入視頻文件CvCapture* pCapture = NULL;if (!(pCapture = cvCaptureFromFile("pedestrain.avi"))){printf("Can not open video file\n");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----------\n");// cvReleaseImage(&pFrame); //釋放時出錯?!cvReleaseCapture(&pCapture); }void Image_to_video() {int i = 0;IplImage* img = 0;char image_name[50];printf("-------- image to video --------\n");CvVideoWriter *writer = 0;int isColor = 1;int fps = 25;int frameW = 640;int frameH = 480;//創(chuàng)建視頻寫入器// 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\nvideo width : %d\nfps : %d\n", frameH, frameW, fps);//創(chuàng)建視頻播放窗口// 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) //圖片不存在則跳過繼續(xù)處理下一幀{// printf("Could not load image file...\n");// img = 0; continue;}cvShowImage("mainWin", img);char key = cvWaitKey(20);cvWriteFrame(writer, img);//img = NULL;cvReleaseImage(&img);}printf("--------image to video over---------\n");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; }
轉(zhuǎn)載于:https://www.cnblogs.com/fcfc940503/p/11338082.html
總結(jié)
以上是生活随笔為你收集整理的视频分解图片原理;图片合成视频原理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【教程】opencv-python+yo
- 下一篇: 给 DataGrid 的删除信息添加提示