Project 3 : Satellite Image Data Analysis using numpy and matplotlib

Data Processing and Visulisation with Python

Loading the libraries we need: numpy, matplotlib

import numpy as np
import matplotlib.pyplot as plt

Creating a numpy array from an image file

Lets choose a satellite image file as an ndarray and display its type

photo_data = plt.imread('sd-3layers.jpg')
type(photo_data)

在这里插入图片描述

Let’s see what is in this image

plt.figure(figsize=(10,10))
plt.imshow(photo_data)

在这里插入图片描述

photo_data.shape

在这里插入图片描述

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.

RGB Color Mapping in the Photo:


  • RED pixel indicates Altitude

  • BLUE pixel indicates Aspect

  • GREEN pixel indicates Slope


The higher values denote higher altitude, aspect and slope. ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210119142902454.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ3ODA1NDgz,size_16,color_FFFFFF,t_70)
photo_data.size
3725*4797*3
photo_data.min(),photo_data.max()
photo_data.mean()

在这里插入图片描述

Pixel on the 150th Row and 250th Column

photo_data[150,250]
photo_data[150,250,1]

在这里插入图片描述

Set a Pixel to All Zeros

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.

photo_revise = photo_data.copy()

photo_revise[150,250] = 0

fig,ax = plt.subplots(1,2,figsize=(20,10))
ax[0].imshow(photo_data),ax[1].imshow(photo_revise)

在这里插入图片描述

photo_revise = photo_data.copy()

photo_revise[150,250] = 0
photo_revise[200:800,:,:] = 255

fig,ax = plt.subplots(1,2,figsize=(20,10))
ax[0].imshow(photo_data),ax[1].imshow(photo_revise)

在这里插入图片描述

photo_revise = photo_data.copy()

photo_revise[150,250] = 0
photo_revise[200:800,:,:] = 0

fig,ax = plt.subplots(1,2,figsize=(20,10))
ax[0].imshow(photo_data),ax[1].imshow(photo_revise)

在这里插入图片描述

Changing Colors in a Range

We can also use a range to change the pixel values. As an example, let’s set the green layer for rows 200 to 800 to full intensity.

# set all three layers to full intensity


# set all three layers to 0

Pick all Pixels with Low Values

low_filter = photo_data<100

low_filter.shape,photo_data.shape

在这里插入图片描述

Filtering Out Low Values

Whenever the low_value_filter is True, set value to 0.

photo_revise = photo_data.copy()

low_filter = photo_data<200
photo_revise[low_filter]=0

fig,ax = plt.subplots(1,2,figsize=(20,10))
ax[0].imshow(photo_data),ax[1].imshow(photo_revise)

在这里插入图片描述

More Row and Column Operations

You can design complex pattens by making cols a function of rows or vice-versa. Here we try a linear relationship between rows and columns.

photo_revise = photo_data.copy()

length = len(photo_revise)
length
photo_revise[:length,:length]=0

fig,ax = plt.subplots(1,2,figsize=(20,10))
ax[0].imshow(photo_data),ax[1].imshow(photo_revise)

在这里插入图片描述

Masking Images

Now let us try something even cooler…a mask that is in shape of a circular disc.
在这里插入图片描述

photo_revise = photo_data.copy()

p_row,p_col,p_layer = photo_revise.shape
X,Y = np.ogrid[:p_row,:p_col]
X

Y
photo_revise.shape

c_row = p_row/2 
c_col = p_col/2
dist_c = (X-c_row)**2 + (Y-c_col)**2
r = (p_row/2)**2

mask = dist_c > r


print(mask[1200:1300,1200:1300])

photo_revise[mask] = 0

fig,ax = plt.subplots(1,2,figsize=(20,10))
ax[0].imshow(photo_data),ax[1].imshow(photo_revise)

在这里插入图片描述

Further Masking

You can further improve the mask. For example, just get the upper half of the disc.

upper_half = X < c_row

mix_mask = upper_half | mask

photo_revise = photo_data.copy()

photo_revise[mix_mask] = 0

fig,ax = plt.subplots(1,2,figsize=(20,10))
ax[0].imshow(photo_data),ax[1].imshow(photo_revise)

在这里插入图片描述

Further Processing of our Satellite Imagery

Processing of RED Pixels

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.

photo_revise = photo_data.copy()

mask = photo_revise[:,:,0] < 150
photo_revise[mask] = 0

fig,ax = plt.subplots(1,2,figsize=(20,10))
ax[0].imshow(photo_data),ax[1].imshow(photo_revise)

在这里插入图片描述

photo_revise = photo_data.copy()

mask = photo_revise[:,:,1] < 150
photo_revise[mask] = 0

fig,ax = plt.subplots(1,2,figsize=(20,10))
ax[0].imshow(photo_data),ax[1].imshow(photo_revise)

在这里插入图片描述

Detecting Highly-GREEN Pixels

photo_revise = photo_data.copy()

mask = photo_revise[:,:,2] < 150
photo_revise[mask] = 0

fig,ax = plt.subplots(1,2,figsize=(20,10))
ax[0].imshow(photo_data),ax[1].imshow(photo_revise)

在这里插入图片描述

Composite mask that takes thresholds on all three layers: RED, GREEN, BLUE

photo_revise = photo_data.copy()

r_mask = photo_revise[:,:,0] < 150
g_mask = photo_revise[:,:,1] > 50
b_mask = photo_revise[:,:,2] < 100

photo_revise[r_mask|g_mask|b_mask] = 0

fig,ax = plt.subplots(1,2,figsize=(20,10))
ax[0].imshow(photo_data),ax[1].imshow(photo_revise)

在这里插入图片描述

photo_revise = photo_data.copy()

r_mask = photo_revise[:,:,0] < 150
g_mask = photo_revise[:,:,1] > 50
b_mask = photo_revise[:,:,2] < 100

photo_revise[r_mask&g_mask&b_mask] = 0

fig,ax = plt.subplots(1,2,figsize=(20,10))
ax[0].imshow(photo_data),ax[1].imshow(photo_revise)

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值