Python实现Local Reed-Xiaoli

Python实现Local Reed-Xiaoli,第1张

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()

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/577678.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-11
下一篇 2022-04-11

发表评论

登录后才能评论

评论列表(0条)

保存