-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImagetoSpectrumHistogram.py
More file actions
179 lines (114 loc) · 4.98 KB
/
ImagetoSpectrumHistogram.py
File metadata and controls
179 lines (114 loc) · 4.98 KB
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
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 20 02:30:30 2020
@author: cosmi
"""
import warnings
warnings.filterwarnings('ignore')
from matplotlib import pyplot as plt # For image viewing
import scipy
import numpy as np
from scipy import misc
from matplotlib import colors
from matplotlib import ticker
from matplotlib.colors import LinearSegmentedColormap
#for batch in a test folder
#for image in os.listdir('./test/'):
# img = Image.open('./test/'+image)
file0 = 'DJI_0128.JPG'
img = misc.imread(file0)
ir = (img[:,:,0]).astype('float')
blue = (img[:,:,2]).astype('float')
# Create a numpy matrix of zeros to hold the calculated NDVI values for each pixel
ndvi = np.zeros(blue.size) # The NDVI image will be the same size as the input image
# Calculate NDVI
ndvi = np.true_divide(np.subtract(ir, blue), np.add(ir, blue))
# Display the results
output_name = 'InfraBlueNDVI3.jpg'
#a nice selection of grayscale colour palettes
cols1 = ['blue', 'green', 'yellow', 'red']
cols2 = ['gray', 'gray', 'red', 'yellow', 'green']
cols3 = ['gray', 'blue', 'green', 'yellow', 'red']
cols4 = ['black', 'gray', 'blue', 'green', 'yellow', 'red']
def create_colormap(args):
return LinearSegmentedColormap.from_list(name='custom1', colors=cols1)
#colour bar to match grayscale units
def create_colorbar(fig, image):
position = fig.add_axes([0.125, 0.19, 0.2, 0.05])
norm = colors.Normalize(vmin=-1., vmax=1.)
cbar = plt.colorbar(image,
cax=position,
orientation='horizontal',
norm=norm)
cbar.ax.tick_params(labelsize=6)
tick_locator = ticker.MaxNLocator(nbins=3)
cbar.locator = tick_locator
cbar.update_ticks()
cbar.set_label("NDVI", fontsize=10, x=0.5, y=0.5, labelpad=-25)
fig, ax = plt.subplots()
image = ax.imshow(ndvi, cmap=create_colormap(colors))
plt.axis('off')
# plt.show()
#%SAVI - Soil Reflectance Leveraged
#The SAVI is structured similar to the NDVI but with the addition of a
#“soil reflectance correction factor” L.
#L is a constant (related to the slope of the soil-line in a feature-space plot)
#Hence the value of L varies by the amount or cover of green vegetation: in very high vegetation regions,
#L=0; and in areas with no green vegetation, L=1. Generally, an L=0.5 works
#well in most situations (i.e. mixed vegetation cover)
#So 0.5 (half) is the default value used. When L=0, then SAVI = NDVI.
#SAVI = [((R-B)*(1+L))./(R+B+L)];
L=0.5;
one = 1;
rplusb = np.add(ir, blue)
rminusb = np.subtract(ir, blue)
oneplusL = np.add(one, L)
savi = np.zeros(blue.size) # The NDVI image will be the same size as the input image
savi = np.true_divide(np.multiply(rminusb, oneplusL), np.add(rplusb, L))
#to use image color data in gaussian kernal density function
#we need to flatten the data from a 2D pixel array to 1D with length corresponding to elements (Warning! this can take some time)
#the fastest way to do this is to use the python library-level function ravel, which reshapes the array and returns its view.
# Ravel will often be many times faster than the similar function flatten as no computer memory needs to be copied and the original array is not affected.
# see reference to this at pgs 42-43 @ Numerical Python: A Practical Techniques Approach for Industry By Robert Johansson
#x = np.ndarray.ravel(blue)
#y = np.ndarray.ravel(ir)
x = np.ravel(img[:,:,2]) # blue channel array
y = np.ravel(img[:,:,0]) # nir channel array
plot1 = plt.figure(1)
#compare red and blue pixel data
nbins = 20
plt.hexbin(x, y, gridsize=nbins, cmap=plt.cm.jet)
plt.xlabel('Blue Reflectance')
plt.ylabel('NIR Reflectance')
# Add a title
plt.title('NIR vs Blue Spectral Data')
# Evaluate a gaussian kde on a regular grid of nbins x nbins over data extents
data = [x,y]
k = scipy.stats.gaussian_kde(data)
#k = kde.gaussian_kde(data)
xi, yi = np.mgrid[x.min():x.max():nbins*1j, y.min():y.max():nbins*1j]
zi = k(np.vstack([xi.flatten(), yi.flatten()]))
plot2 = plt.figure(2)
# plot a density
plt.title('Calculate Gaussian KDE')
plt.pcolormesh(xi, yi, zi.reshape(xi.shape), cmap=plt.cm.jet)
plot3 = plt.figure(3)
# add shading
plt.title('2D Density with shading')
plt.pcolormesh(xi, yi, zi.reshape(xi.shape), shading='gouraud', cmap=plt.cm.jet)
plot4 = plt.figure(4)
# contour
plt.title('Contour')
plt.pcolormesh(xi, yi, zi.reshape(xi.shape), shading='gouraud', cmap=plt.cm.jet)
plt.contour(xi, yi, zi.reshape(xi.shape) )
# threshold line
# Threshold; values above the threshold belong to another class as those below.
thresh = 0.0
clas = ndvi > thresh
#percentage of pixels selected
#imshow(clas, cmap=create_colormap(colors))
plot6 = plt.figure(6)
#size = 100*clas + 30*~clas
plt.pcolormesh(xi, yi, zi.reshape(xi.shape), shading='gouraud', cmap=plt.cm.jet)
plt.contour(xi,yi,clas)
plt.show()