使用itemgetter和numpy数组进行过滤

时间:2013-04-29 09:10:52

标签: python arrays matrix numpy

我收到以下错误:

Traceback (most recent call last):
  File "calibrating.py", line 160, in <module>
    intrinsic = calibrate2(corners, cb_points, (640,480))
  File "calibrating.py", line 100, in calibrate2
    valid_corners = filter(itemgetter(0), image_corners)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

image_corners是numpy数组的列表,即

[array([[ 261.45239258,  140.88212585],
   [ 301.11242676,  156.306427  ],
   [ 343.38937378,  168.20132446],
   [ 382.79559326,  180.48405457],...
   [ 392.16989136,  338.6171875 ],
   [ 439.97772217,  337.2124939 ]], dtype=float32), ... ]

我想做的是在没有dtype=float32的情况下拍摄矩阵,我做错了什么?

2 个答案:

答案 0 :(得分:1)

itemgetter 无法访问 dtype 属性。

请尝试使用此过滤器:

filter(lambda arr: arr.dtype != float32, image_corners)

这将为您提供所有没有dtype==float32的基质。

答案 1 :(得分:0)

list comprehensions

[a for a in image_corners if a.dtype is not float32]