How can I get the x and y dimensions of a ndarray - Numpy / Python

妖精的绣舞 提交于 2019-12-10 13:19:00

问题


I'm wondering if I can get the x and y dimensions of a ndarray separately. 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?

Thank you in advance.


回答1:


You can use tuple unpacking.

y, x = a.shape



回答2:


height, width = a.shape

Note, however, that ndarray has matrix coordinates (i,j), which are opposite to image coordinates (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:

dims = a.shape
height = dims[0]
width = dims[1]



回答3:


ndarray.shape() will throw a 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 (). 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



来源:https://stackoverflow.com/questions/22490721/how-can-i-get-the-x-and-y-dimensions-of-a-ndarray-numpy-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!