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.
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.
Method 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)
This takes around 7 seconds
for image of size of 512*512
Method 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)
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?
Configuration:
- Ubuntu 12.04
- 3rd Gen core i5 processor
- 4 GB RAM
- 2 GB ATI RADEON GPU (which I have turned off)