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;
}
效果圖: