视频处理与自适应对比度阈值均衡化(c++版)
视频处理
读取视频帧并做处理
void VideoDemo::video_demo() {
VideoCapture cap("video/01.mp4");//读取视频文件
int frame_width = cap.get(CAP_PROP_FRAME_WIDTH);
int frame_height = cap.get(CAP_PROP_FRAME_HEIGHT);
int frame_count = cap.get(CAP_PROP_FRAME_COUNT);
double fps = cap.get(CAP_PROP_FPS);
VideoWriter wri("result/wri.mp4", cap.get(CAP_PROP_FOURCC), fps, Size(frame_width, frame_height), true);
Mat frame;
while (true) {
cap.read(frame);//frame为输出,read是将捕获到的视频一帧一帧的传入frame
//对视频读取时,同图像一样会有判空操作
if (frame.empty()) {
break;
}
int w1 = int(frame_width);
int halfw1 = int(w1 / 2);
Mat orig_frame = frame.colRange(1, halfw1).clone();
frame = clahe_deal(frame);
Mat en_frame = frame.colRange(halfw1, w1).clone();
Mat result(cv::Size(frame_width, frame_height), CV_32FC1);
hconcat(orig_frame, en_frame, result);
wri.write(result);
namedWindow("frame", cv::WINDOW_KEEPRATIO);
imshow("frame", result);
int c = waitKey(1);
if (c == 27) {
break;
}
}
cap.release();
wri.release();
}
自适应对比度阈值均衡化
static void color_transfer_with_spilt(cv::Mat& input, std::vector<cv::Mat>& chls)
{
cv::cvtColor(input, input, cv::COLOR_BGR2YCrCb);
cv::split(input, chls);
}
static void color_retransfer_with_merge(cv::Mat& output, std::vector<cv::Mat>& chls)
{
cv::merge(chls, output);
cv::cvtColor(output, output, cv::COLOR_YCrCb2BGR);
}
cv::Mat clahe_deal(cv::Mat& src)
{
cv::Mat ycrcb = src.clone();
std::vector<cv::Mat> channels;
color_transfer_with_spilt(ycrcb, channels);
cv::Mat clahe_img;
cv::Ptr<cv::CLAHE> clahe = cv::createCLAHE();
clahe->setClipLimit(2.); // (int)(4.*(8*8)/256)
clahe->setTilesGridSize(Size(8, 8)); // 将图像分为8*8块
clahe->apply(channels[0], clahe_img);
channels[0].release();
clahe_img.copyTo(channels[0]);
color_retransfer_with_merge(ycrcb, channels);
return ycrcb;
}
其他操作
矩阵的合并与拼接
int w1 = int(frame_width);
int halfw1 = int(w1 / 2);
//读取矩阵1-halfw1列的数据
Mat orig_frame = frame.colRange(1, halfw1).clone();
Mat en_frame = frame.colRange(halfw1, w1).clone();
//创建新的矩阵作为合并后的矩阵,定义矩阵时先列后行
//CV_32FC1
Mat result(cv::Size(frame_width, frame_height), CV_32FC1);
//按列合并矩阵
hconcat(orig_frame, en_frame, result);
visual studio注释
-
批量注释
先按:Ctrl + K
再按:Ctrl + C -
批量取消注释
先按:Ctrl + K
再按:Ctrl + U