最近扒拉了一些光線補償算法的實現,可能是能力比較有限,看到的大多是是基於Face detection in color images是這篇論文的實現。
從效果上來看,的確起到了明亮、美白的效果。但是從代碼本身來看,最終的結果只是分別對各通道進行一個有控制的伸展。只不過這個伸展的彈性是“自適應”的,這里我就疑問:這樣就能夠起到去除影音的效果了嗎?還是所謂光線補償並不是為了取得這樣的一個效果。

#include "stdafx.h"
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <stdio.h>
#include <io.h>
#include <iostream>
#include <stdio.h>
#include "GObeautifyhelper.h" //算法庫
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <math.h>
#include <string>
#include <time.h>
using namespace cv;
using namespace std;
int main()
{
Mat src = imread( "F:\\my\\head_src_2.jpg" );
imshow( "原始圖片" ,src);
Mat dst=src.clone();
////////////////////////////////////////////////////////////////////////// Face detection in color images
//////////////////////////////////////////////////////////////////////////根據高光區域直方圖計算進行光線補償
const float thresholdco = 0.05;
const int thresholdnum = 100;
int histogram[256] = {0};
for(int i=0;i<dst.rows;i++)
{
for(int j=0;j<dst.cols;j++)
{
int b = dst.at<Vec3b>(i,j)[0];
int g = dst.at<Vec3b>(i,j)[1];
int r = dst.at<Vec3b>(i,j)[2];
//計算灰度值
int gray = (r*299+g*587+b*114)/1000;
histogram[gray]++;
}
}
int calnum =0;
int total = dst.rows * dst.cols ;
int num;
//下面的循環得到滿足系數thresholdco的臨界灰度級
for(int i =0;i<256;i++)
{
if((float )calnum/total < thresholdco) //得到前5%的高亮像素。
{
calnum+= histogram[255-i];//histogram保存的是某一灰度值的像素個數,calnum是邊界灰度之上的像素數
num = i;
}
else
break;
}
int averagegray = 0;
calnum =0;
//得到滿足條件的象素總的灰度值
for(int i = 255;i>=255-num;i--)
{
averagegray += histogram[i]*i; //總的像素的個數*灰度值
calnum += histogram[i]; //總的像素數
}
averagegray /=calnum;
//得到光線補償的系數
float co = 255.0/(float )averagegray;
for(int i=0;i<dst.rows;i++)
{
for(int j=0;j<dst.cols;j++)
{
dst.at<Vec3b>(i,j)[0]= CLAMP0255(co*dst.at<Vec3b>(i,j)[0]+0.5);
dst.at<Vec3b>(i,j)[1]=CLAMP0255(co*dst.at<Vec3b>(i,j)[1]+0.5);
dst.at<Vec3b>(i,j)[2]=CLAMP0255(co*dst.at<Vec3b>(i,j)[2]+0.5);
}
}
imshow( "Face detection in color images" ,dst);
cv::waitKey();
return 0;
}
在
中提到了這樣一段

它的前半段看上去很像這種直方圖的方法,但是后面一段非常NB地指出“模擬人視覺的視敏相應曲線”,並且給出了計算公式。所以這種方法最終也只是一種視網膜增強算法,當然它包含了將過大過小的區域進行壓縮的部分。
