简体   繁体   English

如何检查numpy数组的尺寸?

[英]How to check dimensions of a numpy array?

I want to do something like this inside a function: 我想在函数中做这样的事情:

if image.shape == 2 dimensions
    return image # this image is grayscale
else if image.shape = 3 dimensions
    return image # image is either RGB or YCbCr colorspace

Here, the image is a numpy array. 这里,图像是一个numpy数组。 I am not able to define that checking condition. 我无法定义检查条件。 I am really stuck at this point. 我真的被困在这一点上。 Can anyone please help? 有人可以帮忙吗?

numpy.array.shape is a tuple of array dimensions. numpy.array.shape是数组维度的元组。 You can compute the length of a tuple and that will give the number of dimensions. 您可以计算元组的长度,这将给出维数。

if len(image.shape) == 2:
    return image # this image is grayscale
elif len(image.shape) == 3:
    return image # image is either RGB or YCbCr colorspace

Numpy arrays also have a ndim attribute. Numpy数组也有一个ndim属性。

if image.ndim == 2:
    return image # this image is grayscale
elif image.ndim == 3:
    return image # image is either RGB or YCbCr colorspace

THERE IS ONE MORE " hidden-DIMENSION " OF THE PROBLEM : 还有一个问题的“ 隐藏维度 ”:

Numpy has a few performance-motivated tricks, that may confuse you, if not taking due care of: Numpy有一些以性能为导向的技巧,如果不加以适当考虑,可能会让你感到困惑:

There are tricks that np.view() creates, as a view into a " foreign "-owned data, which actually means there has appeared a form of hidden sharing , which may pretty ugly surprise you if some operations on already " return() -ed" image store any modification into a just- .view() -created view . np.view()创建的技巧,作为“ 外国 ”拥有的数据的视图 ,实际上意味着已经出现了一种隐藏共享形式,如果某些操作已经“ return() ,这可能会让你感到非常难过-ed” image存储中的任何修改成只是- .view() -created In such cases your code will silently damage the " foreign "-data, because you did not realise or notice that while the image reports correctly both the image.{shape|ndim} , yet it does not " have " its own data, but provides just a view through a "periscope-alike-window" into the original data, still " owned " somewhere else by someone else ( here, a write into image , like in an exemplary assignment of image[0,0] = -1 will actually " write-through " the -1 to get it stored into the " throat " of A -s A.data , being actually a <read-write buffer for 0x7efe9ff73800, size 7372800, offset 0 at 0x7efe9ff6cbf0> implementor-zone of the actual write-storage operation resulting in modification taking place in A 's data ) 在这种情况下,你的代码会默默地破坏“ 外来 ”数据,因为你没有意识到或注意到image正确报告了image.{shape|ndim} ,但它没有“ 拥有 ”自己的数据,但是提供了一个通过“periscope-like-window”进入原始数据的视图,仍然由其他人“ 拥有 ”在其他地方(这里,写入image ,就像在image的示例性分配image[0,0] = -1将实际“ 写入-1以将其存储到A -s A.data的“ 喉咙 ”中,实际上是A.data<read-write buffer for 0x7efe9ff73800, size 7372800, offset 0 at 0x7efe9ff6cbf0>实现区域实际的写入存储操作导致在A的数据中发生修改)

This side-effect of .view() -s is both very powerful (if used knowingly, where intended, for performance or other reasons) yet also very awful to detect/debug (if not detected in the code design phase and just hunting the devils tail once the " source "-data get damaged without knowing who does turned the so far clear and sound design wreck havoc " foreign & safely-owned "-data from behind a curtain ... that may take and indeed takes pretty long to detect such hidden sharing write-throughs ... if they take place without your prior intent ) .view() s的这种副作用非常强大 (如果故意使用,出于性能或其他原因需要),但检测/调试也非常糟糕 (如果在代码设计阶段没有检测到,只是寻找恶魔尾巴一旦“ 源头 ” - 数据受到损害而不知道是谁转向了迄今为止清晰而健全的设计破坏了“ 外国 安全拥有 ” - 从幕后d d的数据...... 这可能需要很长时间才能实现检测这种隐藏的共享直写...如果它们是在没有您事先意图的情况下发生的话

>>> A = np.arange( 3*640*480 ).reshape( 3, 640, 480 )
>>> A.shape
(3, 640, 480)
>>> A.ndim
3

>>> B = A.view()  # ENFORCE B BECOME A VIEW() ON AN ARRAY
>>> B.shape       #         B MIMICS AN ARRAY, WHILE IT IS NOT AN ARRAY
(3, 640, 480)
>>> B.ndim        #         B MIMICS AN ARRAY, WHILE IT IS NOT AN ARRAY
3
>>> B.flags       #         B MIMICS AN ARRAY, WHILE IT IS NOT AN ARRAY
  C_CONTIGUOUS    : True
  F_CONTIGUOUS    : False
  OWNDATA         : False <------------------- a view() on array, not the array itself
  WRITEABLE       : True
  ALIGNED         : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY    : False

>>> image = B[0,:,:] #   slice B into an IMAGE, which will still look as if it is an array
>>> image.flags
  C_CONTIGUOUS    : True
  F_CONTIGUOUS    : False
  OWNDATA         : False <----------------- a view() onto an array, not the array itself
  WRITEABLE       : True
  ALIGNED         : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY    : False
>>> image.ndim
2
>>> image.shape
(640, 480)

Hope you will enjoy the .view() -s, where used for performance or other reasons and hope you will never have to back-track the hidden-sharing side-effects in production. 希望你会喜欢.view() s,用于表演或其他原因,希望你永远不必回溯生产中隐藏共享的副作用。

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

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