色调均化:彩色影像统计三个颜色通道的直方图的累积概率 --> 计算色阶映射表-->映射颜色

 

#include <opencv2/opencv.hpp>
#include <iostream>
#include <algorithm>
#include <vector>

bool ImgEqualization(cv::Mat& img)
{
	std::vector<cv::Mat> splitImg(3);
	cv::split(img, splitImg);

	int histSize = 256;
	float range[] = { 0, 256 };
	const float* histRange = { range };
	bool uniform = true; 
	bool accumulate = false;
	cv::Mat b_hist, g_hist, r_hist;
	cv::calcHist(&splitImg[0], 1, 0, cv::Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate);
	cv::calcHist(&splitImg[1], 1, 0, cv::Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate);
	cv::calcHist(&splitImg[2], 1, 0, cv::Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate);

	cv::Mat hist = b_hist.clone();
	hist += g_hist;
	hist += r_hist;
	{
		hist /= (img.rows *img.cols *3.0);
		float* ptr = (float*)hist.data;
		for (int i = 1; i < hist.rows; i++) {
			ptr[i] += ptr[i - 1];
		}
	}

	{
		float* ptr = (float*)hist.data;
		for (int i = 0; i < img.rows; i++) {
			cv::Vec3b* dstImgPtr = img.ptr<cv::Vec3b>(i);
			for (int j = 0; j < img.cols; j++) {
				dstImgPtr[j][0] = ptr[dstImgPtr[j][0]] * 255;
				dstImgPtr[j][1] = ptr[dstImgPtr[j][1]] * 255;
				dstImgPtr[j][2] = ptr[dstImgPtr[j][2]] * 255;
			}
		}
	}

	return true;
}

原始影像
色调均化后影像
原始影像
色调均化后影像

 

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐