最近扒拉了一些光线补偿算法的实现,可能是能力比较有限,看到的大多是是基于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地指出“模拟人视觉的视敏相应曲线”,并且给出了计算公式。所以这种方法最终也只是一种视网膜增强算法,当然它包含了将过大过小的区域进行压缩的部分。