diff --git a/python/classify.py b/python/classify.py index 2cf4388..32e39fc 100644 --- a/python/classify.py +++ b/python/classify.py @@ -12,7 +12,10 @@ import sklearn.metrics import scipy import scipy.misc import envi +import spectral import random +import progressbar +import matplotlib.pyplot as plt #generate a 2D color class map using a stack of binary class images #input: C is a C x Y x X binary image @@ -40,7 +43,7 @@ def class2color(C): #input: list of class image names #output: C x Y x X binary image specifying class/pixel membership #example: image2class(("class_coll.bmp", "class_epith.bmp")) -def image2class(masks): +def filenames2class(masks): #get num of mask file names num_masks = len(masks) @@ -50,9 +53,11 @@ def image2class(masks): return classimages = [] - for m in masks: - img = scipy.misc.imread(m, flatten=True).astype(numpy.bool) + bar = progressbar.ProgressBar(max_value=num_masks) + for m in range(0, num_masks): + img = scipy.misc.imread(masks[m], flatten=True).astype(numpy.bool) classimages.append(img) + bar.update(m+1) result = numpy.stack(classimages) sum_images = numpy.sum(result.astype(numpy.uint32), 0) @@ -141,7 +146,24 @@ def random_mask(M, n): new_mask[numpy.unravel_index(new_idx[0:n], new_mask.shape)] = True return new_mask - -#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 +def envi_batch_predict(E, C, batch=10000): + + Fv = E.loadbatch(batch).transpose() + i = 0 + Tv = [] + plt.ion() + bar = progressbar.ProgressBar(max_value=numpy.count_nonzero(E.mask)) + while not Fv == []: + if i == 0: + Tv = C.predict(Fv) + else: + Tv = numpy.concatenate((Tv, C.predict(Fv).transpose()), 0) + tempmask = E.batchmask() + Lv = spectral.unsift2(Tv, tempmask) + Cv = label2class(Lv.squeeze(), background=0) + RGB = class2color(Cv) + plt.imshow(RGB) + plt.pause(0.05) + Fv = E.loadbatch(batch).transpose() + i = i + 1 + bar.update(len(Tv)) \ No newline at end of file diff --git a/python/envi.py b/python/envi.py index 22c54ec..e624d60 100644 --- a/python/envi.py +++ b/python/envi.py @@ -10,6 +10,7 @@ import numpy import scipy import matplotlib.pyplot as plt import progressbar +import sys class envi_header: def __init__(self, filename = ""): @@ -245,7 +246,7 @@ class envi: self.file.readinto(spectrum) M[:, p] = spectrum bar.update(p+1) - if self.header.interleave == "bsq": + elif self.header.interleave == "bsq": band = numpy.zeros(mask.shape, dtype=self.header.data_type) i = numpy.nonzero(mask) bar = progressbar.ProgressBar(max_value=B) @@ -254,7 +255,7 @@ class envi: self.file.readinto(band) M[b, :] = band[i] bar.update(b+1) - if self.header.interleave == "bil": + elif self.header.interleave == "bil": plane = numpy.zeros((B, X), dtype=self.header.data_type) p = 0 bar = progressbar.ProgressBar(max_value=Y) @@ -294,6 +295,7 @@ class envi: F = [] T = [] for c in range(0, C): + print("\nLoading class " + str(c+1) + "...") f = self.loadmask(classimages[c, :, :]) #load the feature matrix for class c t = numpy.ones((f.shape[1])) * (c+1) #generate a target array F.append(f) -- libgit2 0.21.4