| |
1
| +# -*- coding: utf-8 -*- |
| |
2
| +""" |
| |
3
| +Created on Fri Jul 21 20:18:01 2017 |
| |
4
| + |
| |
5
| +@author: david |
| |
6
| +""" |
| |
7
| + |
| |
8
| +import os |
| |
9
| +import numpy |
| |
10
| +import scipy |
| |
11
| +import matplotlib.pyplot as plt |
| |
12
| +import progressbar |
| |
13
| + |
| |
14
| +class envi_header: |
| |
15
| + def __init__(self, filename = ""): |
| |
16
| + if filename != "": |
| |
17
| + self.load(filename) |
| |
18
| + else: |
| |
19
| + self.initialize() |
| |
20
| + |
| |
21
| + #initialization function |
| |
22
| + def initialize(self): |
| |
23
| + self.samples = int(0) |
| |
24
| + self.lines = int(0) |
| |
25
| + self.bands = int(0) |
| |
26
| + self.header_offset = int(0) |
| |
27
| + self.data_type = int(4) |
| |
28
| + self.interleave = "bsq" |
| |
29
| + self.sensor_type = "" |
| |
30
| + self.byte_order = int(0) |
| |
31
| + self.x_start = int(0) |
| |
32
| + self.y_start = int(0) |
| |
33
| + self.z_plot_titles = "" |
| |
34
| + self.pixel_size = [float(0), float(0)] |
| |
35
| + self.pixel_size_units = "Meters" |
| |
36
| + self.wavelength_units = "Wavenumber" |
| |
37
| + self.description = "" |
| |
38
| + self.band_names = [] |
| |
39
| + self.wavelength = [] |
| |
40
| + |
| |
41
| + #convert an ENVI data_type value to a numpy data type |
| |
42
| + def get_numpy_type(self, val): |
| |
43
| + if val == 1: |
| |
44
| + return numpy.byte |
| |
45
| + elif val == 2: |
| |
46
| + return numpy.int16 |
| |
47
| + elif val == 3: |
| |
48
| + return numpy.int32 |
| |
49
| + elif val == 4: |
| |
50
| + return numpy.float32 |
| |
51
| + elif val == 5: |
| |
52
| + return numpy.float64 |
| |
53
| + elif val == 6: |
| |
54
| + return numpy.complex64 |
| |
55
| + elif val == 9: |
| |
56
| + return numpy.complex128 |
| |
57
| + elif val == 12: |
| |
58
| + return numpy.uint16 |
| |
59
| + elif val == 13: |
| |
60
| + return numpy.uint32 |
| |
61
| + elif val == 14: |
| |
62
| + return numpy.int64 |
| |
63
| + elif val == 15: |
| |
64
| + return numpy.uint64 |
| |
65
| + |
| |
66
| + def get_envi_type(self, val): |
| |
67
| + if val == numpy.byte: |
| |
68
| + return 1 |
| |
69
| + elif val == numpy.int16: |
| |
70
| + return 2 |
| |
71
| + elif val == numpy.int32: |
| |
72
| + return 3 |
| |
73
| + elif val == numpy.float32: |
| |
74
| + return 4 |
| |
75
| + elif val == numpy.float64: |
| |
76
| + return 5 |
| |
77
| + elif val == numpy.complex64: |
| |
78
| + return 6 |
| |
79
| + elif val == numpy.complex128: |
| |
80
| + return 9 |
| |
81
| + elif val == numpy.uint16: |
| |
82
| + return 12 |
| |
83
| + elif val == numpy.uint32: |
| |
84
| + return 13 |
| |
85
| + elif val == numpy.int64: |
| |
86
| + return 14 |
| |
87
| + elif val == numpy.uint64: |
| |
88
| + return 15 |
| |
89
| + |
| |
90
| + def load(self, fname): |
| |
91
| + f = open(fname) |
| |
92
| + l = f.readlines() |
| |
93
| + if l[0].strip() != "ENVI": |
| |
94
| + print("ERROR: not an ENVI file") |
| |
95
| + return |
| |
96
| + li = 1 |
| |
97
| + while li < len(l): |
| |
98
| + #t = l[li].split() #split the line into tokens |
| |
99
| + #t = map(str.strip, t) #strip all of the tokens in the token list |
| |
100
| + |
| |
101
| + #handle the simple conditions |
| |
102
| + if l[li].startswith("file type"): |
| |
103
| + if not l[li].strip().endswith("ENVI Standard"): |
| |
104
| + print("ERROR: unsupported ENVI file format: " + l[li].strip()) |
| |
105
| + return |
| |
106
| + elif l[li].startswith("samples"): |
| |
107
| + self.samples = int(l[li].split()[-1]) |
| |
108
| + elif l[li].startswith("lines"): |
| |
109
| + self.lines = int(l[li].split()[-1]) |
| |
110
| + elif l[li].startswith("bands"): |
| |
111
| + self.bands = int(l[li].split()[-1]) |
| |
112
| + elif l[li].startswith("header offset"): |
| |
113
| + self.header_offset = int(l[li].split()[-1]) |
| |
114
| + elif l[li].startswith("data type"): |
| |
115
| + self.data_type = self.get_numpy_type(int(l[li].split()[-1])) |
| |
116
| + elif l[li].startswith("interleave"): |
| |
117
| + self.interleave = l[li].split()[-1].strip() |
| |
118
| + elif l[li].startswith("sensor type"): |
| |
119
| + self.sensor_type = l[li].split()[-1].strip() |
| |
120
| + elif l[li].startswith("byte order"): |
| |
121
| + self.byte_order = int(l[li].split()[-1]) |
| |
122
| + elif l[li].startswith("x start"): |
| |
123
| + self.x_start = int(l[li].split()[-1]) |
| |
124
| + elif l[li].startswith("y start"): |
| |
125
| + self.y_start = int(l[li].split()[-1]) |
| |
126
| + elif l[li].startswith("z plot titles"): |
| |
127
| + i0 = l[li].rindex('{') |
| |
128
| + i1 = l[li].rindex('}') |
| |
129
| + self.z_plot_titles = l[li][i0 + 1 : i1] |
| |
130
| + elif l[li].startswith("pixel size"): |
| |
131
| + i0 = l[li].rindex('{') |
| |
132
| + i1 = l[li].rindex('}') |
| |
133
| + s = l[li][i0 + 1 : i1].split(',') |
| |
134
| + self.pixel_size = [float(s[0]), float(s[1])] |
| |
135
| + self.pixel_size_units = s[2][s[2].rindex('=') + 1:].strip() |
| |
136
| + elif l[li].startswith("wavelength units"): |
| |
137
| + self.wavelength_units = l[li].split()[-1].strip() |
| |
138
| + |
| |
139
| + #handle the complicated conditions |
| |
140
| + elif l[li].startswith("description"): |
| |
141
| + desc = [l[li]] |
| |
142
| + ''' |
| |
143
| + while l[li].strip()[-1] != '}': #will fail if l[li].strip() is empty |
| |
144
| + li += 1 |
| |
145
| + desc.append(l[li]) |
| |
146
| + ''' |
| |
147
| + while True: |
| |
148
| + if l[li].strip(): |
| |
149
| + if l[li].strip()[-1] == '}': |
| |
150
| + break |
| |
151
| + li += 1 |
| |
152
| + desc.append(l[li]) |
| |
153
| + |
| |
154
| + desc = ''.join(list(map(str.strip, desc))) #strip all white space from the string list |
| |
155
| + i0 = desc.rindex('{') |
| |
156
| + i1 = desc.rindex('}') |
| |
157
| + self.description = desc[i0 + 1 : i1] |
| |
158
| + |
| |
159
| + elif l[li].startswith("band names"): |
| |
160
| + names = [l[li]] |
| |
161
| + while l[li].strip()[-1] != '}': |
| |
162
| + li += 1 |
| |
163
| + names.append(l[li]) |
| |
164
| + names = ''.join(list(map(str.strip, names))) #strip all white space from the string list |
| |
165
| + i0 = names.rindex('{') |
| |
166
| + i1 = names.rindex('}') |
| |
167
| + names = names[i0 + 1 : i1] |
| |
168
| + self.band_names = list(map(str.strip, names.split(','))) |
| |
169
| + elif l[li].startswith("wavelength"): |
| |
170
| + waves = [l[li]] |
| |
171
| + while l[li].strip()[-1] != '}': |
| |
172
| + li += 1 |
| |
173
| + waves.append(l[li]) |
| |
174
| + waves = ''.join(list(map(str.strip, waves))) #strip all white space from the string list |
| |
175
| + i0 = waves.rindex('{') |
| |
176
| + i1 = waves.rindex('}') |
| |
177
| + waves = waves[i0 + 1 : i1] |
| |
178
| + self.wavelength = list(map(float, waves.split(','))) |
| |
179
| + |
| |
180
| + li += 1 |
| |
181
| + |
| |
182
| + f.close() |
| |
183
| + |
| |
184
| +class envi: |
| |
185
| + def __init__(self, filename, headername = "", mask = []): |
| |
186
| + self.open(filename, headername) |
| |
187
| + if mask == []: |
| |
188
| + self.mask = numpy.ones((self.header.lines, self.header.samples), dtype=numpy.bool) |
| |
189
| + else: |
| |
190
| + self.mask = mask |
| |
191
| + self.idx = 0 #initialize the batch IDX to 0 for batch reading |
| |
192
| + |
| |
193
| + def open(self, filename, headername = ""): |
| |
194
| + if headername == "": |
| |
195
| + headername = filename + ".hdr" |
| |
196
| + |
| |
197
| + if not os.path.isfile(filename): |
| |
198
| + print("ERROR: " + filename + " not found") |
| |
199
| + return |
| |
200
| + if not os.path.isfile(headername): |
| |
201
| + print("ERROR: " + headername + " not found") |
| |
202
| + return |
| |
203
| + |
| |
204
| + #open the file |
| |
205
| + self.header = envi_header(headername) |
| |
206
| + self.file = open(filename, "rb") |
| |
207
| + |
| |
208
| + def loadall(self): |
| |
209
| + X = self.header.samples |
| |
210
| + Y = self.header.lines |
| |
211
| + B = self.header.bands |
| |
212
| + |
| |
213
| + #load the data |
| |
214
| + D = numpy.fromfile(self.file, dtype=self.header.data_type) |
| |
215
| + |
| |
216
| + if self.header.interleave == "bsq": |
| |
217
| + return numpy.reshape(D, (B, Y, X)) |
| |
218
| + #return numpy.swapaxes(D, 0, 2) |
| |
219
| + elif self.header.interleave == "bip": |
| |
220
| + D = numpy.reshape(D, (Y, X, B)) |
| |
221
| + return numpy.rollaxis(D, 2) |
| |
222
| + elif self.header.interleave == "bil": |
| |
223
| + D = numpy.reshape(D, (Y, B, X)) |
| |
224
| + return numpy.rollaxis(D, 1) |
| |
225
| + |
| |
226
| + #loads all of the pixels where mask != 0 and returns them as a matrix |
| |
227
| + def loadmask(self, mask): |
| |
228
| + X = self.header.samples |
| |
229
| + Y = self.header.lines |
| |
230
| + B = self.header.bands |
| |
231
| + |
| |
232
| + P = numpy.count_nonzero(mask) #count the number of zeros in the mask file |
| |
233
| + M = numpy.zeros((B, P), dtype=self.header.data_type) |
| |
234
| + type_bytes = numpy.dtype(self.header.data_type).itemsize |
| |
235
| + |
| |
236
| + prev_pos = self.file.tell() |
| |
237
| + self.file.seek(0) |
| |
238
| + if self.header.interleave == "bip": |
| |
239
| + spectrum = numpy.zeros(B, dtype=self.header.data_type) |
| |
240
| + flatmask = numpy.reshape(mask, (X * Y)) |
| |
241
| + i = numpy.flatnonzero(flatmask) |
| |
242
| + bar = progressbar.ProgressBar(max_value = P) |
| |
243
| + for p in range(0, P): |
| |
244
| + self.file.seek(i[p] * B * type_bytes) |
| |
245
| + self.file.readinto(spectrum) |
| |
246
| + M[:, p] = spectrum |
| |
247
| + bar.update(p+1) |
| |
248
| + if self.header.interleave == "bsq": |
| |
249
| + band = numpy.zeros(mask.shape, dtype=self.header.data_type) |
| |
250
| + i = numpy.nonzero(mask) |
| |
251
| + bar = progressbar.ProgressBar(max_value=B) |
| |
252
| + for b in range(0, B): |
| |
253
| + self.file.seek(b * X * Y * type_bytes) |
| |
254
| + self.file.readinto(band) |
| |
255
| + M[b, :] = band[i] |
| |
256
| + bar.update(b+1) |
| |
257
| + if self.header.interleave == "bil": |
| |
258
| + plane = numpy.zeros((B, X), dtype=self.header.data_type) |
| |
259
| + p = 0 |
| |
260
| + bar = progressbar.ProgressBar(max_value=Y) |
| |
261
| + for l in range(0, Y): |
| |
262
| + i = numpy.flatnonzero(mask[l, :]) |
| |
263
| + self.file.readinto(plane) |
| |
264
| + M[:, p:p+i.shape[0]] = plane[:, i] |
| |
265
| + p = p + i.shape[0] |
| |
266
| + bar.update(l+1) |
| |
267
| + self.file.seek(prev_pos) |
| |
268
| + return M |
| |
269
| + |
| |
270
| + def loadband(self, n): |
| |
271
| + X = self.header.samples |
| |
272
| + Y = self.header.lines |
| |
273
| + B = self.header.bands |
| |
274
| + |
| |
275
| + band = numpy.zeros((Y, X), dtype=self.header.data_type) |
| |
276
| + type_bytes = numpy.dtype(self.header.data_type).itemsize |
| |
277
| + |
| |
278
| + prev_pos = self.file.tell() |
| |
279
| + if self.header.interleave == "bsq": |
| |
280
| + self.file.seek(n * X * Y * type_bytes) |
| |
281
| + self.file.readinto(band) |
| |
282
| + self.file.seek(prev_pos) |
| |
283
| + return band |
| |
284
| + |
| |
285
| + #create a set of feature/target pairs for classification |
| |
286
| + #input: envi file object, stack of class masks C x Y x X |
| |
287
| + #output: feature matrix (features x pixels), target matrix (1 x pixels) |
| |
288
| + #example: generate_training(("class_coll.bmp", "class_epith.bmp"), (1, 2)) |
| |
289
| + def loadtrain(self, classimages): |
| |
290
| + |
| |
291
| + # get number of classes |
| |
292
| + C = classimages.shape[0] |
| |
293
| + |
| |
294
| + F = [] |
| |
295
| + T = [] |
| |
296
| + for c in range(0, C): |
| |
297
| + f = self.loadmask(classimages[c, :, :]) #load the feature matrix for class c |
| |
298
| + t = numpy.ones((f.shape[1])) * (c+1) #generate a target array |
| |
299
| + F.append(f) |
| |
300
| + T.append(t) |
| |
301
| + |
| |
302
| + return numpy.concatenate(F, 1).transpose(), numpy.concatenate(T) |
| |
303
| + |
| |
304
| + #read a batch of data based on the mask |
| |
305
| + def loadbatch(self, npixels): |
| |
306
| + i = numpy.flatnonzero(self.mask) #get the indices of valid pixels |
| |
307
| + if len(i) == self.idx: #if all of the pixels have been read, return an empyt array |
| |
308
| + return [] |
| |
309
| + npixels = min(npixels, len(i) - self.idx) #if there aren't enough pixels, change the batch size |
| |
310
| + B = self.header.bands |
| |
311
| + |
| |
312
| + batch = numpy.zeros((B, npixels), dtype=self.header.data_type) #allocate space for the batch |
| |
313
| + pixel = numpy.zeros((B), dtype=self.header.data_type) #allocate space for a single pixel |
| |
314
| + type_bytes = numpy.dtype(self.header.data_type).itemsize #calculate the size of a single value |
| |
315
| + if self.header.interleave == "bip": |
| |
316
| + for n in range(0, npixels): #for each pixel in the batch |
| |
317
| + self.file.seek(i[self.idx] * B * type_bytes) #seek to the current pixel in the file |
| |
318
| + self.file.readinto(pixel) #read a single pixel |
| |
319
| + batch[:, n] = pixel #save the pixel into the batch matrix |
| |
320
| + self.idx = self.idx + 1 |
| |
321
| + return batch |
| |
322
| + elif self.header.interleave == "bsq": |
| |
323
| + print("ERROR: BSQ batch loading isn't implemented yet!") |
| |
324
| + elif self.header.interleave == "bil": |
| |
325
| + print("ERROR: BIL batch loading isn't implemented yet!") |
| |
326
| + |
| |
327
| + #returns the current batch index |
| |
328
| + def getidx(self): |
| |
329
| + return self.idx |
| |
330
| + |
| |
331
| + #returns an image of the pixels that have been read using batch loading |
| |
332
| + def batchmask(self): |
| |
333
| + #allocate a new mask |
| |
334
| + outmask = numpy.zeros(self.mask.shape, dtype=numpy.bool) |
| |
335
| + |
| |
336
| + #zero out any unclassified pixels |
| |
337
| + idx = self.getidx() |
| |
338
| + i = numpy.nonzero(self.mask) |
| |
339
| + outmask[i[0][0:idx], i[1][0:idx]] = self.mask[i[0][0:idx], i[1][0:idx]] |
| |
340
| + return outmask |
| |
341
| + |
| |
342
| + |
| |
343
| + def __del__(self): |
| |
344
| + self.file.close() |
0
| \ No newline at end of file |
345
| \ No newline at end of file |