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

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


def LRX(mat, win_in, win_out):
    rows, cols, bands = mat.shape  # XX为图像的长度 YY为图像的高度 band为图像的波段数
    result = np.zeros((rows, cols))
    w1 = int(np.fix(win_out / 2))
    w2 = int(np.fix(win_in / 2))
    M = win_out ** 2
    # padding avoid edges
    DataTest = np.zeros((3 * rows, 3 * cols, bands))
    DataTest[rows: 2 * rows, cols: 2 * cols, :] = mat
    DataTest[rows: 2 * rows, 0: cols, :] = mat[:, cols:: -1, :]
    DataTest[rows: 2 * rows, 2 * cols: 3 * cols, :] = mat[:, cols:: -1, :]
    DataTest[0: rows, :, :] = DataTest[2 * rows:rows:-1, :, :]
    DataTest[2 * rows: 3 * rows, :, :] = DataTest[2 * rows:rows:-1, :, :]
    for i in range(cols, 2 * cols):
        for j in range(rows, 2 * rows):
            block = DataTest[j - w1: j + w1 + 1, i - w1: i + w1 + 1, :].copy()
            y = DataTest[j, i, :].T  # 1 x dim
            block[w1 - w2: w1 + w2 + 1, w1 - w2: w1 + w2 + 1, :] = np.NAN
            block = np.reshape(block, (M, bands))
            block = np.delete(block, np.where(np.isnan(block[:, 0]))[0], axis=0)
            H = block.T
            Sigma = np.dot(H, H.T)
            Sigma_inv = pinv(Sigma)
            result[j - rows][i - cols] = np.dot(np.dot(y, Sigma_inv), y.T)
    return result


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

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