python列表可以将多维数组作为其元素吗?

时间:2013-08-21 11:08:20

标签: python python-2.7 numpy

我正在尝试使用python进行图像处理。

我尝试创建一个包含numpy.ndarrays的列表。

我的代码看起来像这样,

def Minimum_Close(Shade_Corrected_Image, Size):

    uint32_Shade_Corrected_Image = pymorph.to_int32(Shade_Corrected_Image)
    Angles = []

    [Row, Column] = Shade_Corrected_Image.shape


    Angles = [i*15 for i in range(12)] 



    Image_Close = [0 for x in range(len(Angles))]
    Image_Closing = numpy.zeros((Row, Column))

    for s in range(len(Angles)):

        Struct_Element = pymorph.seline(Size, Angles[s])
        Image_Closing = pymorph.close(uint32_Shade_Corrected_Image,Struct_Element )
        Image_Close[s] = Image_Closing

    Min_Close_Image = numpy.zeros(Shade_Corrected_Image.shape)

    temp_array = [][]
    Temp_Cell = numpy.zeros((Row, Column))

    for r in range (1, Row):
        for c in range(1,Column):
            for Cell in Image_Close:

                Temp_Cell = Image_Close[Cell]

                temp_array[Cell] = Temp_Cell[r][c]

            Min_Close_Image[r][c] = min(temp_array)    



    Min_Close_Image = Min_Close_Image - Shade_Corrected_Image    

    return Min_Close_Image

运行此代码时出现错误:

Temp_Cell = Image_Close[Cell]

TypeError: only integer arrays with one element can be converted to an index

如何创建一个包含不同多维数组然后遍历它的数据结构?

2 个答案:

答案 0 :(得分:1)

当你使用numpy时,不需要制作数组列表。

我建议重写整个函数:

def Minimum_Close(shade_corrected_image, size):

    uint32_shade_corrected_image = pymorph.to_int32(shade_corrected_image)

    angles = np.arange(12) * 15

    def pymorph_op(angle):
        struct_element = pymorph.seline(size, angle)
        return pymorph.close(uint32_shade_corrected_image, struct_element)

    image_close = np.dstack(pymorph_op(a) for a in angles)

    min_close_image = np.min(image_close, axis=-1) - shade_corrected_image

    return min_close_image

我降低了有根据的变量名称,以便它们不再以类的形式突出显示。

答案 1 :(得分:0)

怎么样:

for cnt,Cell in enumerate(Image_Close):
  Temp_Cell = Image_Close[cnt]