/*-------------------------------------------------------------------------*/
// 函数名称: histeq()
// 传入参数:
// BYTE*dstData 要匹配的灰度图像内存空间
// double *srcArray 模版的直方图累积,并进行归一化,大小为256
// int m_Width 匹配内存空间的宽度
// int m_Height 匹配内存空间的高度
// int m_pitch 匹配内存空间的每行所在内存大小
/*-------------------------------------------------------------------------*/
void histeq(BYTE*dstData,double *srcArray,int m_Width,int m_Height,int m_pitch)
{
double dstHist[];
memset(dstHist,, * sizeof(double)); int i,j; double dstArray[];
memset(dstArray,, * sizeof(double)); //统计直方图
for (i = ;i < m_Height;i++)
{
for (j = ;j < m_Width;j++)
{
dstHist[(int)dstData[i * m_pitch + j]]++;
}
} //计算直方图累积
double m_Bytes = m_Width * m_Height;
dstArray[] = dstHist[];
for (i = ;i < ;i++)
{
dstArray[i] = dstArray[i - ] + dstHist[i];
} //直方图累积归一化
for (i = ;i < ;i++)
{
dstArray[i] /= m_Bytes;
} //直方图匹配
double m_diffA,m_diffB;
int k = ;
BYTE mapPixel[];
memset(mapPixel,, * sizeof(BYTE));
for (i = ;i < ;i++)
{
m_diffB = ;
for (j = k; j < ;j++)
{
m_diffA = abs(dstArray[i] - srcArray[j]);
if (m_diffA - m_diffB < 1.0E-5)
{
m_diffB = m_diffA;
k = j;
}
else
{
k = j - ;
break;
}
}
if (k == )
{
for (int l = i;l < ;l++)
{
mapPixel[l] = (BYTE) k;
}
break;
}
mapPixel[i] = (BYTE) k;
} //目标图像查找索引表
for (i = ;i < m_Height;i++)
{
for (j = ;j < m_Width;j++)
{
dstData[i * m_pitch + j] = mapPixel[dstData[i * m_pitch + j]];
}
}
}
源地址:http://blog.csdn.net/hcx25909/article/details/7090921