1.介绍
色调均化功能通常是在进行修片处理前期比较常用的功能之一,如果让我们自己实现色调均化功能,我觉得大部分人会用直方图均衡化试一试,刚好,此方法就是借用直方图均衡化,由于彩色图片一般是RGB三通道,那么我们是分开处理每个通道之后再合并,还是三个通道一起处理呢?答案见代码
2.代码
#include<opencv2\opencv.hpp>
#include<iostream>
#include<vector>
using namespace std;
using namespace cv;
void colorEqualization(const Mat& src, Mat& dst) {
vector<int>HistGram(256);
int width = src.cols;
int high = src.rows;
for (int h = 0; h < high; ++h) {
for (int w = 0; w < width; ++w) {
HistGram[src.at<Vec3b>(h, w)[0]]++;
HistGram[src.at<Vec3b>(h, w)[1]]++;
HistGram[src.at<Vec3b>(h, w)[2]]++;
}
}
int Num = 0;
vector<int>Lut(256);
for (int i = 0; i < 256; ++i) {
Num += HistGram[i];
Lut[i] = int(double(Num) / (width * high * 3) * 255);
}
for (int h = 0; h < high; ++h) {
for (int w = 0; w < width; ++w) {
dst.at<Vec3b>(h, w)[0] = Lut[src.at<Vec3b>(h, w)[0]];
dst.at<Vec3b>(h, w)[1] = Lut[src.at<Vec3b>(h, w)[1]];
dst.at<Vec3b>(h, w)[2] = Lut[src.at<Vec3b>(h, w)[2]];
}
}
}
int main() {
Mat src = imread("Lena.jpg");
Mat dst = Mat::zeros(src.size(), src.type());
colorEqualization(src, dst);
imshow("src", src);
imshow("dst", dst);
waitKey();
return 0;
}
效果图: