title: Speed up iteration over Numpy arrays / OpenCV cv2 image
body: <p>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.</p>
  
  <p>Here <code>Ix_Ix_blur</code>, <code>Ix_Iy_blur</code> and <code>Iy_Iy_blur</code> are of the same size. I'm trying to find features and draw it on OpenCV image.</p>
  
  <hr>
  
  <p>Method 1:</p>
  
  <pre><code>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 &gt; thresh):
              cv2.circle(img, (i,j), 1, (0, 0, 255), -1, 8)
  </code></pre>
  
  <p>This takes around <code>7 seconds</code> for image of size of 512*512</p>
  
  <hr>
  
  <p>Method 2:</p>
  
  <pre><code>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 &gt; 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)
  </code></pre>
  
  <p>This method also seems to take <code>7 seconds</code> to iterate over the same size of image.</p>
  
  <p>Is there any other way using which I can reduce the time required for iterations?</p>
  
  <p>Configuration:</p>
  
  <ul>
  <li>Ubuntu 12.04</li>
  <li>3rd Gen core i5 processor</li>
  <li>4 GB RAM</li>
  <li>2 GB ATI RADEON GPU (which I have turned off)</li>
  </ul>