Commit 254e3bc8fcb7eba1708594772763cdc7d5bfc095
1 parent
59f31ea4
added a full classification function for envi files and updated color selection
Showing
1 changed file
with
19 additions
and
6 deletions
Show diff stats
python/classify.py
@@ -17,6 +17,20 @@ import random | @@ -17,6 +17,20 @@ import random | ||
17 | import progressbar | 17 | import progressbar |
18 | import matplotlib.pyplot as plt | 18 | import matplotlib.pyplot as plt |
19 | 19 | ||
20 | +#generate N qualitative colors and return the value for color c | ||
21 | +def qualcolor(c, N): | ||
22 | + dN = numpy.ceil(numpy.sqrt(N)).astype(numpy.int32) | ||
23 | + h = c/N | ||
24 | + | ||
25 | + sp = c/N * 2 * numpy.pi * dN + numpy.pi/2 | ||
26 | + s = numpy.sin(sp) * 0.25 + 0.75 | ||
27 | + | ||
28 | + vp = c/N * 2 * numpy.pi * dN | ||
29 | + v = numpy.sin(vp) * 0.25 + 0.75 | ||
30 | + | ||
31 | + rgb = numpy.array(colorsys.hsv_to_rgb(h, s, v)) | ||
32 | + return rgb * 255 | ||
33 | + | ||
20 | #generate a 2D color class map using a stack of binary class images | 34 | #generate a 2D color class map using a stack of binary class images |
21 | #input: C is a C x Y x X binary image | 35 | #input: C is a C x Y x X binary image |
22 | #output: an RGB color image with a unique color for each class | 36 | #output: an RGB color image with a unique color for each class |
@@ -33,8 +47,7 @@ def class2color(C): | @@ -33,8 +47,7 @@ def class2color(C): | ||
33 | 47 | ||
34 | #for each class | 48 | #for each class |
35 | for c in range(0, nc): | 49 | for c in range(0, nc): |
36 | - hsv = (c * 1.0 / nc, 1, 1) | ||
37 | - color = numpy.asarray(colorsys.hsv_to_rgb(hsv[0], hsv[1], hsv[2])) * 255 | 50 | + color = qualcolor(c, nc) |
38 | RGB[C[c, ...], :] = color | 51 | RGB[C[c, ...], :] = color |
39 | 52 | ||
40 | return RGB | 53 | return RGB |
@@ -148,22 +161,22 @@ def random_mask(M, n): | @@ -148,22 +161,22 @@ def random_mask(M, n): | ||
148 | 161 | ||
149 | def envi_batch_predict(E, C, batch=10000): | 162 | def envi_batch_predict(E, C, batch=10000): |
150 | 163 | ||
151 | - Fv = E.loadbatch(batch).transpose() | 164 | + Fv = E.loadbatch(batch) |
152 | i = 0 | 165 | i = 0 |
153 | Tv = [] | 166 | Tv = [] |
154 | plt.ion() | 167 | plt.ion() |
155 | bar = progressbar.ProgressBar(max_value=numpy.count_nonzero(E.mask)) | 168 | bar = progressbar.ProgressBar(max_value=numpy.count_nonzero(E.mask)) |
156 | while not Fv == []: | 169 | while not Fv == []: |
157 | if i == 0: | 170 | if i == 0: |
158 | - Tv = C.predict(Fv) | 171 | + Tv = C.predict(Fv.transpose()) |
159 | else: | 172 | else: |
160 | - Tv = numpy.concatenate((Tv, C.predict(Fv).transpose()), 0) | 173 | + Tv = numpy.concatenate((Tv, C.predict(Fv.transpose()).transpose()), 0) |
161 | tempmask = E.batchmask() | 174 | tempmask = E.batchmask() |
162 | Lv = spectral.unsift2(Tv, tempmask) | 175 | Lv = spectral.unsift2(Tv, tempmask) |
163 | Cv = label2class(Lv.squeeze(), background=0) | 176 | Cv = label2class(Lv.squeeze(), background=0) |
164 | RGB = class2color(Cv) | 177 | RGB = class2color(Cv) |
165 | plt.imshow(RGB) | 178 | plt.imshow(RGB) |
166 | plt.pause(0.05) | 179 | plt.pause(0.05) |
167 | - Fv = E.loadbatch(batch).transpose() | 180 | + Fv = E.loadbatch(batch) |
168 | i = i + 1 | 181 | i = i + 1 |
169 | bar.update(len(Tv)) | 182 | bar.update(len(Tv)) |
170 | \ No newline at end of file | 183 | \ No newline at end of file |