用 Python 计算光谱距离 (Jeffries-Matusita)

问题描述 投票:0回答:0

我正在尝试使用 Python 计算 Indian Pines 数据中波段的 JM 距离。下面是我的代码。任何意见?结果似乎正确?

代码下载 Indian Pines 并将其存储在一个 numpy 数组中。计算 Bhattacharya 然后使用它 对于 Jeffries Matusita

# Import necessary and appropriate packages

import numpy as np
import os
import pandas as pd
import requests
from scipy.io import loadmat # MATlab data files
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator
from mpl_toolkits.mplot3d import Axes3D

from sklearn.metrics import normalized_mutual_info_score, mutual_info_score
from sklearn.feature_selection import mutual_info_regression

from IPython.core.display import json

def getIndianPinesData():

    if os.path.exists("Indian_pines_corrected.mat") and os.path.exists("Indian_pines_gt.mat"):
        print('files exists')
    else:
        URL = "http://www.ehu.eus/ccwintco/uploads/6/67/Indian_pines_corrected.mat"
        response = requests.get(URL)
        open("Indian_pines_corrected.mat", "wb").write(response.content)

        URL = "http://www.ehu.eus/ccwintco/uploads/c/c4/Indian_pines_gt.mat"
        response = requests.get(URL)
        open("Indian_pines_gt.mat", "wb").write(response.content)
    
    X = loadmat('Indian_pines_corrected.mat')['indian_pines_corrected']
    y = loadmat('Indian_pines_gt.mat')['indian_pines_gt']

    print(type(X),type(y))
    print("Dimensions of Indian Pines array are:",X.shape,", with",X.shape[2],'bands')
    
    
    return X,y

def computeBhattacharyya(bands):
    numberOfBands = bands.shape[2]
    BHdistances = np.zeros((numberOfBands, numberOfBands))
    for i in range(numberOfBands):
        for j in range(numberOfBands):
            # Get the two bands
            band1 = bands[:,:,i]
            band2 = bands[:,:,j]
        
            # Calculate the means and standard deviations of the two bands
            mean1 = np.mean(band1)
            mean2 = np.mean(band2)
            std1 = np.std(band1)
            std2 = np.std(band2)
        
            # Calculate the Bhattacharyya distance
            b_dist = np.sqrt(1 - np.exp(-0.25 * (mean1 - mean2)**2 / (std1**2 + std2**2)))
        
            # Store the distance in the distances matrix
            BHdistances[i,j] = b_dist
   
    print("Bhattacharyya distance matrix\n")
    print("Bhattacharyya distance matrix:\n\n",BHdistances)
    
    im = plt.imshow(BHdistances, cmap='hot')
    plt.colorbar(im, orientation='horizontal')
    plt.show()
    return BHdistances

def computeJM(BH):
    
    numberOfBands = BH.shape[1]
    JM = np.zeros((numberOfBands, numberOfBands))
    '''
    The Jeffries-Matusita (JM) measure is a statistical measure of the similarity between two 
    multivariate distributions, and it can be calculated using the Bhattacharyya distance matrix. 
    The formula for calculating the JM measure from the Bhattacharyya distance matrix is:

    JM = sqrt(2 * (1 - exp(-BD)))

    where BD is the Bhattacharyya distance matrix.
    '''
    for i in range(numberOfBands):
        for j in range(numberOfBands):
            # Get the two bands
            if i != j:
                JM[i][j] = np.sqrt(2 * (1 - np.exp(-BH[i][j])))
    print("plot Jeffries Matusita measurement\n")
    print(JM)
    
    im = plt.imshow(JM, cmap='hot')
    plt.colorbar(im, orientation='horizontal')
    plt.show()

def main():
    #loadHSIdatafile()
    #checkFile()
    A, b = getIndianPinesData()
    BHdistances = computeBhattacharyya(A)
    print(BHdistances.shape)
    computeJM(BHdistances)


if __name__ == '__main__':
    main()

似乎还可以...只是不熟悉问题集...

python distance
© www.soinside.com 2019 - 2024. All rights reserved.