關鍵
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\n" << 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...\n" << 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;
}
