光线补偿
HsuRL《Face detection in color images》论文下载地址 http://www.51xuewen.com/res/show.aspx?resid=2453
unsigned char RedTemp,GreenTemp,BlueTemp;
for (j=y1; j<y2; j++) {
for (i=x1; i<x2; i++) {
index = VL_RGB_PIXEL * (j*width + i);
//得到rgb值
RedTemp=input[index];
GreenTemp=input[index+1];
BlueTemp=input[index+2];
//计算灰度值
int gray = (RedTemp * 299 + GreenTemp * 587 + BlueTemp * 114)/1000;
histogram[gray]++;
}
}
int calnum =0;
int total = x2 * y2;
int num;
//下面的循环得到满足系数thresholdco的临界灰度级
for(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(i = 255;i>=255-num;i--)
{
averagegray += histogram[i]
calnum += histogram[i]
}
averagegray /=calnum;
//得到光线补偿的系数
float co = 255.0/(float)averagegray;
//下面的循环对图象进行光线补偿
for (j=y1; j<y2; j++) {
for (i=x1; i<x2; i++)
{
//得到数据偏移
index = VL_RGB_PIXEL * (j*width + i);
//得到分量
for(int n=0;n<3;n++)
{
int nTemp=input[index+n]*co;
output[index+n]=(nTemp>255)?255:nTemp;
} } }
核心代码:
const float thresholdco = 0.05;
//象素个数的临界常数
const int thresholdnum = 100;
//灰度级数组
int histogram[256];
for(i =0;i<256;i++)
histogram
另外一种补偿方法是东南大学夏思宇博士在
1.计算图像的R,G,B分量的各自平均值R',G',B',并令图像的平均灰度值avgGray=(G'+R'+B')/3.
2.令Kr=avgGray/R',Kg=avgGray/G',Kb=avgGray/B',调整每个像素点的RGB值R=R*Kr,G=G*Kg,B=B*Kb,并处理越界情况,将大于255的值置为255
代码:
int RedTotal=0,GreenTotal=0,BlueTotal=0,NumTotal,GrayTotal,RedTemp,GreenTemp,BlueTemp;
unsigned char RedAverage,GreenAverage,BlueAverage,GrayAverage;
float Kr,Kg,Kb;
for (j=y1; j<y2; j++) {
for (i=x1; i<x2; i++) {
index = VL_RGB_PIXEL * (j*width + i);
//得到rgb值
RedTotal+=input[index];
GreenTotal+=input[index+1];
BlueTotal+=input[index+2];
//计算灰度值
//GrayTotal = (input[index] * 299 + input[index+1] * 587 + input[index+2] * 114)/1000;
}
}
NumTotal=x2 * y2;
RedAverage=RedTotal/NumTotal;
GreenAverage=GreenTotal/NumTotal;
BlueAverage=BlueTotal/NumTotal;
GrayAverage=(RedAverage+GreenAverage+BlueAverage)/3;
Kr=(float)GrayAverage/(RedAverage);
Kg=(float)GrayAverage/(GreenAverage);
Kb=(float)GrayAverage/(BlueAverage);
for (j=y1; j<y2; j++) {
for (i=x1; i<x2; i++) {
index = VL_RGB_PIXEL * (j*width + i);
RedTemp=input[index] * Kr;
GreenTemp=input[index+1] * Kg;
BlueTemp=input[index+2] * Kb;
output[index]=(RedTemp>255) ? 255 : RedTemp;
output[index+1]=(GreenTemp>255) ? 255 : GreenTemp;
output[index+2]=(BlueTemp>255) ? 255 : BlueTemp;
}
}
它的补偿效果有待进一步验证