Skip to content

Instantly share code, notes, and snippets.

@ucalyptus
Created February 4, 2020 07:09
Show Gist options
  • Save ucalyptus/b13280d8dc3c0820101c70072f456db6 to your computer and use it in GitHub Desktop.
Save ucalyptus/b13280d8dc3c0820101c70072f456db6 to your computer and use it in GitHub Desktop.
Original Implementation of Mean Spectral Divergence
def cal_mean_spectral_divergence(band_subset):
    """
    Spectral Divergence is defined as the symmetrical KL divergence (D_KLS) of two bands probability distribution.
    We use Mean SD (MSD) to quantify the redundancy among a band set.
    B_i and B_j should be a gray histagram.
    SD = D_KL(B_i||B_j) + D_KL(B_j||B_i)
    MSD = 2/n*(n-1) * sum(ID_ij)
    Ref:
    [1] GONG MAOGUO, ZHANG MINGYANG, YUAN YUAN. Unsupervised Band Selection Based on Evolutionary Multiobjective
    Optimization for Hyperspectral Images [J]. IEEE Transactions on Geoscience and Remote Sensing, 2016, 54(1): 544-57.
    :param band_subset: with shape (n_row, n_clm, n_band)
    :return:
    """
    n_row, n_column, n_band = band_subset.shape
    N = n_row * n_column
    hist = []
    for i in range(n_band):
        hist_, edge_ = np.histogram(band_subset[:, :, i], 256)
        hist.append(hist_ / N)
    hist = np.asarray(hist)
    hist[np.nonzero(hist <= 0)] = 1e-20
    # entropy_lst = entropy(hist.transpose())
    info_div = 0
    # band_subset[np.nonzero(band_subset <= 0)] = 1e-20
    for b_i in range(n_band):
        for b_j in range(n_band):
            band_i = hist[b_i].reshape(-1)/np.sum(hist[b_i])
            band_j = hist[b_j].reshape(-1)/np.sum(hist[b_j])
            entr_ij = entropy(band_i, band_j)
            entr_ji = entropy(band_j, band_i)
            entr_sum = entr_ij + entr_ji
            info_div += entr_sum
    msd = info_div * 2 / (n_band * (n_band - 1))
    return msd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment