Python如何在numpy中以C速度循環並存儲一些位置。

[英]Python how to loop through an array in numpy at C speed and store some positions


I am new to python, numpy and opencv. I am playing with the first example of harris corner detector from here. My objective is to get an ordered list of all the corners. With this simple code I am able to get the X and Y coordinates of the corners and their value:

我是python、numpy和opencv的新手。我正在玩第一個哈里斯角探測器的例子。我的目標是得到所有角落的有序列表。通過這個簡單的代碼,我可以得到角的X和Y坐標及其值:

height, width, depth = img.shape
print height, width
for i in range(0, height): #looping at python speed
  for j in range(0, (width)): 
    if dst[i,j] > 0.9*dst.max():
      print i, j, dst[i,j]

However, it is dead slow. I don't know how this is called but apparently with numpy one can loop through arrays at C speed and even assign values, example:

然而,它非常緩慢。我不知道這是怎么叫的,但是顯然,有了numpy,人們可以以C的速度循環數組,甚至可以賦值,例如:

img[0:height, 0:width, 0:depth] = 0

Can I loop through an array and assign the position of interesting values in another variable? I.e. can I use this on my code to make it faster?

我可以在一個數組中循環並在另一個變量中指定有趣值的位置嗎?也就是說,我可以在代碼中使用它來加快速度嗎?

2 个解决方案

#1


4  

You can get a mask of elements that would pass the IF conditional statement. Next up, if you need the indices that would pass the condition, use np.where or np.argwhere on the mask. For the valid dst elements, index dst with the same mask, thus using boolean indexing. The implementation would look something like this -

您可以獲得一個元素的蒙版,它將傳遞IF條件語句。接下來,如果你需要通過條件的指標,使用np。或np。argwhere面具。對於有效的dst元素,索引dst具有相同的掩碼,因此使用布爾索引。實現看起來是這樣的

mask = dst > 0.9*dst.max()
out = np.column_stack((np.argwhere(mask),dst[mask]))

If you would like to get those three printed outputs separately, you could do -

如果你想分別獲得這三種打印輸出,你可以。

I,J = np.where(mask)
valid_dst = dst[mask]

Finally, if you would like to edit the 3D array img based on the 2D mask, you could do -

最后,如果你想編輯基於二維蒙版的三維陣列img,你可以做-

img[mask] = 0

This way, you would change the corresponding elements in img across all channels in one go.

通過這種方式,您可以一次跨所有通道更改img中的相應元素。

#2


0  

First of all if you are using Python 2.X you should use xrange instead of range, this speeds things up. range in Python 3.X has the same implementation as xrange in Python 2.X.

首先,如果您使用的是Python 2。X應該使用xrange而不是range,這樣可以加快速度。Python 3內的范圍。X在Python 2.X中有與xrange相同的實現。

If you want to iterate over numpy arrays why not use the numpy enumerator for iteration?

如果您想要遍歷numpy數組,為什么不使用numpy枚舉器進行迭代呢?

# creating a sample img array with size 2x2 and RGB values
# so the shape is (2, 2, 3)
> img = np.random.randint(0, 255, (2,2,3))

> img
> array([[[228,  61, 154],
          [108,  25,  52]],

          [[237, 207, 127],
           [246, 223, 101]]])

# iterate over the values, getting key and value at the same time
# additionally, no nasty nested for loops
> for key, val in np.ndenumerate(img):
>     print key, val

# this prints the following
> (0, 0, 0) 228
> (0, 0, 1) 61
> (0, 0, 2) 154
> (0, 1, 0) 108
> (0, 1, 1) 25
> (0, 1, 2) 52
> (1, 0, 0) 237
> (1, 0, 1) 207
> (1, 0, 2) 127
> (1, 1, 0) 246
> (1, 1, 1) 223
> (1, 1, 2) 101

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/10/31/725a968f91870e0d74c7fcac1eb541ed.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com