首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python中的数组/图像插值

python中的数组/图像插值
EN

Stack Overflow用户
提问于 2012-11-28 22:59:57
回答 3查看 847关注 0票数 3

我一年中的每一天都有365个2d numpy阵列,显示的图像如下:

我把它们都堆成了一个3Dnumpy数组。我想要搜索前7天,或未来7天(前7层,下7层)以找到云以外的值,然后用该像素的其他可能值(相应像素在其他天/层中经历的值)替换云值。

我是python的新手,有点迷失方向。

有什么想法吗?

谢谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-12-31 20:35:49

我通过以下方式解决了这个问题:

代码语言:javascript
复制
interpdata = []
j = 0
for i in stack:
    try:
        temp = np.where( stack[j] == 50, stack[j-1], modis[j] )
        temp = np.where( temp == 50, stack[j+1], temp )
        temp = np.where( temp == 50, stack[j-2], temp )
        temp = np.where( temp == 50, stack[j+2], temp )
        temp = np.where( temp == 50, stack[j-3], temp )
        temp = np.where( temp == 50, stack[j+3], temp ) 
        temp = np.where( temp == 50, stack[j-4], temp )
        temp = np.where( temp == 50, stack[j+4], temp )
    except IndexError:
        print 'IndexError Passed'       
        pass
    else:
        pass
    interpdata [j, :, :] = temp
    j = j + 1   
票数 1
EN

Stack Overflow用户

发布于 2012-11-29 02:36:22

您实际上是在尝试为您的数组编写一个过滤器。

在你的例子中,该函数将接受一维数组,并返回最接近中间索引的元素,而不是cloud:

代码语言:javascript
复制
import numpy as np
from scipy.ndimage.filters import generic_filter

_cloud = -1

def findNearestNonCloud(elements):
    middleIndex = len(elements) / 2
    if elements[middleIndex] != _cloud:
        return elements[middleIndex] # middle value is not cloud

    nonCloudIndices, = np.where(elements != _cloud)
    if len(nonCloudIndices) == 0:
        return elements[middleIndex] # all values were cloud

    prevNonCloudIndex = np.where(nonCloudIndices < middleIndex, 
            nonCloudIndices, -1).max()
    nextNonCloudIndex = -np.where(nonCloudIndices > middleIndex, 
            -nonCloudIndices, 1).min()
    # -1 means no non-cloud index

    # pick index closest to middle index    
    if (abs(prevNonCloudIndex - middleIndex) 
            <= abs(nextNonCloudIndex - middleIndex)):
        return elements[prevNonCloudIndex]
    else:
        return elements[nextNonCloudIndex]

现在,您需要将此函数应用于您感兴趣的元素。要做到这一点,您需要一个掩码来表示您对特定元素感兴趣的其他元素。

代码语言:javascript
复制
from scipy.ndimage.filters import generic_filter

# creates 5 days worth of a 3x3 plot of land
input = np.ones((5, 3, 3)) * _cloud
input[0,:,:] = 10 # set first "image" to all be 10s
input[4,0,0] = 12 # uppper left corner of fourth image is set to 12
print "input data\n", input, "\n"

mask = (5, 1, 1)
# mask represents looking at the present day, 2 days in the future and 2 days in 
# the past for 5 days in total.

print "result\n", generic_filter(input, findNearestNonCloud, size=mask)
# second and third images should mirror first image, 
# except upper left corner of third image should be 12
票数 3
EN

Stack Overflow用户

发布于 2012-11-28 23:28:15

我认为你可以这样做:

代码语言:javascript
复制
data = somehow_get_your_3d_data() #indexed as [day_of_year,y,x]
for i,dat in enumerate(data):
    weeks2 = data[max(i-7,i):min(i+7,len(data)), ... ]
    new_value = get_new_value(weeks2) #get value from weeks2 here somehow
    dat[dat == cloud_value] = new_value
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13608029

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档