首页 > 代码库 > C++ 直方图匹配算法代码

C++ 直方图匹配算法代码

/*-------------------------------------------------------------------------*///   函数名称: 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[256];    memset(dstHist,0,256 * sizeof(double));    int i,j;    double dstArray[256];    memset(dstArray,0,256 * sizeof(double));    //统计直方图    for (i = 0;i < m_Height;i++)    {        for (j = 0;j < m_Width;j++)        {            dstHist[(int)dstData[i * m_pitch + j]]++;        }    }    //计算直方图累积    double m_Bytes = m_Width * m_Height;    dstArray[0] = dstHist[0];    for (i = 1;i < 256;i++)    {        dstArray[i] = dstArray[i - 1] + dstHist[i];    }    //直方图累积归一化    for (i = 0;i < 256;i++)    {        dstArray[i] /= m_Bytes;    }    //直方图匹配    double m_diffA,m_diffB;    int k = 0;    BYTE mapPixel[256];    memset(mapPixel,0,256 * sizeof(BYTE));    for (i = 0;i < 256;i++)    {        m_diffB = 1;        for (j = k; j < 256;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 - 1;                break;            }        }        if (k == 255)        {            for (int l = i;l < 256;l++)            {                mapPixel[l] = (BYTE) k;            }            break;        }        mapPixel[i] = (BYTE) k;    }    //目标图像查找索引表    for (i = 0;i < m_Height;i++)    {        for (j = 0;j < m_Width;j++)        {            dstData[i * m_pitch + j] = mapPixel[dstData[i * m_pitch + j]];        }    }}

源地址:http://blog.csdn.net/hcx25909/article/details/7090921

C++ 直方图匹配算法代码