欢迎有兴趣的朋友交流指点。最后,废话不多说直接上代码
import matplotlib.pyplot as plt
from scipy.io import loadmat
from scipy.stats import chi2
import spectral as spy
def anomaly_detect_demo(src, gt):
spy.imshow(data=src, bands=[29, 19, 9], title="img")
rx_vals = spy.rx(src) # 马氏距离平方
spy.imshow(rx_vals, stretch=(0.99, 0.9999), title="untreated") # 进行下拉伸以便更好显示
n = src.shape[2]
P = chi2.ppf(0.999, n) # 阈值设为0.001
spy.imshow(1 * (rx_vals > P), title="globe")
plt.figure()
plt.hist(rx_vals.ravel(), bins=200, log=True) # 由直方图可方便看出少量离散点
local_rx_vals = spy.rx(src, window=(5, 21)) # 局部窗口背景
spy.imshow(local_rx_vals, stretch=(0.99, 0.9999), title="local")
C = spy.cov_avg(src, gt) # 全局协方差估计背景
cpv_rx_vals = spy.rx(src, window=(5, 21), cov=C)
spy.imshow(1 * (cpv_rx_vals > P), title="cov")
def match_demo(src):
t = src[377, 45] # 白色建筑物处
mf_scores = spy.matched_filter(src, t)
mask = 1 * (mf_scores > 0.2) # 匹配值大于0.2时的目标
view = spy.imshow(data=src, bands=[29, 19, 9], title="result", classes=mask, colors=[255, 0, 0])
view.set_display_mode("overlay")
view.class_alpha = 1 # 红色显示为检测到的位置 c/d切换显示模式
input_image = loadmat('D:/Hyper/Salinas_corrected.mat')['salinas_corrected']
gt = loadmat("D:/Hyper/Salinas_gt.mat")['salinas_gt']
anomaly_detect_demo(input_image, gt)
match_demo(input_image)
plt.pause(60)