对图像进行平均,然后内插出nans

时间:2019-04-22 18:26:39

标签: python numpy image-processing scipy

我有一堆卫星图像,这些图像都是在很短的时间内拍摄的,但是都没有足够的覆盖我的学习区域。所以我想:

  1. 将它们平均在一起-如果该位置至少还有一个有效数据点,则忽略nans
  2. 在数组上进行插值以填充空洞-除非没有一个具有该位置n个像素的有效数据点。如果是这种情况,我将其保留为nan,并在最终处理中将其忽略。

到目前为止,我有:

initial_satellite_data.shape
>>> (5, 500, 500, 2)
# This is five images, 500x500 pixels, and two bands per image.

# need to convert this to float in order to do the next step
sat_data_float = initial_satellite_data.astype(np.float32)

# 255 values signify areas without data, so converting that to nan
sat_data_float[sat_data_float==255]=np.nan

# get the average across the different images
average_image = np.nanmean(np_imageset, axis=0)

average_image.shape
>>> (500, 500, 2)

但是我不清楚如何在数据上插值以填补仍然包含nan数据的空白,以及我的告诫:如果像素没有n像素的有效数据,我不想插值它。

作为一个大致如何在此处显示数据的示例,是一些代码生成了我确实想要插值的噪声和不想在单波段图像上插值的丢失块的类型:< / p>

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 1, 500)
y = x[:, None]
average_image = x + y

# Destroy some values - this is about the amount of missing 
# data I have that I want to interpolate across
mask = np.random.random(average_image.shape) > 0.98
average_image[mask] = np.nan

# Now destroy a whole corner that might be missing and it 
# doesn't make sense to interpolate into this mass of missing data
average_image[0:80, 0:80] = np.nan

# Here should be some interpolation process, I've been able to interpolate in 1D across the row and that actually works well 
# enough to get rid of the missing individual pixels but I can't do a sanity check to make sure it isn't interpolating a pixel 50 pixels away from real data.
interpolated_image = average_image

fig, (ax0, ax1) = plt.subplots(1, 2)

ax0.imshow(average_image, cmap='gray', interpolation='nearest')
ax0.set_title('Input image')
ax1.imshow(interpolated_image, cmap='gray', interpolation='nearest')
ax1.set_title('Interpolated data')
plt.show()

matplotlib output

0 个答案:

没有答案