|
| 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() |
0 commit comments