第一次写博客,主要是想记录一下自己的学习心得,学习内容等。
本人刚开始接触视觉领域,对这方面还是比较感兴趣的。从学习OpenCV开始,OpenCV上手还是比较容易的,在应用中学习,不懂多就多百度,脚踏实地的来。
代码区
废话不多说直接上代码:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
//打开视频文件,建立VideoCapture类
VideoCapture capture("HumanCam.avi");
//创建VideoWriter类
VideoWriter writer("VideoTest.avi", CV_FOURCC('M', 'J', 'P', 'G'), 12.0, Size(320, 240));
if (!capture.isOpened())
cout << "fail to open!" << endl;
//承载每一帧的图像
Mat frame, frame_a, frame_b, frame_c;
//定义对比度值、亮度值、饱和度值
double ContrastValue, BrightValue, SaturationValue;
//用来分离通道
vector<Mat> channels_a, channels_b;
//可以定义滤波器的核
//Mat kernel = Mat::ones(3, 3, CV_32F) / 9;
/*Mat kernel2 = (Mat_<char>(3, 3) << 0, -1, 0,
-1, 5, -1,
0, -1, 0);*/
while (1)
{
//读取下一帧
if (!capture.read(frame))
{
cout << "读取视频失败" << endl;
return -1;
}
//显示原图
imshow("Extracted frame", frame);
// 去除红色通道值,并将绿色通道值赋给红色通道
split(frame, channels_a);
channels_a.at(2) = channels_a.at(1);
merge(channels_a, frame_a);
imshow("after split", frame_a);
//调节饱和度与亮度
//从RGB空间转换到HSV空间
cvtColor(frame_a, frame_b, CV_BGR2HSV);
split(frame_b, channels_b);
//降低70%的亮度,亮度不变
SaturationValue = 0.3;
BrightValue = 1.0;
channels_b.at(1).convertTo(channels_b.at(1), CV_8U, SaturationValue, 0);
channels_b.at(2).convertTo(channels_b.at(2), CV_8U, BrightValue, 0);
merge(channels_b, frame_b);
cvtColor(frame_b, frame_b, CV_HSV2BGR);
imshow("after saturation and brightness adjustment", frame_b);
//均值滤波
//filter2D(frame_b, frame_c, -1, kernel);
//高斯滤波
GaussianBlur(frame_b, frame_c, Size(19, 19), 0, 0);
imshow("after filter", frame_c);
//写视频
writer << frame_c;
waitKey(20);
}
//关闭视频文件
capture.release();
waitKey(0);
return 0;
}