Commit f3de42f83d400e423932a2e28a72d9ee65ab704d
1 parent
c8b12984
added stim_spectral.py
Showing
1 changed file
with
53 additions
and
0 deletions
Show diff stats
1 | +# -*- coding: utf-8 -*- | |
2 | +""" | |
3 | +Created on Sun Jul 23 13:52:22 2017 | |
4 | + | |
5 | +@author: david | |
6 | +""" | |
7 | +import numpy | |
8 | + | |
9 | +#sift a 2D hyperspectral image into a PxB matrix where P is the number of pixels and B is the number of bands | |
10 | +def sift2(I, mask = []): | |
11 | + | |
12 | + #get the shape of the input array | |
13 | + S = I.shape | |
14 | + | |
15 | + #convert that array into a 1D matrix | |
16 | + M = numpy.reshape(I, (S[0], S[1] * S[2])) | |
17 | + | |
18 | + #gif no mask is provided, just return all pixels | |
19 | + if mask == []: | |
20 | + return M | |
21 | + | |
22 | + #if a mask is provided, only return pixels corresponding to that mask | |
23 | + flatmask = numpy.reshape(mask, (S[1] * S[2])) | |
24 | + i = numpy.flatnonzero(flatmask) #get the nonzero indices | |
25 | + return M[:, i] #return pixels corresponding to the masked values | |
26 | + | |
27 | +def unsift2(M, mask): | |
28 | + | |
29 | + #get the size of the input matrix | |
30 | + S = M.shape | |
31 | + | |
32 | + #count the number of nonzero values in the mask | |
33 | + nnz = numpy.count_nonzero(mask) | |
34 | + | |
35 | + #the number of masked values should be the same as the number of pixels in the input matrix | |
36 | + if len(S) == 1: | |
37 | + if not S[0] == nnz: | |
38 | + print("ERROR: expected " + str(nnz) + " pixels based on the mask but there are " + str(S[0]) + " in the matrix.") | |
39 | + elif not S[1] == nnz: | |
40 | + print("ERROR: expected " + str(nnz) + " pixels based on the mask but there are " + str(S[1]) + " in the matrix.") | |
41 | + | |
42 | + | |
43 | + i = numpy.nonzero(mask) | |
44 | + | |
45 | + if len(S) == 1: | |
46 | + I = numpy.zeros((1, mask.shape[0], mask.shape[1]), dtype=M.dtype) | |
47 | + else: | |
48 | + I = numpy.zeros((M.shape[0], mask.shape[0], mask.shape[1]), dtype=M.dtype) | |
49 | + I[:, i[0], i[1]] = M | |
50 | + return I | |
51 | + | |
52 | +#create a function that sifts a color image | |
53 | +#input: image name, mask | |
0 | 54 | \ No newline at end of file | ... | ... |