视频写操作,通道分离与合并

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
// 视频读入与输出路径设置
string sourceVideoPath = "..\\images\\test.avi";
string outputVideoPath = "..\\images\\testWrite.avi";
// 视频输入
VideoCapture inputVideo(sourceVideoPath);
// 检测视频输入的有效性      
if (!inputVideo.isOpened())
{
cout << "fail to open!" << endl;
return -1;
}
VideoWriter outputVideo;
// 获取视频分辨率
cv::Size videoResolution = cv::Size((int)inputVideo.get(
CV_CAP_PROP_FRAME_WIDTH), (int)inputVideo.get(
CV_CAP_PROP_FRAME_HEIGHT));
double fps = inputVideo.get(CV_CAP_PROP_FPS);
// 获取视频帧率
cout << "totalFrame:" << inputVideo.get(
CV_CAP_PROP_FRAME_COUNT) << endl;
// 获取视频总帧数   
cout << "fps:" << inputVideo.get(CV_CAP_PROP_FPS) << endl;
// 获取视频图像宽高
cout << "videoResolution:" << videoResolution.width <<
" " << videoResolution.height << endl;
// open方法相关设置 
outputVideo.open(outputVideoPath, -1, 25.0,
videoResolution, true);
if (!outputVideo.isOpened())
{
cout << "fail to open!" << endl;
return -1;
}
cv::Mat frameImg;
int count = 0;
// vector RGB分量
std::vector< cv::Mat > rgb;
cv::Mat resultImg;
for (int i = 0; i < 30; i++)
{
inputVideo >> frameImg;
// 视频帧结束判断
if (!frameImg.empty())
{
count++;
cv::imshow("frameImg", frameImg);
// 分离出三通道rgb
cv::split(frameImg, rgb);
for (int i = 0; i < 3; i++)
{
if (i != 0)
{
// 提取B通道分量
rgb[i] = cv::Mat::zeros(videoResolution,
rgb[0].type());
}
// 通道合并
cv::merge(rgb, resultImg);
}
cv::imshow("resultImg", resultImg);
outputVideo << resultImg;
}
else
{
break;
}
// 按下键盘上q键退出
if (char(waitKey(1)) == 'q')
{
inputVideo.release();
outputVideo.release();
break;
}
}
std::cout << "writeTotalFrame:" << count << std::endl;

inputVideo.release();
outputVideo.release();
return 0;
}

转载:http://blog.csdn.net/zhuwei1988


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值