相互情報

5044 ワード

相互情報


医学画像の分割には,相互情報を用いて2枚の画像の類似度を計算する必要がある.前に関数を書きましたが、ここでバックアップします.
float MI_hhl(float ***Img1, float ***Img2, int Image_X_Size, int Image_Y_Size, int Image_Z_Size)
{
    // 
    int Hist_Img1[256], Hist_Img2[256];
    for (int t = 0; t < 256; t++)
    {
        Hist_Img1[t] = 0;
        Hist_Img2[t] = 0;
    }
    for (int k = 0; k < Image_Z_Size; k++)
    {
        for (int i = 0; i < Image_X_Size; i++)
        {
            for (int j = 0; jint)Img1[k][i][j]]++;
                Hist_Img2[(int)Img2[k][i][j]]++;
            }
        }
    }
    // 
    int Hist[256][256];
    for (int i = 0; i < 256; i++)
    {
        for (int j = 0; j < 256; j++)
        {
            Hist[i][j] = 0;
        }
    }
    for (int k = 0; k < Image_Z_Size; k++)
    {
        for (int i = 0; i < Image_X_Size; i++)
        {
            for (int j = 0; jint)Img1[k][i][j]][(int)Img2[k][i][j]]++;
            }
        }
    }

    // 
    float P_Img1[256], P_Img2[256];
    float all_number;
    all_number = Image_X_Size * Image_Y_Size * Image_Z_Size;
    for (int t = 0; t < 256; t++)
    {
        P_Img1[t] = (float)Hist_Img1[t] / all_number;
        P_Img2[t] = (float)Hist_Img2[t] / all_number;
    }

    // 
    float P_Hist[256][256];
    for (int i = 0; i < 256; i++)
    {
        for (int j = 0; j < 256; j++)
        {
            P_Hist[i][j] = (float)Hist[i][j] / all_number;
        }
    }

    // 
    float Mut_INF = 0;
    for (int i = 0; i < 256; i++)
    {
        for (int j = 0; j < 256; j++)
        {
            if (P_Hist[i][j] == 0 || P_Img1[i] == 0 || P_Img2[j] == 0)
            {
                continue;
            }
            Mut_INF += P_Hist[i][j] * log10(P_Hist[i][j] / (P_Img1[i] * P_Img2[j]));
        }
    }

    return Mut_INF;
}