-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSatellite Image Analysis using numpy.py
351 lines (207 loc) · 8.44 KB
/
Satellite Image Analysis using numpy.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/env python
# coding: utf-8
# <p style="font-family: Arial; font-size:3.75em;color:purple; font-style:bold"><br>
# Satellite Image Data <br><br><br>Analysis using numpy</p>
#
# <p style="font-family: Arial; font-size:1.75em;color:#2462C0; font-style:bold"><br>Data Source: Satellite Image from WIFIRE Project</p>
#
#
# WIFIRE is an integrated system for wildfire analysis, with specific regard to changing urban dynamics and climate. The system integrates networked observations such as heterogeneous satellite data and real-time remote sensor data, with computational techniques in signal processing, visualization, modeling, and data assimilation to provide a scalable method to monitor such phenomena as weather patterns that can help predict a wildfire's rate of spread. You can read more about WIFIRE at: https://wifire.ucsd.edu/
#
# In this example, we will analyze a sample satellite image dataset from WIFIRE using the numpy Library.
#
# <p style="font-family: Arial; font-size:1.75em;color:#2462C0; font-style:bold">Loading the libraries we need: numpy, scipy, matplotlib</p>
# In[1]:
get_ipython().run_line_magic('matplotlib', 'inline')
import numpy as np
from scipy import misc
import matplotlib.pyplot as plt
import imageio
# <p style="font-family: Arial; font-size:1.75em;color:#2462C0; font-style:bold">
# Creating a numpy array from an image file:</p>
#
# <br>
# Lets choose a WIFIRE satellite image file as an ndarray and display its type.
#
# In[2]:
from skimage import data
photo_data = imageio.imread('./wifire/sd-3layers.jpg')
type(photo_data)
# Let's see what is in this image.
# In[3]:
plt.figure(figsize=(15,15))
plt.imshow (photo_data)
# In[4]:
photo_data.shape
#print(photo_data)
# The shape of the ndarray show that it is a three layered matrix. The first two numbers here are length and width, and the third number (i.e. 3) is for three layers: Red, Green and Blue.
#
# <p style="font-family: Arial; font-size:1.75em;color:#2462C0; font-style:bold">
# RGB Color Mapping in the Photo:</p> <br>
# <ul>
# <li><p style="font-family: Arial; font-size:1.75em;color:red; font-style:bold">
# RED pixel indicates Altitude</p>
# <li><p style="font-family: Arial; font-size:1.75em;color:blue; font-style:bold">
# BLUE pixel indicates Aspect
# </p>
# <li><p style="font-family: Arial; font-size:1.75em;color:green; font-style:bold">
# GREEN pixel indicates Slope
# </p>
# </ul>
# <br>
# The higher values denote higher altitude, aspect and slope.
#
# In[5]:
photo_data.size
# In[6]:
photo_data.min(), photo_data.max()
# In[7]:
photo_data.mean()
# <p style="font-family: Arial; font-size:1.75em;color:#2462C0; font-style:bold"><br>
#
# Pixel on the 150th Row and 250th Column</p>
# In[8]:
photo_data[150, 250]#150=r,250=col,17, 35, 255=rgb
# In[9]:
photo_data[150, 250, 1]#0,1,2 means r,g,b
# <p style="font-family: Arial; font-size:1.75em;color:#2462C0; font-style:bold"><br>
# Set a Pixel to All Zeros</p>
# <br/>
# We can set all three layer in a pixel as once by assigning zero globally to that (row,column) pairing. However, setting one pixel to zero is not noticeable.
# In[10]:
#photo_data = misc.imread('./wifire/sd-3layers.jpg')
photo_data[150, 250] = 0
plt.figure(figsize=(10,10))
plt.imshow(photo_data)
# <p style="font-family: Arial; font-size:1.75em;color:#2462C0; font-style:bold"><br>
# Changing colors in a Range<p/>
# <br/>
# We can also use a range to change the pixel values. As an example, let's set the green layer for rows 200 t0 800 to full intensity.
# In[11]:
photo_data = imageio.imread('./wifire/sd-3layers.jpg')
photo_data[200:800, : ,1] = 255
plt.figure(figsize=(10,10))
plt.imshow(photo_data)
# In[12]:
photo_data = imageio.imread('./wifire/sd-3layers.jpg')
photo_data[200:800, :] = 255
plt.figure(figsize=(10,10))
plt.imshow(photo_data)
# In[13]:
photo_data = imageio.imread('./wifire/sd-3layers.jpg')
photo_data[200:800, :] = 0
plt.figure(figsize=(10,10))
plt.imshow(photo_data)
# <p style="font-family: Arial; font-size:1.75em;color:#2462C0; font-style:bold"><br>
# Pick all Pixels with Low Values</p>
# In[14]:
photo_data = imageio.imread('./wifire/sd-3layers.jpg')
print("Shape of photo_data:", photo_data.shape)
low_value_filter = photo_data < 200
print("Shape of low_value_filter:", low_value_filter.shape)
# <p style="font-family: Arial; font-size:1.75em;color:#2462C0; font-style:bold">
# Filtering Out Low Values</p><br/>
# Whenever the low_value_filter is True, set value to 0.
# In[15]:
#import random
plt.figure(figsize=(10,10))
plt.imshow(photo_data)
photo_data[low_value_filter] = 0
plt.figure(figsize=(10,10))
plt.imshow(photo_data)
# <p style="font-family: Arial; font-size:1.75em;color:#2462C0; font-style:bold">
# More Row and Column Operations</p><br>
# You can design complex patters by making cols a function of rows or vice-versa. Here we try a linear relationship between rows and columns.
# In[16]:
rows_range = np.arange(len(photo_data))
cols_range = rows_range
print(type(rows_range))
print(rows_range)
# In[17]:
photo_data[rows_range, cols_range] = 255
# In[18]:
plt.figure(figsize=(15,15))
plt.imshow(photo_data)
# <p style="font-family: Arial; font-size:1.75em;color:#2462C0; font-style:bold"><br>
# Masking Images</p>
# <br>Now let us try something even cooler...a mask that is in shape of a circular disc.
# <img src="./1494532821.png" align="left" style="width:550px;height:360px;"/>
# In[19]:
total_rows, total_cols, total_layers = photo_data.shape
print("photo_data = ", photo_data.shape)
X, Y = np.ogrid[:total_rows, :total_cols]
print("X = ", X.shape, " and Y = ", Y.shape)
print(Y)
# In[20]:
center_row, center_col = total_rows / 2, total_cols / 2
print("center_row = ", center_row, "AND center_col = ", center_col)
print(X - center_row)
print(Y - center_col)
dist_from_center = (X - center_row)**2 + (Y - center_col)**2
print(dist_from_center)
radius = (total_rows / 2)**2
print("Radius = ", radius)
circular_mask = (dist_from_center > radius)
print(circular_mask)
print(circular_mask[1500:1700,2000:2200])
# In[21]:
photo_data = imageio.imread('./wifire/sd-3layers.jpg')
photo_data[circular_mask] = 0
plt.figure(figsize=(15,15))
plt.imshow(photo_data)
# <p style="font-family: Arial; font-size:1.75em;color:#2462C0; font-style:bold">
# Further Masking</p>
# <br/>You can further improve the mask, for example just get upper half disc.
# In[22]:
import numpy as np
X, Y = np.ogrid[:total_rows, :total_cols]
half_upper = X < center_row # this line generates a mask for all rows above the center
half_upper_mask = np.logical_and(half_upper, circular_mask)
# In[28]:
photo_data = imageio.imread('./wifire/sd-3layers.jpg')
photo_data[half_upper_mask] = 255
#photo_data[half_upper_mask] = random.randint(200,255)
plt.figure(figsize=(15,15))
plt.imshow(photo_data)
# <p style="font-family: Arial; font-size:2.75em;color:purple; font-style:bold"><br>
# Further Processing of our Satellite Imagery </p>
#
# <p style="font-family: Arial; font-size:1.75em;color:#2462C0; font-style:bold">
# Processing of RED Pixels</p>
#
# Remember that red pixels tell us about the height. Let us try to highlight all the high altitude areas. We will do this by detecting high intensity RED Pixels and muting down other areas.
# In[29]:
photo_data = imageio.imread('./wifire/sd-3layers.jpg')
red_mask = photo_data[:, : ,0] < 150
photo_data[red_mask] = 0
plt.figure(figsize=(15,15))
plt.imshow(photo_data)
# <p style="font-family: Arial; font-size:1.75em;color:#2462C0; font-style:bold"><br>
# Detecting Highl-GREEN Pixels</p>
# In[30]:
photo_data = imageio.imread('./wifire/sd-3layers.jpg')
green_mask = photo_data[:, : ,1] < 150
photo_data[green_mask] = 0
plt.figure(figsize=(15,15))
plt.imshow(photo_data)
# <p style="font-family: Arial; font-size:1.75em;color:#2462C0; font-style:bold"><br>
# Detecting Highly-BLUE Pixels</p>
# In[31]:
photo_data = imageio.imread('./wifire/sd-3layers.jpg')
blue_mask = photo_data[:, : ,2] < 150
photo_data[blue_mask] = 0
plt.figure(figsize=(15,15))
plt.imshow(photo_data)
# <p style="font-family: Arial; font-size:1.75em;color:#2462C0; font-style:bold"><br>
#
# Composite mask that takes thresholds on all three layers: RED, GREEN, BLUE</p>
# In[34]:
photo_data = imageio.imread('./wifire/sd-3layers.jpg')
red_mask = photo_data[:, : ,0] < 150
green_mask = photo_data[:, : ,1] > 100
blue_mask = photo_data[:, : ,2] < 100
final_mask = np.logical_and(red_mask, green_mask, blue_mask)
photo_data[final_mask] = 0
plt.figure(figsize=(15,15))
plt.imshow(photo_data)
# In[ ]: