-
Notifications
You must be signed in to change notification settings - Fork 29
/
processNGB.py
194 lines (144 loc) · 5.21 KB
/
processNGB.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import sys
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import numpy as numpy
from PIL import Image
import gc
# function for generating NIR imagery from NGB input files
def nir(imageInPath,imageOutPath):
img=Image.open(imageInPath)
imgN, imgG, imgB = img.split() #get channels
arrR = numpy.asarray(imgN).astype('float64')
arr_nir=arrR
fig=plt.figure()
fig.set_frameon(False)
ax=fig.add_subplot(111)
ax.set_axis_off()
ax.patch.set_alpha(0.0)
nir_plot = ax.imshow(arr_nir, cmap=plt.cm.gist_gray, interpolation="nearest")
#fig.colorbar(nir_plot)
fig.savefig(imageOutPath)
#needed to clear memory if used to process many frames ...
fig.clf()
plt.close()
gc.collect()
# function for generating NDVI imagery from NGB or NBG input files
def ndvi(imageInPath,imageOutPath,vmin,vmax,histogramOption):
img=Image.open(imageInPath)
imgR, imgB, imgG = img.split() #get channels from NGB
#imgR, imgG, imgB = img.split() #get channels from NBG
arrR = numpy.asarray(imgR).astype('float64')
arrG = numpy.asarray(imgG).astype('float64')
arrB = numpy.asarray(imgB).astype('float64')
num=(arrR - arrB)
denom=(arrR + arrB)
arr_ndvi=num/denom
if arr_ndvi.max()>0:
img_w,img_h=img.size
dpi=600. #need this to be floating point!
fig_w=img_w/dpi
fig_h=img_h/dpi
fig=plt.figure(figsize=(fig_w,fig_h),dpi=dpi)
fig.set_frameon(False)
ax_rect = [0.0, #left
0.0, #bottom
1.0, #width
1.0] #height
ax = fig.add_axes(ax_rect)
ax.yaxis.set_ticklabels([])
ax.xaxis.set_ticklabels([])
ax.set_axis_off()
ax.axes.get_yaxis().set_visible(False)
ax.patch.set_alpha(0.0)
axes_img = ax.imshow(arr_ndvi,
cmap=plt.cm.spectral,
vmin = vmin,
vmax = vmax,
aspect = 'equal',
interpolation="nearest"
)
# axes_img = ax.imshow(arr_ndvi,
# cmap=plt.cm.spectral,
# aspect = 'equal',
# interpolation="nearest"
# )
if histogramOption==1:
#plot the Red histogram
x=arrR.ravel()
a = plt.axes([.05,.7,.18,.18], axisbg='y')
bins=numpy.arange(0,255,8)
n, bins, patches = plt.hist(x, bins, normed=1,linewidth=.2)
plt.setp(patches, 'facecolor', 'r', 'alpha', 0.75)
plt.setp(a,xticks=[0,120,255],yticks=[])
plt.setp(a,xticks=[],yticks=[])
plt.xticks(fontsize=2)
#plot the Blue histogram
x=arrB.ravel()
a = plt.axes([.05,.4,.18,.18], axisbg='y')
bins=numpy.arange(0,255,8)
n, bins, patches = plt.hist(x, bins, normed=1,linewidth=.2)
plt.setp(patches, 'facecolor', 'b', 'alpha', 0.75)
plt.setp(a,xticks=[0,120,255],yticks=[])
plt.setp(a,xticks=[],yticks=[])
plt.xticks(fontsize=2)
#plot the NDVI histogram
x=arr_ndvi.ravel()
a = plt.axes([.05,.1,.18,.18], axisbg='y')
bins=numpy.arange(-1,1,.01)
n, bins, patches = plt.hist(x, bins, normed=1,linewidth=.2)
plt.setp(patches, 'facecolor', 'w', 'alpha', 0.75)
plt.setp(a,xticks=[-1,0,1],yticks=[])
plt.setp(a,xticks=[],yticks=[])
plt.xticks(fontsize=2)
# Add colorbar
#make an axis for colorbar
cax = fig.add_axes([0.8,0.05,0.05,0.85]) #left, bottom, width, height
cbar = fig.colorbar(axes_img, cax=cax) #this resizes the axis
cbar.ax.tick_params(labelsize=2) #this changes the font size on the axis
#position of the colorbar
#cbar.ax.yaxis.set_ticks_position('left')
#color of the colorbar text
#cbytick_obj = plt.getp(cbar.ax.axes, 'yticklabels') #tricky
#plt.setp(cbytick_obj, color='r')
fig.savefig(imageOutPath,
dpi=dpi,
bbox_inches='tight',
pad_inches=0.0,
)
#plt.show() #show the plot after saving
fig.clf()
plt.close()
gc.collect()
###### testing the code #######
indir = str(sys.argv[1]) #the input directory
outdir = str(sys.argv[2]) #the output directory
VMIN = float(sys.argv[3]) #minimum value for the colorbar
VMAX = float(sys.argv[4]) #max value for the colorbar
histogramOption = int(sys.argv[5]) #whether to include histograms of R, B, and NDVI -- 0: no histogram, 1: include histogram
import glob
indir=indir+'*' #add all files in the inputdirectory to the list
# get the files from the directory, and sort them in case we're making a movie
files= sorted(glob.glob(indir))
print "Detected ",len(files), "files in", str(indir)
#process all the files
index=0
for f in files:
index=index+1 #update the index
inFilePath=f
inFileName= os.path.basename(f)
print "File ",index," of ",len(files),":",inFilePath
outFileName='ndvi_'+inFileName
outFilePath= os.path.join(outdir,outFileName)
ndvi(inFilePath,outFilePath,VMIN,VMAX,histogramOption)
print "---->", outFilePath