#include <iostream>
#include <string>
static void help()
{
cout
<< "------------------------------------------------------------------------------" << endl
<< "此程式展示如何寫入影片檔案。" << endl
<< "您可以提取輸入影片的R或G或B顏色通道。" << endl
<< "用法:" << endl
<< "./video-write <input_video_name> [ R | G | B] [Y | N]" << endl
<< "------------------------------------------------------------------------------" << endl
<< endl;
}
int main(
int argc,
char *argv[])
{
help();
if (argc != 4)
{
cout << "引數不足" << endl;
return -1;
}
const string source = argv[1];
const bool askOutputType = argv[3][0] =='Y';
if (!inputVideo.isOpened())
{
cout << "無法開啟輸入影片: " << source << endl;
return -1;
}
string::size_type pAt = source.find_last_of('.');
const string NAME = source.substr(0, pAt) + argv[2][0] + ".avi";
int ex = static_cast<int>(inputVideo.get(CAP_PROP_FOURCC));
char EXT[] = {(char)(ex & 0XFF) , (char)((ex & 0XFF00) >> 8),(char)((ex & 0XFF0000) >> 16),(char)((ex & 0XFF000000) >> 24), 0};
Size S =
Size((
int) inputVideo.get(CAP_PROP_FRAME_WIDTH),
(int) inputVideo.get(CAP_PROP_FRAME_HEIGHT));
if (askOutputType)
outputVideo.
open(NAME, ex=-1, inputVideo.get(CAP_PROP_FPS), S,
true);
else
outputVideo.
open(NAME, ex, inputVideo.get(CAP_PROP_FPS), S,
true);
{
cout << "無法開啟輸出影片以進行寫入: " << source << endl;
return -1;
}
<< " of nr#: " << inputVideo.get(CAP_PROP_FRAME_COUNT) << endl;
cout << "輸入編解碼器型別: " << EXT << endl;
int channel = 2;
switch(argv[2][0])
{
case 'R' : channel = 2; break;
case 'G' : channel = 1; break;
case 'B' : channel = 0; break;
}
vector<Mat> spl;
for(;;)
{
inputVideo >> src;
if (src.empty()) break;
for (int i =0; i < 3; ++i)
if (i != channel)
spl[i] = Mat::zeros(S, spl[0].type());
outputVideo << res;
}
cout << "完成寫入" << endl;
return 0;
}
用於指定影像或矩形大小的模板類。
Definition types.hpp:335
_Tp height
高度
Definition types.hpp:363
_Tp width
寬度
Definition types.hpp:362
用於從影片檔案、影像序列或相機捕獲影片的類。
Definition videoio.hpp:772
影片寫入類。
定義 videoio.hpp:1071
virtual bool open(const String &filename, int fourcc, double fps, Size frameSize, bool isColor=true)
初始化或重新初始化影片寫入器。
virtual bool isOpened() const
如果影片寫入器已成功初始化,則返回 true。
void split(const Mat &src, Mat *mvbegin)
將多通道陣列劃分為多個單通道陣列。
void merge(const Mat *mv, size_t count, OutputArray dst)
從幾個單通道陣列建立一個多通道陣列。
int main(int argc, char *argv[])
定義 highgui_qt.cpp:3