在http://www.cnblogs.com/Imageshop/archive/2011/11/13/2247614.html 一文中,作者给出了“自动对比度”的实现方法,非常nice
实际实现过程中,我发现文中有 “Dim HistRed(255)”这样的定义。一般来说,通道是0-255一个256阶的吧,如果不是语法的不同,应该是一个bug.
另附上opencv的实现代码,dirty code,欢迎有人优化!
Mat autocontrost(Mat matface)
{
//进行自动对比度校正
double HistRed[]={};
double HistGreen[]={};
double HistBlue[]={};
int bluemap[]={};
int redmap[]={};
int greenmap[]={}; double dlowcut = 0.1;
double dhighcut = 0.1;
for (int i=;i<matface.rows;i++)
{
for (int j=;j<matface.cols;j++)
{
int iblue =matface.at<Vec3b>(i,j)[];
int igreen=matface.at<Vec3b>(i,j)[];
int ired =matface.at<Vec3b>(i,j)[];
HistBlue[iblue]++;
HistGreen[igreen]++;
HistRed[ired]++;
}
}
int PixelAmount = matface.rows*matface.cols;
int isum = ;
// blue
int iminblue=;int imaxblue=;
for (int y = ;y<;y++)//这两个操作我基本能够了解了
{
isum= isum+HistBlue[y];
if (isum>=PixelAmount*dlowcut*0.01)
{
iminblue = y;
break;
}
}
isum = ;
for (int y=;y>=;y--)
{
isum=isum+HistBlue[y];
if (isum>=PixelAmount*dhighcut*0.01)
{
imaxblue=y;
break;
}
}
//red
isum=;
int iminred=;int imaxred=;
for (int y = ;y<;y++)//这两个操作我基本能够了解了
{
isum= isum+HistRed[y];
if (isum>=PixelAmount*dlowcut*0.01)
{
iminred = y;
break;
}
}
isum = ;
for (int y=;y>=;y--)
{
isum=isum+HistRed[y];
if (isum>=PixelAmount*dhighcut*0.01)
{
imaxred=y;
break;
}
}
//green
isum=;
int imingreen=;int imaxgreen=;
for (int y = ;y<;y++)//这两个操作我基本能够了解了
{
isum= isum+HistGreen[y];
if (isum>=PixelAmount*dlowcut*0.01)
{
imingreen = y;
break;
}
}
isum = ;
for (int y=;y>=;y--)
{
isum=isum+HistGreen[y];
if (isum>=PixelAmount*dhighcut*0.01)
{
imaxgreen=y;
break;
}
}
/////////自动色阶
//自动对比度
int imin = ;int imax =;
if (imin>iminblue)
imin = iminblue;
if (imin>iminred)
imin = iminred;
if (imin>imingreen)
imin = imingreen;
iminblue = imin ;
imingreen=imin;
iminred = imin ;
if (imax<imaxblue)
imax = imaxblue;
if (imax<imaxgreen)
imax =imaxgreen;
if (imax<imaxred)
imax =imaxred;
imaxred = imax;
imaxgreen = imax;
imaxblue=imax;
/////////////////
//blue
for (int y=;y<;y++)
{
if (y<=iminblue)
{
bluemap[y]=;
}
else
{
if (y>imaxblue)
{
bluemap[y]=;
}
else
{
// BlueMap(Y) = (Y - MinBlue) / (MaxBlue - MinBlue) * 255 '线性隐射
float ftmp = (float)(y-iminblue)/(imaxblue-iminblue);
bluemap[y]=(int)(ftmp*);
}
} }
//red
for (int y=;y<;y++)
{
if (y<=iminred)
{
redmap[y]=;
}
else
{
if (y>imaxred)
{
redmap[y]=;
}
else
{
// BlueMap(Y) = (Y - MinBlue) / (MaxBlue - MinBlue) * 255 '线性隐射
float ftmp = (float)(y-iminred)/(imaxred-iminred);
redmap[y]=(int)(ftmp*);
}
} }
//green
for (int y=;y<;y++)
{
if (y<=imingreen)
{
greenmap[y]=;
}
else
{
if (y>imaxgreen)
{
greenmap[y]=;
}
else
{
// BlueMap(Y) = (Y - MinBlue) / (MaxBlue - MinBlue) * 255 '线性隐射
float ftmp = (float)(y-imingreen)/(imaxgreen-imingreen);
greenmap[y]=(int)(ftmp*);
}
} }
//查表
for (int i=;i<matface.rows;i++)
{
for (int j=;j<matface.cols;j++)
{
matface.at<Vec3b>(i,j)[]=bluemap[matface.at<Vec3b>(i,j)[]];
matface.at<Vec3b>(i,j)[]=greenmap[matface.at<Vec3b>(i,j)[]];
matface.at<Vec3b>(i,j)[]=redmap[matface.at<Vec3b>(i,j)[]];
}
}
return matface;
}