Skip to content

Commit 37ce691

Browse files
author
Yutai
committed
Merge branch 'master' of github.com:GatorSense/hsi_toolkit_py
2 parents 9d72693 + 009da69 commit 37ce691

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed
Binary file not shown.
+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#function [SpecDist] = PlotSpectraDistribution(Spectra, WaveLengths,SampStuff, FigNum);
2+
###
3+
######################################################################
4+
### A FUNCTION THAT CREATES & DISPLAYS SPECTRA AS A 2D HISTOGRAM ###
5+
### SPECTRA ARE ASSUMED REFLECTANCES OR EMISSIVITIES IN [0,1] ###
6+
### SPECTRA ARE MAPPED TO INTEGERS BETWEEN 0 AND 100 (OR < 100) ###
7+
######################################################################
8+
###
9+
### INPUTS:
10+
### I1. Spectra IS A NUMBER SPECTRA x NUMBER BANDS... ###
11+
### ...ARRAY OF REFLECTANCES OR EMISSIVITIES ###
12+
### I2. WaveLengths IS A VECTOR OF THE SPECTRAL WAVELENGTHS ###
13+
### I3. SampStuff IS A VECTOR CONTAINING ###
14+
### SampInt: FRACTIONAL SIZE OF HISTOGRAM BINS ###
15+
### IntSampInt: INT VERSION OF SampInt ###
16+
### IntTopReflect: INT VALUE OF MAX REF/EMIS BIN ###
17+
### I4. FigNum IS THE INDEX OF THE FIGURE TO USE FOR DISPLAY ###
18+
### IF FigNum < 1, DO NOT DISPLAY ANYTHING ###
19+
###
20+
### OUTPUTS:
21+
### O1. SpecDist IS THE 2D HISTOGRAM ###
22+
##################################################################
23+
### ###
24+
### MATLAB AUTHOR: Darth Gader ###
25+
### PYTHON AUTHOR: Ron Fick ###
26+
### LAST UPDATE: 021519 ###
27+
### ###
28+
##################################################################
29+
def PlotSpectraDistribution(Spectra, WaveLengths, SampStuff, FigNum):
30+
31+
import numpy as np
32+
from scipy import signal
33+
import cv2
34+
import matplotlib.pyplot as plt
35+
from mpl_toolkits.mplot3d import Axes3D
36+
from matplotlib import cm
37+
38+
##
39+
### INITIALIZE PARAMETERS ###
40+
41+
SampInt = SampStuff[0]
42+
IntSampInt = SampStuff[1]
43+
IntTopReflect = SampStuff[2]
44+
SMOOTHSIZE = [3,3]
45+
NumWave = np.size(Spectra, 1)
46+
SpecDist = np.zeros((IntTopReflect, NumWave))
47+
assert NumWave == np.size(WaveLengths), 'Wavelength sizes don''t match'
48+
49+
### MAP SPECTRA TO [0, 100] ###
50+
MappedSpectra = np.minimum(100, (Spectra*99)+1)
51+
MappedSpectra = np.maximum(1, np.round(MappedSpectra/SampInt)*SampInt)
52+
53+
##
54+
### MAKE A HISTOGRAM FOR EACH WAVELENGTH ###
55+
for k in range(NumWave):
56+
SpecDist[:, k] = np.histogram(MappedSpectra[:, k], np.arange(0, IntTopReflect+IntSampInt, IntSampInt))[0]
57+
58+
### SMOOTH BY TAKING A LOCAL MAX FOLLOWED BY A LOCAL AVERAGE ###
59+
SpecDist = cv2.dilate(SpecDist, np.ones((3,3)), iterations=1)
60+
SpecDist = signal.convolve2d(SpecDist, (1/np.prod(SMOOTHSIZE))*np.ones(SMOOTHSIZE), 'same')
61+
62+
##
63+
### DISPLAY AS MESH ###
64+
if(FigNum > 0):
65+
XAxis = WaveLengths;
66+
YAxis = np.arange(0, IntTopReflect, IntSampInt).T
67+
X, Y = np.meshgrid(XAxis, YAxis)
68+
fig = plt.figure(FigNum)
69+
ax = fig.gca(projection='3d')
70+
surf = ax.plot_surface(X, Y, SpecDist, cmap=cm.coolwarm, linewidth=0, antialiased=False)
71+
fig.colorbar(surf, shrink=0.5, aspect=5)
72+
plt.title('Spectra Histogram')
73+
plt.xlabel('Wavelength (nm)')
74+
plt.ylabel('Reflectance')
75+
plt.show()
76+
77+
### END OF FUNCTION ###
78+
#######################
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import scipy.io as sio
2+
import numpy as np
3+
from batchplotspectra import PlotSpectraDistribution
4+
5+
an_hsi_image_sub_for_demo = sio.loadmat('an_hsi_image_sub_for_demo.mat')
6+
hsi_img_sub = an_hsi_image_sub_for_demo['hsi_img_sub']
7+
wavelengths = an_hsi_image_sub_for_demo['wavelengths']
8+
mask_sub = an_hsi_image_sub_for_demo['mask_sub']
9+
10+
x_dims, y_dims, band_dims = hsi_img_sub.shape
11+
12+
mat_data = np.reshape(hsi_img_sub, (x_dims*y_dims, band_dims))
13+
14+
mask_reshaped = np.reshape(mask_sub, (x_dims*y_dims))
15+
16+
masked_data = mat_data[mask_reshaped == 1]
17+
18+
PlotSpectraDistribution(masked_data, wavelengths, [1,1,100], 1)

0 commit comments

Comments
 (0)