简体   繁体   English

Python中3D数组的最大值

[英]Max value of a 3d array in python

I want to find the max value of a 3d array in python. 我想在python中找到3d数组的最大值。 I tried 我试过了

image_file1 = open("lena256x256.bmp","rb")
img_i = PIL.Image.open(image_file1)
pix = numpy.array(img_i);
maxval= max(pix)

but i am getting an error 但我遇到一个错误

 File "test.py", line 31, in <module>
    maxval= max(pix)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I cannot catch my mistake here, please help me. 我在这里找不到我的错误,请帮助我。

You are using the builtin max function that does not understand multidimensional NumPy arrays. 您正在使用无法理解多维NumPy数组的内置max函数。 You must instead use one of: 您必须改为使用以下之一:

These are also faster than the builtin in the case of 1D NumPy arrays. 这些也比一维NumPy数组的内置速度更快。

Max期望使用单个值,错误消息应该非常清楚,您想使用amax代替。

maxval = numpy.amax(pix)

In accordance to what georgesl wrote, you can use flat to get an iterator for the array and then do something like 根据georgesl编写的内容,您可以使用flat获取数组的迭代器,然后执行类似的操作

m = reduce(max, ar.flat)

Edit: removed the lambda , the default max should be OK. 编辑:删除了lambda ,默认max应为OK。

The np.max function works for vectors, not matrices (or along an axis). np.max函数适用于向量,不适用于矩阵(或沿轴)。 To have the max element a multi-dimensionnal array, you can use flatten() : maxval= pp.max( pix.flatten() ) 要使max元素具有多维数组,可以使用flatten()maxval= pp.max( pix.flatten() )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM