简体   繁体   English

如何获得ndarray的x和y维度 - Numpy / Python

[英]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.

相关问题 如何更快地迭代具有 2 个维度的 Python numpy.ndarray - How to faster iterate over a Python numpy.ndarray with 2 dimensions Python:Numpy 切片未一起比较/广播。 [x:y] vs [0:yx] 声称不同的维度 - Python: Numpy slices not comparing / broadcasting together. [x:y] vs [0: y-x] Claiming different dimensions NumPy ndarray 广播 - 形状 (X,) 与 (X, 1) 与 (X,Y) 一起操作 - NumPy ndarray broadcasting - shape (X,) vs (X, 1) to operate with (X,Y) 如何使用python将所有索引值的总和加到numpy ndarray的每个元素中? - How can I add to each element of a numpy ndarray the sum of all its index values with python ? 我如何检查 numpy.ndarray 的每个值在 Python 中都相同 - How can I check That Every values of an numpy.ndarray are all the same in Python 如何使用python将numpy.ndarray的人脸编码附加到列表对象中 - How can I append Face Encodings of numpy.ndarray into List Object using python 在Python中,如何将表示为numpy.ndarray的方阵提升为非整数幂? - In Python, how can I raise a square matrix represented as a numpy.ndarray to non-integer powers? 如何将numpy.ndarray更改为带有大括号的点格式 - Python 3.7 - How can i change a numpy.ndarray to point format with curly brackets - Python 3.7 python-opencv 我如何将文件对象转换为 numpy.ndarray - python-opencv How i can convert file-object to numpy.ndarray 在numpy,Python中的ndarray中查找最近的值X - Find nearest value X in ndarray in numpy, Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM