diff --git a/python/classify.py b/python/classify.py index 61f8fd8..ef6e610 100644 --- a/python/classify.py +++ b/python/classify.py @@ -16,7 +16,7 @@ import envi #generate a 2D color class map using a stack of binary class images #input: C is a C x Y x X binary image #output: an RGB color image with a unique color for each class -def classcolor2(C): +def class2color(C): #determine the number of classes nc = C.shape[0] @@ -119,6 +119,17 @@ def prob2roc(P, t_vals, mask=[]): return sklearn.metrics.roc_curve(labels, scores) +#convert a label image to a C x Y x X class image +def label2class(L): + unique = numpy.unique(L) + s = L.shape + s = numpy.append(numpy.array((len(unique)-1)), s) + print(s) + C = numpy.zeros(s, dtype=numpy.bool) + for i in range(1, len(unique)): + C[i-1, :, :] = L == unique[i] + return C + #Function to convert a set of class labels to a matrix of neuron responses for an ANN #Function CNN extraction function \ No newline at end of file diff --git a/python/envi.py b/python/envi.py index 4b3f9c5..b4619fc 100644 --- a/python/envi.py +++ b/python/envi.py @@ -182,12 +182,13 @@ class envi_header: f.close() class envi: - def __init__(self, filename, headername = "", maskname = ""): + def __init__(self, filename, headername = "", mask = []): self.open(filename, headername) - if maskname == "": + if mask == []: self.mask = numpy.ones((self.header.samples, self.header.lines), dtype=numpy.bool) else: - self.mask = scipy.misc.imread(maskname, flatten=True).astype(numpy.bool) + self.mask = mask + self.idx = 0 #initialize the batch IDX to 0 for batch reading def open(self, filename, headername = ""): if headername == "": @@ -232,6 +233,7 @@ class envi: M = numpy.zeros((B, P), dtype=self.header.data_type) type_bytes = numpy.dtype(self.header.data_type).itemsize + prev_pos = self.file.tell() self.file.seek(0) if self.header.interleave == "bip": spectrum = numpy.zeros(B, dtype=self.header.data_type) @@ -262,6 +264,7 @@ class envi: M[:, p:p+i.shape[0]] = plane[:, i] p = p + i.shape[0] bar.update(l+1) + self.file.seek(prev_pos) return M def loadband(self, n): @@ -271,11 +274,12 @@ class envi: band = numpy.zeros((Y, X), dtype=self.header.data_type) type_bytes = numpy.dtype(self.header.data_type).itemsize - + + prev_pos = self.file.tell() if self.header.interleave == "bsq": self.file.seek(n * X * Y * type_bytes) self.file.readinto(band) - + self.file.seek(prev_pos) return band #create a set of feature/target pairs for classification @@ -296,7 +300,33 @@ class envi: T.append(t) return numpy.concatenate(F, 1).transpose(), numpy.concatenate(T) - + + #read a batch of data based on the mask + def loadbatch(self, npixels): + i = numpy.flatnonzero(self.mask) #get the indices of valid pixels + npixels = min(npixels, len(i) - self.idx - 1) #if there aren't enough pixels, change the batch size + B = self.header.bands + + batch = numpy.zeros((B, npixels), dtype=self.header.data_type) #allocate space for the batch + pixel = numpy.zeros((B), dtype=self.header.data_type) #allocate space for a single pixel + type_bytes = numpy.dtype(self.header.data_type).itemsize #calculate the size of a single value + if self.header.interleave == "bip": + for n in range(0, npixels): #for each pixel in the batch + self.file.seek(i[self.idx] * B * type_bytes) #seek to the current pixel in the file + self.file.readinto(pixel) #read a single pixel + batch[:, n] = pixel #save the pixel into the batch matrix + self.idx = self.idx + 1 + #print("idx: " + str(self.idx)) + #print("tell(): " + str(self.file.tell())) + return batch + elif self.header.interleave == "bsq": + print("ERROR: BSQ batch loading isn't implemented yet!") + elif self.header.interleave == "bil": + print("ERROR: BIL batch loading isn't implemented yet!") + + #returns the current batch index + def getidx(self): + return self.idx def __del__(self): self.file.close() \ No newline at end of file -- libgit2 0.21.4