diff --git a/python/classify.py b/python/classify.py index 496367f..ffcbea9 100644 --- a/python/classify.py +++ b/python/classify.py @@ -12,7 +12,7 @@ import sklearn.metrics import scipy import scipy.misc import envi -import spectral +import hyperspectral import random import progressbar import matplotlib.pyplot as plt @@ -177,7 +177,7 @@ def envi_batch_predict(E, C, batch=10000): else: Tv = numpy.concatenate((Tv, C.predict(Fv.transpose()).transpose()), 0) tempmask = E.batchmask() - Lv = spectral.unsift2(Tv, tempmask) + Lv = hyperspectral.unsift2(Tv, tempmask) Cv = label2class(Lv.squeeze(), background=0) RGB = class2color(Cv) plt.imshow(RGB) diff --git a/python/digitalstain.py b/python/digitalstain.py index 9406846..71aa6b7 100644 --- a/python/digitalstain.py +++ b/python/digitalstain.py @@ -5,7 +5,7 @@ Created on Tue Jul 25 16:28:37 2017 @author: david """ -import spectral +import hyperspectral import envi import classify import numpy @@ -18,22 +18,36 @@ import glob import matplotlib.pyplot as plt import random -def generate_stain(envifile, stainfile, N=5000, batch_size=10000, validate=True): - E = envi.envi(envifile) +def generate_stain(envifile, stainfile, maskfile="", trainmask="", N=5000, batch_size=10000, validate=True): + if trainmask == "": + E = envi.envi(envifile) + else: + mask = scipy.misc.imread(trainmask, flatten=True) + E = envi.envi(envifile, mask=mask) + mask = classify.random_mask(E.mask, N) + scipy.misc.imsave("random.bmp", mask) Ft = E.loadmask(mask).transpose() stain = numpy.rollaxis(scipy.misc.imread(stainfile), 2) - Tt = spectral.sift2(stain, mask).transpose() + Tt = hyperspectral.sift2(stain, mask).transpose() + print("Training MLPRegressor...") CLASS = sklearn.neural_network.MLPRegressor(solver='lbfgs', alpha=1e-5, hidden_layer_sizes=(), random_state=1, verbose=True) CLASS.fit(Ft, Tt) if validate == False: return CLASS - plt.ion() + print("Validating Stain...") + plt.ion() + if not maskfile == "": + E.close() #close the ENVI file + mask = scipy.misc.imread(maskfile, flatten=True) + print(numpy.count_nonzero(mask)) + E = envi.envi(envifile, mask=mask) + Fv = E.loadbatch(batch_size) #load the first batch n = 0 while not Fv == []: #loop until an empty batch is returned @@ -41,7 +55,7 @@ def generate_stain(envifile, stainfile, N=5000, batch_size=10000, validate=True) Tv = CLASS.predict(Fv.transpose()).transpose() else: Tv = numpy.append(Tv, CLASS.predict(Fv.transpose()).transpose(), 1) #append the predicted labels from this batch to those of previous batches - COLORS = spectral.unsift2(Tv, E.batchmask()) #convert the matrix of class labels to a 2D array + COLORS = hyperspectral.unsift2(Tv, E.batchmask()) #convert the matrix of class labels to a 2D array RGB = numpy.rollaxis(COLORS, 0, 3).astype(numpy.ubyte) plt.imshow(RGB) #display it plt.pause(0.05) diff --git a/python/envi.py b/python/envi.py index 47dbefa..b8edcba 100644 --- a/python/envi.py +++ b/python/envi.py @@ -100,11 +100,11 @@ class envi_header: #t = map(str.strip, t) #strip all of the tokens in the token list #handle the simple conditions - if l[li].startswith("file type"): - if not l[li].strip().endswith("ENVI Standard"): - print("ERROR: unsupported ENVI file format: " + l[li].strip()) - return - elif l[li].startswith("samples"): + #if l[li].startswith("file type"): + # if not l[li].strip().endswith("ENVI Standard"): + # print("ERROR: unsupported ENVI file format: " + l[li].strip()) + # return + if l[li].startswith("samples"): self.samples = int(l[li].split()[-1]) elif l[li].startswith("lines"): self.lines = int(l[li].split()[-1]) diff --git a/python/hyperspectral.py b/python/hyperspectral.py new file mode 100644 index 0000000..6f56001 --- /dev/null +++ b/python/hyperspectral.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +""" +Created on Sun Jul 23 13:52:22 2017 + +@author: david +""" +import numpy + +#sift a 2D hyperspectral image into a PxB matrix where P is the number of pixels and B is the number of bands +def sift2(I, mask = []): + + #get the shape of the input array + S = I.shape + + #convert that array into a 1D matrix + M = numpy.reshape(I, (S[0], S[1] * S[2])) + + #gif no mask is provided, just return all pixels + if mask == []: + return M + + #if a mask is provided, only return pixels corresponding to that mask + flatmask = numpy.reshape(mask, (S[1] * S[2])) + i = numpy.flatnonzero(flatmask) #get the nonzero indices + return M[:, i] #return pixels corresponding to the masked values + +def unsift2(M, mask): + + #get the size of the input matrix + S = M.shape + + #count the number of nonzero values in the mask + nnz = numpy.count_nonzero(mask) + + #the number of masked values should be the same as the number of pixels in the input matrix + if len(S) == 1: + if not S[0] == nnz: + print("ERROR: expected " + str(nnz) + " pixels based on the mask but there are " + str(S[0]) + " in the matrix.") + elif not S[1] == nnz: + print("ERROR: expected " + str(nnz) + " pixels based on the mask but there are " + str(S[1]) + " in the matrix.") + + + i = numpy.nonzero(mask) + + if len(S) == 1: + I = numpy.zeros((1, mask.shape[0], mask.shape[1]), dtype=M.dtype) + else: + I = numpy.zeros((M.shape[0], mask.shape[0], mask.shape[1]), dtype=M.dtype) + I[:, i[0], i[1]] = M + return I + +#create a function that sifts a color image +#input: image name, mask \ No newline at end of file diff --git a/python/stim_spectral.py b/python/stim_spectral.py deleted file mode 100644 index 6f56001..0000000 --- a/python/stim_spectral.py +++ /dev/null @@ -1,53 +0,0 @@ -# -*- coding: utf-8 -*- -""" -Created on Sun Jul 23 13:52:22 2017 - -@author: david -""" -import numpy - -#sift a 2D hyperspectral image into a PxB matrix where P is the number of pixels and B is the number of bands -def sift2(I, mask = []): - - #get the shape of the input array - S = I.shape - - #convert that array into a 1D matrix - M = numpy.reshape(I, (S[0], S[1] * S[2])) - - #gif no mask is provided, just return all pixels - if mask == []: - return M - - #if a mask is provided, only return pixels corresponding to that mask - flatmask = numpy.reshape(mask, (S[1] * S[2])) - i = numpy.flatnonzero(flatmask) #get the nonzero indices - return M[:, i] #return pixels corresponding to the masked values - -def unsift2(M, mask): - - #get the size of the input matrix - S = M.shape - - #count the number of nonzero values in the mask - nnz = numpy.count_nonzero(mask) - - #the number of masked values should be the same as the number of pixels in the input matrix - if len(S) == 1: - if not S[0] == nnz: - print("ERROR: expected " + str(nnz) + " pixels based on the mask but there are " + str(S[0]) + " in the matrix.") - elif not S[1] == nnz: - print("ERROR: expected " + str(nnz) + " pixels based on the mask but there are " + str(S[1]) + " in the matrix.") - - - i = numpy.nonzero(mask) - - if len(S) == 1: - I = numpy.zeros((1, mask.shape[0], mask.shape[1]), dtype=M.dtype) - else: - I = numpy.zeros((M.shape[0], mask.shape[0], mask.shape[1]), dtype=M.dtype) - I[:, i[0], i[1]] = M - return I - -#create a function that sifts a color image -#input: image name, mask \ No newline at end of file -- libgit2 0.21.4