-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
171 lines (118 loc) · 3.86 KB
/
utils.py
File metadata and controls
171 lines (118 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# -*- Encoding: Latin-1 -*-
#!/usr/bin/python
from math import *
import numpy as np
import matplotlib.pyplot as plt
from scipy.io import loadmat
# ------------------------------------------------------------------------------
# Hyperspectral Image (HSI) data
# ------------------------------------------------------------------------------
class HSI:
"""
Class used to represent Hyperspectral Image (HSI) data
Parameters
----------
data: numpy array
spectral observations
rows: int
number of rows in the image
cols: int
number of columns in the image
Attributes
----------
data: numpy array
spectral observations
rows: int
number of rows in the image
cols: int
number of columns in the image
bands: int
number of spectral bands
image: array-like
hyperspectral image
gt: numpy array
endmembers
abundances_map: numpy array
abundances map
"""
def __init__(self, data, rows, cols, endmembers, abundances_map):
if data.shape[0] < data.shape[1]:
data = data.transpose()
self.bands = data.shape[1]
self.cols = cols
self.rows = rows
self.image = np.reshape(data,(self.rows, self.cols, self.bands))
self.endmembers = endmembers
self.abundances_map = abundances_map
def get_spectra(self):
"""
Returns the spectral observations
Returns
-------
out: numpy array
spectral observations
"""
return np.reshape(self.image, (self.rows*self.cols, self.bands))
def get_abundances(self):
"""
Return the abundances associated with the spectral observations
Return
------
out: numpy array (size: n by k)
abundances
"""
return np.reshape(self.abundances_map, (self.rows*self.cols, -1))
def get_bands(self, bands):
"""
Return the channel of the HSI corresponding to the specified band
Parameters
----------
bands: int
index of the spectral band to return
Return
------
out: numpy array
channel of the HSI associated with the specified band
"""
return self.image[:, :, bands]
def crop_image(self,start_x,start_y,delta_x=None,delta_y=None):
"""
Apply a spatial crop to the HSI and returns the cropped image
Parameters
----------
start_x, start_y: int
coordinates of the top left pixel of the cropped image
delta_x, delta_y: int
shape of the cropped image
if set to None, then the bottom right pixel of the cropped is taken
to be the bottom right pixel of the original image
Returns
-------
out: numpy array
cropped image
"""
if delta_x is None: delta_x = self.cols - start_x
if delta_y is None: delta_y = self.rows - start_y
return self.image[start_x:delta_x+start_x,start_y:delta_y+start_y,:]
def load_HSI(path):
"""
Load an hyperspectral image from a .mat file
Parameters
----------
path: string
location of the .mat file
Returns
-------
out: instance of the class HSI
HSI data
"""
data = loadmat(path)
Y = np.asarray(data['Y'], dtype=np.float32)
n_rows = data['lines'].item()
n_cols = data['cols'].item()
abundances_map = data['S_GT']
if 'GT' in data.keys():
gt = np.asarray(data['GT'], dtype=np.float32)
else:
gt = None
return HSI(Y, n_rows, n_cols, gt, data['S_GT']), Y, data