加快Numpy数组/ OpenCV cv2映像的迭代速度 [英] Speed up iteration over Numpy arrays / OpenCV cv2 image
问题描述
我有3个numpy形状阵列>(500,500)。我试图同时迭代它们。我尝试过两种不同的方法,但两种方法都很慢。
I have 3 numpy arrays of shape > (500, 500). I am trying to iterate over them simultaneously. I have tried two different methods but both of them are slow.
这里 Ix_Ix_blur
, Ix_Iy_blur
和 Iy_Iy_blur
具有相同的大小。我正在尝试查找功能并在OpenCV图像上绘制。
Here Ix_Ix_blur
, Ix_Iy_blur
and Iy_Iy_blur
are of the same size. I'm trying to find features and draw it on OpenCV image.
方法1:
for i in xrange (Ix_Ix_blur.shape[1]):
for j in xrange(Ix_Ix_blur.shape[0]):
A = np.array([ [Ix_Ix_blur[j][i], Ix_Iy_blur[j][i]],
[Ix_Iy_blur[j][i], Iy_Iy_blur[j][i]] ])
detA = (A[0][0]*A[1][1])-(A[0][1]*A[1][0])
traceA = A[0][0]+A[1][1]
harmonic_mean = detA/traceA
if(harmonic_mean > thresh):
cv2.circle(img, (i,j), 1, (0, 0, 255), -1, 8)
这需要 7秒
用于尺寸为512 * 512的图片
This takes around 7 seconds
for image of size of 512*512
方法2 :
Ix_Iy_blur_iter = np.nditer(Ix_Iy_blur)
Iy_Iy_blur_iter = np.nditer(Iy_Iy_blur)
Ix_Ix_blur_iter = np.nditer(Ix_Ix_blur)
while(not Ix_Iy_blur_iter.finished):
try:
A = np.array([[Ix_Ix_blur_iter.next(), Ix_Iy_blur_iter.next()],[Ix_Iy_blur_iter.value, Iy_Iy_blur_iter.next()]])
except StopIteration:
break
detA = (A[0][0]*A[1][1])-(A[0][1]*A[1][0])
traceA = A[0][0]+A[1][1]
harmonic_mean = detA/traceA
if(harmonic_mean > thresh):
i = Ix_Ix_blur_iter.iterindex/Ix.shape[0]
j = Ix_Ix_blur_iter.iterindex - Ix.shape[0]*i
cv2.circle(img, (j,i), 1, (0, 0, 255), -1, 8)
此方法似乎也似乎拿 7秒
来迭代相同大小的图像。
This method also seems to take 7 seconds
to iterate over the same size of image.
还有其他方法可以减少迭代所需的时间?
Is there any other way using which I can reduce the time required for iterations?
配置:
- Ubuntu 12.04
- 第三代核心i5处理器
- 4 GB RAM
- 2 GB ATI RADEON GPU(已关闭)
- Ubuntu 12.04
- 3rd Gen core i5 processor
- 4 GB RAM
- 2 GB ATI RADEON GPU (which I have turned off)
推荐答案
首先你可以使用 Ix_Ix_blur [j,i]
而不是 Ix_Ix_blur [j] [i]
。 Ix_Ix_blur [j] [i]
将创建一个非常慢的临时数组。
First you can use Ix_Ix_blur[j, i]
instead of Ix_Ix_blur[j][i]
. Ix_Ix_blur[j][i]
will create a temporary array which is very slow.
加速元素访问ndarray,你可以使用item()方法,它返回python本机数值,你不需要创建一个临时数组A.用本机数值计算比numpy标量快。
To speedup element access with ndarray, you can use item() method, which return python native numeric values, and you don't need to create a temporary array A. Calculation with native numeric values is faster than numpy scalars.
for i in xrange (Ix_Ix_blur.shape[1]):
for j in xrange(Ix_Ix_blur.shape[0]):
a, b, c = Ix_Ix_blur.item(j, i), Ix_Iy_blur.item(j, i), Iy_Iy_blur.item(j, i)
detA = a*c - b*b
traceA = a + c
harmonic_mean = detA/traceA
if harmonic_mean > thresh:
cv2.circle(img, (i,j), 1, (0, 0, 255), -1, 8)
对于您的特定问题,没有必要在循环中进行计算,您可以:
For your particular problem, it's not necessary to do the calculation in a loop, you can:
detA = Ix_Ix_blur * Iy_Iy_blur - Ix_Iy_blur**2
traceA = Ix_Ix_blur + Iy_Iy_blur
harmonic_mean = detA / traceA
for j, i in np.argwhere(harmonic_mean > thresh):
cv2.circle(img, (i,j), 1, (0, 0, 255), -1, 8)
这篇关于加快Numpy数组/ OpenCV cv2映像的迭代速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!