Python实现Global Reed-Xiaoli(GRX)高光谱异常检测算法

import scipy.io as scio
import numpy as np
import matplotlib.pyplot as plt
import time


def GRX(mat):
    rows, cols, bands = mat.shape  # XX为图像的长度 YY为图像的高度 band为图像的波段数
    X = np.reshape(mat, (rows * cols, bands))
    cov_X = np.cov(X.T)  # 得到矩阵r的协方差矩阵K,K为band乘band的矩阵
    X_mean = np.expand_dims(np.mean(X.T, axis=1), 0).T  # 求r矩阵即每一个波段所有像素的均值,u为均值列向量
    Y = X.T - np.repeat(X_mean, rows * cols, axis=1)
    result = np.zeros((1, rows * cols))
    cov_inv = np.linalg.pinv(cov_X)
    for i in range(rows * cols):
        result[0][i] = np.dot(np.dot(Y[:, i].T, cov_inv), Y[:, i])
    result = np.reshape(result, (rows, cols))
    return result


if __name__ == '__main__':
    t1 = time.time()
    dataFile = 'Airport.mat'
    data = scio.loadmat(dataFile)
    result = GRX(data['data'])
    t2 = time.time()
    print('time:', round(t2 - t1, 3), 's')
    plt.imshow(result)
    plt.show()

你可能感兴趣的:(高光谱,计算机视觉,计算机视觉,python,目标检测)