[英]how to filter an numpy.ndarray by a spevific number?
roi_pixel_img = crop_img[indices_list]
print (roi_pixel_img)
when i add (I use only to use the entire array (meaning only a part):当我添加时(我只使用整个数组(仅表示一部分)):
np.set_printoptions(threshold=sys.maxsize)
th output is:第 output 是:
the whole part happens in a while loop because I'm extracting pixels in this section, which is irrelevant to the question.整个部分发生在一个 while 循环中,因为我在这部分中提取像素,这与问题无关。
My goal is not to include the lines with [0 255 255] in this array, how can I do that?我的目标是不在此数组中包含带有 [0 255 255] 的行,我该怎么做?
the type of roi_pixel_img is numpy.ndarray. roi_pixel_img 的类型是 numpy.ndarray。
is it even possible to answer this question without an example code for you?如果没有示例代码,甚至可以回答这个问题吗?
You can do this by creating an indexing array:您可以通过创建索引数组来做到这一点:
r = (roi_pixel_img == [0,255,255]).all(axis = -1)
roi_pixel_img[~r]
The roi_pixel_img == [0,255,255]
statement will result in an array with the same shape as roi_pixel_img
(say (N, 3)
) and will compare element-wise, eg [0,255,0]
will result in [True, True, False]
. roi_pixel_img == [0,255,255]
语句将生成一个与roi_pixel_img
具有相同形状的数组(比如(N, 3)
),并将按元素进行比较,例如[0,255,0]
将生成[True, True, False]
. Using .all(axis = -1)
Will reduce along the last axis (in this case axis = 1
would produce the same result) and will result in True
if all the element match.使用.all(axis = -1)
将沿最后一个轴减少(在这种情况下axis = 1
将产生相同的结果)并且如果所有元素匹配将导致True
。 So r
will have shape (N, )
.所以r
的形状是(N, )
。
Using ~r
to index will exclude the matching pixels and due to the shape will be broadcast appropriately by numpy
.使用~r
索引将排除匹配像素,并且由于形状将由numpy
适当广播。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.