/**************************************************************/
/*
函数: ImageAdjust
功能: 彩色图像的亮度分量的拉伸
参数:
srcimage ---指向彩色车牌图像的指针,返回拉伸后的图像
dwWidth ---车牌宽度
dwHeight ---车牌高度
gamma ---非线性拉伸系数
返回:
成功返回0
*/
/***************************************************************/
int ImageAdjust(float *&srcimage, int dwWidth, int dwHeight, float gamma )
{
int i,j,k=0,l,m,n=0;
int N=dwWidth*dwHeight/100+1;
int ColorLineBytes = WIDTHBYTES(dwWidth*COLORBITS24);
float *low,*high;// X方向:low and high are the intensities of src
float temp;
low=(float *)calloc(N,sizeof(float));
high=(float *)calloc(N,sizeof(float));
if(srcimage==NULL)
return -1;
for( i = 0; i < dwHeight; i++)
{
for (j = 0; j <dwWidth; j++)
{
temp=*(srcimage + ColorLineBytes*i + j*3+2);
if (temp<(*(low+k-1))||k<N)
{
for(l=k-1;l>=0;l--)
{
if (*(low+l)<temp)
break;
}
if (k<N)
{
for(m=k;m>l+1;m--)
*(low+m)=*(low+m-1);
*(low+l+1) = temp;
k++;
}
else
{
for(m=N-1;m>l+1;m--)
*(low+m)=*(low+m-1);
*(low+l+1) = temp;
}
}
if (temp>high[n-1]||n<N)
{
for(l=n-1;l>=0;l--)
{
if (*(high+l)>temp)
break;
}
if (n<N)
{
for(m=n;m>l+1;m--)
*(high+m)=*(high+m-1);
*(high+l+1) = temp;
n++;
}
else
{
for(m=N-1;m>l+1;m--)
*(high+m)=*(high+m-1);
*(high+l+1) = temp;
}
}
}
}
float low2 = *(low+N-1)*255;
float high2 =*(high+N-1)*255;
float bottom2 = 0;// Y方向:mapped to bottom and top of dst
float top2 = 255;
float err_in = high2 - low2;
float err_out = top2 - bottom2;
float val;
// intensity transform
for( i = 0; i < dwHeight; i++)
{
for (j = 0; j <dwWidth; j++)
{
val = *(srcimage + ColorLineBytes*i + j*3+2)*255;
val = (float)pow((val - low2)/err_in, gamma) * err_out + bottom2;
if(val>high2)
val=255;
if(val<low2)
val=0; // Make sure src is in the range [low,high]
*(srcimage + ColorLineBytes*i + j*3+2) = val/255;
}
}
if (low)
{
free(low);
low=NULL;
}
if (high)
{
free(high);
high=NULL;
}
return 0;
}
评论0