Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python数组处理:如何逐像素生成新值_Python_Arrays_Raster - Fatal编程技术网

python数组处理:如何逐像素生成新值

python数组处理:如何逐像素生成新值,python,arrays,raster,Python,Arrays,Raster,我的问题是python中的数组操作。我想做的是用python分析一个多波段光栅,并根据结果输出另一个光栅。 真实案例示例:我有一个具有3个波段的光栅,我想看看同一像素的不同波段值是按递增顺序还是递减顺序(从波段1到波段3)。如果值的顺序是递增的,则新的值(例如“1”)将分配给新输出光栅中的特定像素(也可以是同一光栅中的新波段)。如果值按降序排列,则将分配值“2”。我已经定义了函数并读取了数组值,但我一直在为每个像素递归地构建列表,并为新光栅分配新值 我将发布到目前为止的代码: import ar

我的问题是python中的数组操作。我想做的是用python分析一个多波段光栅,并根据结果输出另一个光栅。 真实案例示例:我有一个具有3个波段的光栅,我想看看同一像素的不同波段值是按递增顺序还是递减顺序(从波段1到波段3)。如果值的顺序是递增的,则新的值(例如“1”)将分配给新输出光栅中的特定像素(也可以是同一光栅中的新波段)。如果值按降序排列,则将分配值“2”。我已经定义了函数并读取了数组值,但我一直在为每个像素递归地构建列表,并为新光栅分配新值

我将发布到目前为止的代码:

import arcpy
import numpy as np

arcpy.env.workspace = r"\\workspace"

def decrease(A):
    return all(A[i] >= A[i+1] for i in range(len(A)-1)) # function to check if a list has decreasing values
def increase(A):
    return all(A[i] <= A[i+1] for i in range(len(A)-1))  # function to check if a list has increasing values
def no0(A,val):
    return[value for value in A if value !=val]   # function to check if a list has 0 values, and if so, strip them

myArray = arcpy.RasterToNumpyArray(r"raster_3bands.img")  # my 3 bands raster

print myArray.shape  # to see how many dimensions the array has
for z in range(3): # where z is the number of bands, in my case 3
    for y in range(6): #the number of columns, i.e. the vertical axis, in my case 6
        for x in range(6): #the number of rows,i.e. the horizontal axis, in my case 6.
            a = myArray[z,y,x] # get all the values for the 3 bands
            print "The value for axes:  ", z,y,x, " is: ",a

首先要考虑的是代码的结构。在使用numpy时(尤其是在这种情况下),很少需要应用循环。可以简单地沿轴应用矢量化操作

在这种情况下,您可以使用阵列广播来查找哪些像素集在增加和减少

increasing_pixels = (myArray[0] <= myArray[1]) * (myArray[1] <= myArray[2])
这些阵列可以用作光栅的遮罩,允许您识别要更改值的像素。下面是基于原始三个波段中像素值的增加或减少创建新波段的示例

new_band = np.zeros_like(myArray[0])

new_band[increasing_pixels==True] = 1
new_band[decreasing_pixels==True] = 2
编辑:

对于任意数量的频带,上述比较可替换为:

increasing_pixels = np.product([(myArray[i] <= myArray[i+1]) for i in range(nbands-1)],axis=0)
decreasing_pixels = np.product([(myArray[i] >= myArray[i+1]) for i in range(nbands-1)],axis=0)
增加像素=np.product([(myArray[i]=myArray[i+1]),用于范围内的i(nbands-1)],轴=0)

Hi Bogdan,欢迎来到SO!非常感谢您的回答,这是一个更好的方法!还有一个问题:如果一个光栅有更多的波段,而不仅仅是三个波段,我如何循环n个波段来计算增加的像素?我试图将myArray集成到我的increase()函数中,但出现了错误…再次非常感谢。然而,我通过用
增加像素=np.product([(myArray[I]=并很抱歉再次返回,但我错过了一些内容。在这种方法中,是否可以跳过0值?我的意思是,如果band1==1、band2==0和band3==3,我仍然希望增加_像素的值为真。您可以将循环版本中的表达式更改为读取
增加_像素=np.product([((myArray[I]
decreasing_pixels = (myArray[0] >= myArray[1]) * (myArray[1] >= myArray[2])
new_band = np.zeros_like(myArray[0])

new_band[increasing_pixels==True] = 1
new_band[decreasing_pixels==True] = 2
increasing_pixels = np.product([(myArray[i] <= myArray[i+1]) for i in range(nbands-1)],axis=0)
decreasing_pixels = np.product([(myArray[i] >= myArray[i+1]) for i in range(nbands-1)],axis=0)