numpy的两个数组满足不同的条件

时间:2019-03-02 20:41:58

标签: python arrays numpy image-processing

具体问题是我有一个二进制图像。现在,我想获得像素的索引,使其落入特定区域。因此,我首先采用nonzeroPixel = nonzero(img)并通过Py=np.array(nonzeroPixel[0])Px=np.array(nonzeroPixel[1])得到它们的x和y值(即坐标)。 假设我希望他们落入的矩形区域是x in (10,20)y in (30,40) 我的问题是如何获取落入此矩形区域的像素的索引?这意味着10<Px<20 && 30<Py<40

我尝试了np.where((Py >= 30) & (Py < 40) & (Px >= 10) & (Px < 20)),但我认为它仅适用于一个数组?

这是一个可能的解决方案,但我不知道它正在使用什么numpy函数。

good_pixel_index = ((Py > 30) & (Py < 40) & (Px > 10) &  (Px < 20)).nonzero()[0]

任何想法都会受到赞赏

2 个答案:

答案 0 :(得分:0)

您可以使用切片而不是np.where来执行此操作。

import numpy as np

# Make some fake 100x100 data
arr = np.random.randint(0, 2, 10000).reshape(100, 100)

# Select the correct slice (rows, columns)
print(arr[30:40, 10:20])

# Find the indices of the slice that aren't zero
ones = arr[30:40, 10:20].nonzero()

# Bump up the indices by the starting value of the slice
x_coords = ones[0] + 30
y_coords = ones[1] + 10

答案 1 :(得分:0)

举一个具体的例子:

In [168]: img = np.random.randint(0,2,(10,10))                                  
In [169]: img                                                                   
Out[169]: 
array([[1, 1, 1, 1, 0, 1, 1, 1, 0, 1],
       [0, 0, 1, 0, 1, 0, 0, 1, 1, 1],
       [1, 0, 0, 1, 0, 1, 1, 1, 0, 1],
       [0, 1, 0, 0, 1, 0, 1, 1, 0, 0],
       [0, 0, 1, 1, 1, 1, 0, 0, 1, 0],
       [0, 0, 0, 1, 0, 0, 0, 1, 0, 1],
       [1, 1, 1, 0, 1, 1, 1, 0, 1, 1],
       [1, 1, 0, 1, 0, 1, 1, 1, 1, 1],
       [1, 1, 0, 1, 1, 1, 0, 1, 1, 0],
       [0, 1, 0, 0, 1, 1, 1, 1, 0, 0]])
In [170]: idx = np.nonzero(img)                                                 
In [171]: idx                                                                   
Out[171]: 
(array([0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3,
        3, 4, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7,
        7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9]),
 array([0, 1, 2, 3, 5, 6, 7, 9, 2, 4, 7, 8, 9, 0, 3, 5, 6, 7, 9, 1, 4, 6,
        7, 2, 3, 4, 5, 8, 3, 7, 9, 0, 1, 2, 4, 5, 6, 8, 9, 0, 1, 3, 5, 6,
        7, 8, 9, 0, 1, 3, 4, 5, 7, 8, 1, 4, 5, 6, 7]))
In [172]: jdx = np.nonzero((idx[0]>=3)&(idx[0]<=8)&(idx[1]>4)&(idx[1]<9))       
In [173]: jdx                                                                   
Out[173]: (array([21, 22, 26, 27, 29, 35, 36, 37, 42, 43, 44, 45, 51, 52, 53]),)
In [174]: idx1 = (idx[0][jdx],idx[1][jdx])                                      
In [175]: idx1                                                                  
Out[175]: 
(array([3, 3, 4, 4, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8]),
 array([6, 7, 5, 8, 7, 5, 6, 8, 5, 6, 7, 8, 5, 7, 8]))
In [176]: img[idx1]                                                             
Out[176]: array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])

为了更好地显示我们选择的点:

In [177]: img[idx1] = 2                                                         
In [178]: img                                                                   
Out[178]: 
array([[1, 1, 1, 1, 0, 1, 1, 1, 0, 1],
       [0, 0, 1, 0, 1, 0, 0, 1, 1, 1],
       [1, 0, 0, 1, 0, 1, 1, 1, 0, 1],
       [0, 1, 0, 0, 1, 0, 2, 2, 0, 0],
       [0, 0, 1, 1, 1, 2, 0, 0, 2, 0],
       [0, 0, 0, 1, 0, 0, 0, 2, 0, 1],
       [1, 1, 1, 0, 1, 2, 2, 0, 2, 1],
       [1, 1, 0, 1, 0, 2, 2, 2, 2, 1],
       [1, 1, 0, 1, 1, 2, 0, 2, 2, 0],
       [0, 1, 0, 0, 1, 1, 1, 1, 0, 0]])

In [180]: img[3:9, 5:9]                                                         
Out[180]: 
array([[0, 2, 2, 0],
       [2, 0, 0, 2],
       [0, 0, 2, 0],
       [2, 2, 0, 2],
       [2, 2, 2, 2],
       [2, 0, 2, 2]])

In [182]: np.argwhere(img[3:9, 5:9])   # transpose of `nonzero`                                            
Out[182]: 
array([[0, 1],
       [0, 2],
       [1, 0],
       ...
       [5, 2],
       [5, 3]])

按照@roganjosh的建议应用切片偏移量,重新创建idx1数组:

In [183]: np.argwhere(img[3:9, 5:9])+[3,5]                                      
Out[183]: 
array([[3, 6],
       [3, 7],
       [4, 5],
       [4, 8],
       [5, 7],
       [6, 5],
       [6, 6],
       [6, 8],
       [7, 5],
       [7, 6],
       [7, 7],
       [7, 8],
       [8, 5],
       [8, 7],
       [8, 8]])

我认为走哪条路线无关紧要。