招聘網(wǎng)頁(yè)設(shè)計(jì)師全域seo
- 操作系統(tǒng):ubuntu22.04
- OpenCV版本:OpenCV4.9
- IDE:Visual Studio Code
- 編程語(yǔ)言:C++11
算法描述
cv::VideoWriter::write() 函數(shù)用于將圖像幀寫入視頻文件。
該函數(shù)/方法將指定的圖像寫入視頻文件。圖像的大小必須與打開(kāi)視頻編寫器時(shí)指定的大小相同。
函數(shù)原型
virtual void cv::VideoWriter::write
(InputArray image
)
參數(shù)
- 參數(shù)image 被寫入的幀。一般來(lái)說(shuō),期望的是 BGR 格式的彩色圖像。
代碼示例
#include <iostream>
#include <opencv2/opencv.hpp>int main()
{// 設(shè)置視頻的寬度和高度int frameWidth = 640;int frameHeight = 480;// 設(shè)置視頻編碼器的 FourCC 代碼// 使用 XVID 編碼器作為替代方案int fourcc = cv::VideoWriter::fourcc( 'X', 'V', 'I', 'D' );// 創(chuàng)建 VideoWriter 對(duì)象cv::VideoWriter writer;// 初始化 VideoWriter 對(duì)象bool isOpened = writer.open( "output.avi", fourcc, 25, cv::Size( frameWidth, frameHeight ), true );if ( !isOpened ){std::cerr << "Failed to initialize the video writer." << std::endl;return -1;}// 創(chuàng)建一個(gè)示例幀cv::Mat frame = cv::Mat::zeros( frameHeight, frameWidth, CV_8UC3 );// 寫入一幀到視頻文件writer.write( frame );// 再次創(chuàng)建一個(gè)不同的幀cv::Mat anotherFrame = cv::Mat::ones( frameHeight, frameWidth, CV_8UC3 ) * 255;// 寫入另一幀到視頻文件writer.write( anotherFrame );// 釋放資源writer.release();return 0;
}