OpenCV 4.12.0
開源計算機視覺
載入中...
搜尋中...
無匹配項
samples/cpp/tutorial_code/videoio/video-write/video-write.cpp

檢視對應的教程以瞭解更多詳情

#include <iostream> // 用於標準I/O
#include <string> // 用於字串
#include <opencv2/core.hpp> // 基本OpenCV結構 (cv::Mat)
#include <opencv2/videoio.hpp> // 影片寫入
using namespace std;
using namespace cv;
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'; // 如果為false,它將使用輸入的編解碼器型別
VideoCapture inputVideo(source); // 開啟輸入
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)); // 獲取編解碼器型別 - Int形式
// 透過位運算子從int轉換為char
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));
VideoWriter outputVideo; // 開啟輸出
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);
if (!outputVideo.isOpened())
{
cout << "無法開啟輸出影片以進行寫入: " << source << endl;
return -1;
}
cout << "輸入幀解析度: 寬度=" << S.width << " 高度=" << S.height
<< " 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;
}
Mat src, res;
vector<Mat> spl;
for(;;) //在視窗中顯示捕獲的影像並重復
{
inputVideo >> src; // 讀取
if (src.empty()) break; // 檢查是否在結尾
split(src, spl); // 處理 - 僅提取正確的通道
for (int i =0; i < 3; ++i)
if (i != channel)
spl[i] = Mat::zeros(S, spl[0].type());
merge(spl, res);
//outputVideo.write(res); //儲存或
outputVideo << res;
}
cout << "完成寫入" << endl;
return 0;
}
n 維密集陣列類
定義 mat.hpp:830
用於指定影像或矩形大小的模板類。
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
定義 core.hpp:107
STL 名稱空間。