[英]How can I get the x and y dimensions of a ndarray - Numpy / Python
I'm wondering if I can get the x and y dimensions of a ndarray separately. 我想知道我是否可以分别获得ndarray的x和y尺寸。 I know that I can use
ndarray.shape
to get a tuple representing the dimensions, but how can I separate this in x and y information? 我知道我可以使用
ndarray.shape
来获取表示维度的元组,但是如何在x和y信息中将其分开?
Thank you in advance. 先感谢您。
你可以使用元组解包。
y, x = a.shape
height, width = a.shape
Note, however, that ndarray
has matrix coordinates ( i,j
), which are opposite to image coordinates ( x,y
). 但是请注意,
ndarray
具有矩阵坐标( i,j
),它与图像坐标( x,y
)相反。 That is: 那是:
i, j = y, x # and not x, y
Also, Python tuples support indexing, so you can access separate dimensions like this: 此外,Python元组支持索引,因此您可以访问单独的维度,如下所示:
dims = a.shape
height = dims[0]
width = dims[1]
ndarray.shape()
will throw a TypeError: 'tuple' object is not callable.
ndarray.shape()
将抛出一个TypeError: 'tuple' object is not callable.
because it's not a function, it's a value. 因为它不是一个功能,它是一个价值。
What you want to do is just tuple unpack .shape
without the ()
. 你想要做的只是在没有
()
情况下解包.shape
。 Example: 例:
>> import numpy
>> ndarray = numpy.ndarray((20, 21))
>> ndarray.shape
(20, 21)
>> x, y = ndarray.shape
>> x
20
>> y
21
http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.shape.html http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.shape.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.