Skip to content

Commit 9d72693

Browse files
author
Yutai
committed
changed around demo names, added dim mismatch bug fix to sig detectors
1 parent 3286bd0 commit 9d72693

34 files changed

+243
-166
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
# hsi_toolkit_py
22
GatorSense Hyperspectral Image Analysis Toolkit - Python Implementation
3+
4+
Hyperspectral image format: height x width x bands

signature_detectors/abd_detector.py

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ def abd_detector(hsi_img, tgt_sig, ems, mask = None):
2121
6/2/2018 - Edited by Alina Zare
2222
12/2018 - Python Implementation by Yutai Zhou
2323
"""
24+
if tgt_sig.ndim == 1:
25+
tgt_sig = tgt_sig[:, np.newaxis]
26+
2427
abd_out, kwargsout = img_det(abd_helper,hsi_img,tgt_sig,mask,ems = ems);
2528

2629
return abd_out

signature_detectors/ace_detector.py

+37-34
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,47 @@
44
import numpy as np
55

66
def ace_detector(hsi_img, tgt_sig, mask = None, mu = None, sig_inv = None):
7-
"""
8-
Squared Adaptive Cosine/Coherence Estimator
9-
10-
Inputs:
11-
hsi_image - n_row x n_col x n_band hyperspectral image
12-
tgt_sig - target signature (n_band x 1 - column vector)
13-
mask - binary image limiting detector operation to pixels where mask is true
14-
if not present or empty, no mask restrictions are used
15-
mu - background mean (n_band x 1 column vector)
16-
sig_inv - background inverse covariance (n_band x n_band matrix)
17-
18-
Outputs:
19-
ace_out - detector image
20-
mu - mean of input data
21-
sig_inv - inverse covariance of input data
22-
23-
8/8/2012 - Taylor C. Glenn
24-
6/2/2018 - Edited by Alina Zare
25-
10/2018 - Python Implementation by Yutai Zhou
26-
"""
27-
ace_out, kwargsout = img_det(ace_det_helper, hsi_img, tgt_sig, mask, mu = mu, sig_inv = sig_inv)
28-
return ace_out, kwargsout['mu'], kwargsout['sig_inv']
7+
"""
8+
Squared Adaptive Cosine/Coherence Estimator
9+
10+
Inputs:
11+
hsi_image - n_row x n_col x n_band hyperspectral image
12+
tgt_sig - target signature (n_band x 1 - column vector)
13+
mask - binary image limiting detector operation to pixels where mask is true
14+
if not present or empty, no mask restrictions are used
15+
mu - background mean (n_band x 1 column vector)
16+
sig_inv - background inverse covariance (n_band x n_band matrix)
17+
18+
Outputs:
19+
ace_out - detector image
20+
mu - mean of input data
21+
sig_inv - inverse covariance of input data
22+
23+
8/8/2012 - Taylor C. Glenn
24+
6/2/2018 - Edited by Alina Zare
25+
10/2018 - Python Implementation by Yutai Zhou
26+
"""
27+
if tgt_sig.ndim == 1:
28+
tgt_sig = tgt_sig[:, np.newaxis]
29+
30+
ace_out, kwargsout = img_det(ace_det_helper, hsi_img, tgt_sig, mask, mu = mu, sig_inv = sig_inv)
31+
return ace_out, kwargsout['mu'], kwargsout['sig_inv']
2932

3033
def ace_det_helper(hsi_data, tgt_sig, kwargs):
31-
mu = np.mean(hsi_data, axis = 1) if kwargs['mu'] is None else kwargs['mu']
32-
sig_inv = np.linalg.pinv(np.cov(hsi_data.T, rowvar = False)) if kwargs['sig_inv'] is None else kwargs['sig_inv']
34+
mu = np.mean(hsi_data, axis = 1) if kwargs['mu'] is None else kwargs['mu']
35+
sig_inv = np.linalg.pinv(np.cov(hsi_data.T, rowvar = False)) if kwargs['sig_inv'] is None else kwargs['sig_inv']
3336

34-
mu = np.reshape(mu, (len(mu), 1), order='F')
35-
s = tgt_sig - mu
36-
z = hsi_data - mu
37+
mu = np.reshape(mu, (len(mu), 1), order='F')
38+
s = tgt_sig - mu
39+
z = hsi_data - mu
3740

38-
st_sig_inv = s.T @ sig_inv
39-
st_sig_inv_s = s.T @ sig_inv @ s
41+
st_sig_inv = s.T @ sig_inv
42+
st_sig_inv_s = s.T @ sig_inv @ s
4043

41-
A = np.sum(st_sig_inv @ z, 0)
42-
B = st_sig_inv_s
43-
C = np.sum(z * (sig_inv @ z), 0)
44+
A = np.sum(st_sig_inv @ z, 0)
45+
B = st_sig_inv_s
46+
C = np.sum(z * (sig_inv @ z), 0)
4447

45-
ace_data = A * A / (B * C)
48+
ace_data = A * A / (B * C)
4649

47-
return ace_data.T.squeeze(), {'mu':mu, 'sig_inv': sig_inv}
50+
return ace_data.T.squeeze(), {'mu':mu, 'sig_inv': sig_inv}

signature_detectors/ace_local_detector.py

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ def ace_local_detector(hsi_img, tgt_sig, mask = None, guard_win = 2, bg_win = 4,
2727
mask = np.ones([n_row, n_col]) if mask is None else mask
2828
reg = beta * np.eye(n_band)
2929

30+
if tgt_sig.ndim == 1:
31+
tgt_sig = tgt_sig[:, np.newaxis]
32+
33+
3034
out, kwargsout = rx_det(ace_local_helper, hsi_img, tgt_sig, mask = mask, guard_win = guard_win, bg_win = bg_win, reg = reg)
3135
return out, kwargsout
3236

signature_detectors/ace_rt_detector.py

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ def ace_rt_detector(hsi_img, tgt_sig, mask = None, mu = None, sig_inv = None):
2424
6/2/2018 - Edited by Alina Zare
2525
11/2018 - Python Implementation by Yutai Zhou
2626
"""
27+
if tgt_sig.ndim == 1:
28+
tgt_sig = tgt_sig[:, np.newaxis]
29+
2730
ace_rt_out, kwargsout = img_det(ace_rt_helper, hsi_img, tgt_sig, mask, mu = mu, sig_inv = sig_inv)
2831
return ace_rt_out, kwargsout['mu'], kwargsout['sig_inv']
2932

signature_detectors/ace_rt_max_detector.py

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ def ace_rt_max_detector(hsi_img, tgt_sig, mask = None, mu = None, sig_inv = None
2525
6/2/2018 - Edited by Alina Zare
2626
11/2018 - Python Implementation by Yutai Zhou
2727
"""
28+
if tgt_sig.ndim == 1:
29+
tgt_sig = tgt_sig[:, np.newaxis]
30+
2831
ace_rt_max_out, kwargsout = img_det(ace_rt_max_helper, hsi_img, tgt_sig, mask, mu = mu, sig_inv = sig_inv)
2932
return ace_rt_max_out, kwargsout['mu'], kwargsout['sig_inv']
3033

signature_detectors/ace_ss_detector.py

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ def ace_ss_detector(hsi_img, tgt_sig, mask = None, mu = None, sig_inv = None):
2020
6/2/2018 - Edited by Alina Zare
2121
11/2018 - Python Implementation by Yutai Zhou
2222
"""
23+
if tgt_sig.ndim == 1:
24+
tgt_sig = tgt_sig[:, np.newaxis]
25+
2326
ace_ss_out, kwargsout = img_det(ace_ss_helper, hsi_img, tgt_sig, mask, mu = mu, sig_inv = sig_inv)
2427
return ace_ss_out
2528

signature_detectors/amsd_detector.py

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ def amsd_detector(hsi_img, tgt_sig, mask = None, n_dim_tgt = 1, n_dim_bg = 5):
2828
6/02/2018 - Edited by Alina Zare
2929
12/2018 - Python Implementation by Yutai Zhou
3030
"""
31+
if tgt_sig.ndim == 1:
32+
tgt_sig = tgt_sig[:, np.newaxis]
33+
3134
amsd_out, kwargsout = img_det(amsd_helper, hsi_img, tgt_sig, mask, n_dim_tgt = n_dim_tgt, n_dim_bg = n_dim_bg)
3235
return amsd_out
3336

signature_detectors/ccmf_detector.py

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ def ccmf_detector(hsi_img, tgt_sig, mask = None, n_comp = 5, gmm = None):
2424
6/2/2018 - Edited by Alina Zare
2525
12/2018 - Python Implementation by Yutai Zhou
2626
"""
27+
if tgt_sig.ndim == 1:
28+
tgt_sig = tgt_sig[:, np.newaxis]
29+
2730
ccmf_out, kwargsout = img_det(ccmf_helper, hsi_img, tgt_sig, mask, n_comp = n_comp, gmm = gmm)
2831

2932
return ccmf_out, kwargsout['gmm']

signature_detectors/cem_detector.py

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ def cem_detector(hsi_img, tgt_sig, mask = None):
2323
6/2/2018 - Edited by Alina Zare
2424
12/2018 - Python Implementation by Yutai Zhou
2525
"""
26+
if tgt_sig.ndim == 1:
27+
tgt_sig = tgt_sig[:, np.newaxis]
2628

2729
cem_out, kwargsout = img_det(cem_helper, hsi_img, tgt_sig, mask)
2830

signature_detectors/ctmf_detector.py

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ def ctmf_detector(hsi_img, tgt_sig, n_cluster = 2):
1919
6/02/2018 - Edited by Alina Zare
2020
12/2018 - Python Implementation by Yutai Zhou
2121
"""
22+
if tgt_sig.ndim == 1:
23+
tgt_sig = tgt_sig[:, np.newaxis]
24+
2225
n_row, n_col, n_band = hsi_img.shape
2326
n_pixel = n_row * n_col
2427

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import sys
2+
from scipy.io import loadmat
3+
import matplotlib.pyplot as plt
4+
sys.path.append('../')
5+
sys.path.append('../util/')
6+
from signature_detectors import *
7+
from get_RGB import get_RGB
8+
from img_seg import img_seg
9+
from sklearn.cluster import KMeans
10+
"""
11+
Demo script that runs all signature detectors in hsi_toolkit_py
12+
13+
Inputs:
14+
hsi_sub - n_row x n_col x n_band hyperspectral image
15+
tgt_spectra - n_band x 1 target signature vector
16+
wavelengths - n_band x 1 vector listing wavelength values for hsi_sub in nm
17+
gt_img_sub - n_row x n_col ground truths
18+
mask - binary image limiting detector operation to pixels where mask is true
19+
if not present or empty, no mask restrictions are used
20+
Outputs:
21+
det_out - dictionary of RGB image, ground truth image, and detector outputs
22+
23+
6/2/2018 - Alina Zare
24+
10/12/2018 - Python Implementation by Yutai Zhou
25+
"""
26+
# Load data
27+
an_hsi_img_for_tgt_det_demo = loadmat('an_hsi_img_for_tgt_det_demo.mat')
28+
hsi_sub = an_hsi_img_for_tgt_det_demo['hsi_sub']
29+
tgt_spectra = an_hsi_img_for_tgt_det_demo['tgt_spectra']
30+
tgt_spectra = tgt_spectra.squeeze()
31+
wavelengths = an_hsi_img_for_tgt_det_demo['wavelengths']
32+
gt_img_sub = an_hsi_img_for_tgt_det_demo['gtImg_sub']
33+
34+
det_out = {}
35+
det_out['RGB'] = get_RGB(hsi_sub, wavelengths)
36+
det_out['Ground Truth'] = gt_img_sub
37+
38+
# init detector args
39+
guard_win = 1; bg_win = 3; beta = 0.001; n_dim_ss = 10;
40+
ems = hsi_sub[:3,1,:].T # need to provide background endmembers (can get them using SPICE unmixing)
41+
42+
# call detectors
43+
abd_out = abd_detector(hsi_sub, tgt_spectra, ems)
44+
det_out['ABD'] = abd_out
45+
ace_out, _, _ = ace_detector(hsi_sub, tgt_spectra)
46+
det_out['ACE Squared'] = ace_out
47+
ace_local_out, _ = ace_local_detector(hsi_sub, tgt_spectra, guard_win = guard_win, bg_win = bg_win, beta = beta)
48+
det_out['ACE Local Squared'] = ace_local_out
49+
ace_ss_out = ace_ss_detector(hsi_sub, tgt_spectra)
50+
det_out['ACE SS'] = ace_ss_out
51+
ace_rt_out, _, _ = ace_rt_detector(hsi_sub, tgt_spectra)
52+
det_out['ACE RT'] = ace_rt_out
53+
ace_rt_max_out, _, _ = ace_rt_max_detector(hsi_sub, tgt_spectra)
54+
det_out['ACE RT Max'] = ace_rt_max_out
55+
amsd_out= amsd_detector(hsi_sub, tgt_spectra, n_dim_tgt = 1, n_dim_bg = 3)
56+
det_out['AMSD'] = amsd_out
57+
ccmf_out, _ = ccmf_detector(hsi_sub, tgt_spectra, n_comp = 2)
58+
det_out['CCMF'] = ccmf_out
59+
cem_out, w = cem_detector(hsi_sub, tgt_spectra)
60+
det_out['CEM'] = cem_out
61+
ctmf_out, _ = ctmf_detector(hsi_sub, tgt_spectra, n_cluster = 2)
62+
det_out['CTMF'] = ctmf_out
63+
ftmf_out = ftmf_detector(hsi_sub, tgt_spectra, gamma = 1)
64+
det_out['FTMF'] = ftmf_out
65+
ha_out = ha_detector(hsi_sub, tgt_spectra, ems, n_comp = 2)
66+
det_out['HA'] = ha_out
67+
hsd_out, _ = hsd_detector(hsi_sub, tgt_spectra, ems)
68+
det_out['HSD'] = hsd_out
69+
hsd_local_out = hsd_local_detector(hsi_sub, tgt_spectra, ems, guard_win = guard_win, bg_win = bg_win, beta = beta)
70+
det_out['HSD Local'] = hsd_local_out
71+
hua_out = hua_detector(hsi_sub, tgt_spectra, ems, n_comp = 2)
72+
det_out['HUA'] = hua_out
73+
mtmf_out,_ = mtmf_statistic(hsi_sub, tgt_spectra)
74+
det_out['MTMF'] = mtmf_out
75+
smf_out, _, _ = smf_detector(hsi_sub, tgt_spectra)
76+
det_out['SMF'] = smf_out
77+
smf_local_out = smf_local_detector(hsi_sub, tgt_spectra, guard_win = guard_win, bg_win = bg_win)
78+
det_out['SMF Local'] = smf_local_out
79+
smf_max_out = smf_max_detector(hsi_sub, tgt_spectra)
80+
det_out['SMF Max'] = smf_max_out
81+
fam_statistic_out = fam_statistic(hsi_sub, tgt_spectra)
82+
det_out['FAM Statistic'] = fam_statistic_out
83+
osp_out = osp_detector(hsi_sub, tgt_spectra, n_dim_ss = 10)
84+
det_out['OSP'] = osp_out
85+
qmf_out = qmf_detector(hsi_sub, tgt_spectra, 0.1 * np.eye(hsi_sub.shape[2]))
86+
det_out['QMF'] = qmf_out
87+
sam_out = sam_detector(hsi_sub, tgt_spectra)
88+
det_out['SAM'] = sam_out
89+
spsmf_out = spsmf_detector(hsi_sub, tgt_spectra)
90+
det_out['SPSMF'] = spsmf_out
91+
palm_out = palm_detector(hsi_sub, tgt_spectra, n_comp = 5)
92+
det_out['PALM'] = palm_out
93+
94+
95+
# Segmented Detector Examples
96+
97+
# # # get Segments (using K-means here, but better ways to do this in general, see context-dependent methods for detection)
98+
# n_cluster = 3
99+
# n_row, n_col, n_band = hsi_sub.shape
100+
# idx = KMeans(n_clusters = n_cluster, n_init = 1).fit(hsi_sub.reshape((n_row * n_col, n_band), order='F')).labels_
101+
# idx_img = idx.reshape((n_row,n_col), order='F')
102+
103+
# segments = np.zeros((n_cluster,n_row,n_col))
104+
# for i in range(n_cluster):
105+
# segments[i,:,:] = idx_img == i
106+
107+
# # Segmented Spectral Angle Mapper
108+
# seg_sam_out = img_seg(sam_detector,hsi_sub, tgt_spectra, segments)
109+
# det_out['Seg SAM'] = seg_sam_out
110+
111+
# # Segmented Spectral Angle Mapper
112+
# seg_ace_out,_,_ = img_seg(ace_detector,hsi_sub, tgt_spectra, segments)
113+
# det_out['Seg ACE'] = seg_ace_out
114+
# plt.imshow(seg_ace_out)
115+
# plt.show()
116+
117+
118+
# # visualization
119+
plt.figure(figsize=(10, 15))
120+
plt.subplots_adjust(hspace=.5)
121+
n_row = 5; n_col = 7
122+
#
123+
i = 1
124+
for key, value in det_out.items():
125+
plt.subplot(n_row, n_col, i);
126+
plt.imshow(value); plt.title(key)
127+
i += 1
128+
plt.show()

signature_detectors/fam_statistic.py

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ def fam_statistic(hsi_img, tgt_sig, mu = None, sig_inv = None):
1616
6/2/2018 - Edited by Alina Zare
1717
11/2018 - Python Implementation by Yutai Zhou
1818
"""
19+
if tgt_sig.ndim == 1:
20+
tgt_sig = tgt_sig[:, np.newaxis]
21+
1922
# assume target variance same as background variance
2023
n_row, n_col, n_band = hsi_img.shape
2124
n_pixel = n_row * n_col

signature_detectors/ftmf_detector.py

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ def ftmf_detector(hsi_img, tgt_sig, gamma = 1):
1919
is a scaled version of bg variance
2020
Eismann pp 681
2121
"""
22+
if tgt_sig.ndim == 1:
23+
tgt_sig = tgt_sig[:, np.newaxis]
24+
2225
n_row, n_col, n_band = hsi_img.shape
2326
n_pixel = n_row * n_col
2427

signature_detectors/ha_detector.py

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ def ha_detector(hsi_img, tgt_sig, ems, mask = None, n_comp = 2):
2424
6/2/2018 - Edited by Alina Zare
2525
12/2018 - Python Implementation by Yutai Zhou
2626
"""
27+
if tgt_sig.ndim == 1:
28+
tgt_sig = tgt_sig[:, np.newaxis]
29+
2730
ha_out, kwargsout = img_det(ha_helper, hsi_img, tgt_sig, mask, ems = ems, n_comp = n_comp)
2831
return ha_out
2932

signature_detectors/hsd_detector.py

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ def hsd_detector(hsi_img, tgt_sig, ems, mask = None, sig_inv = None):
3030
6/2/2018 - Edited by Alina Zare
3131
12/2018 - Python Implementation by Yutai Zhou
3232
"""
33+
if tgt_sig.ndim == 1:
34+
tgt_sig = tgt_sig[:, np.newaxis]
35+
3336
hsd_out, kwargsout = img_det(hsd_helper, hsi_img, tgt_sig, mask, ems = ems, sig_inv = sig_inv)
3437
return hsd_out, kwargsout['tgt_p']
3538

signature_detectors/hsd_local_detector.py

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ def hsd_local_detector(hsi_img, tgt_sig, ems, mask = None, guard_win = 2, bg_win
2424
6/3/2018 - Edited by Alina Zare
2525
12/2018 - Python Implementation by Yutai Zhou
2626
"""
27+
if tgt_sig.ndim == 1:
28+
tgt_sig = tgt_sig[:, np.newaxis]
29+
2730
n_row, n_col, n_band = hsi_img.shape
2831
hsi_data = hsi_img.reshape((n_row * n_col, n_band), order='F').T
2932

signature_detectors/hua_detector.py

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ def hua_detector(hsi_img, tgt_sig, ems, mask = None, n_comp = 2, sig_inv = None)
3030
6/3/2018 - Edited by Alina Zare
3131
12/2018 - Python Implementation by Yutai Zhou
3232
"""
33+
if tgt_sig.ndim == 1:
34+
tgt_sig = tgt_sig[:, np.newaxis]
35+
3336
hua_out, kwargsout = img_det(hua_helper, hsi_img, tgt_sig, mask, ems = ems, n_comp = n_comp, sig_inv = sig_inv)
3437
return hua_out
3538

signature_detectors/mtmf_statistic.py

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ def mtmf_statistic(hsi_img,tgt_sig, mask = None):
2222
8/12/2012 - Taylor C. Glenn - [email protected]
2323
12/2018 - Python Implementation by Yutai Zhou
2424
"""
25+
if tgt_sig.ndim == 1:
26+
tgt_sig = tgt_sig[:, np.newaxis]
27+
2528
mnf_img, n_dim, mnf_vecs, mnf_eigvals, mnf_mu = mnf(hsi_img,1);
2629
# tgt_sig = tgt_sig[:n_dim,0][:,np.newaxis]
2730
s = mnf_vecs @ (tgt_sig - mnf_mu)

signature_detectors/osp_detector.py

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ def osp_detector(hsi_img, tgt_sig, mask = None, n_dim_ss = 2):
2222
6/2/2018 - Edited by Alina Zare
2323
11/2018 - Python Implementation by Yutai Zhou
2424
"""
25+
if tgt_sig.ndim == 1:
26+
tgt_sig = tgt_sig[:, np.newaxis]
27+
2528
osp_out, kwargsout = img_det(osp_helper, hsi_img, tgt_sig, mask, n_dim_ss = n_dim_ss)
2629

2730
return osp_out

signature_detectors/palm_detector.py

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ def palm_detector(hsi_img, tgt_sig, mask = None, n_comp = 5):
2222
6/2/2018 - Edited by Alina Zare
2323
12/2018 - Python Implementation by Yutai Zhou
2424
"""
25+
if tgt_sig.ndim == 1:
26+
tgt_sig = tgt_sig[:, np.newaxis]
27+
2528
palm_out, kwargsout = img_det(palm_helper, hsi_img, tgt_sig, mask, n_comp = n_comp)
2629
return palm_out
2730

0 commit comments

Comments
 (0)