title: How to get the MSE of each pixel in two images of the same dimension
body: <p>I have two images that will be identical in dimensions.</p>
  
  <p>The images are numpy arrays and I am itterating the pixels and aquiring the <code>r</code> <code>g</code> <code>b</code> of each pixel like this:</p>
  
  <pre><code>for i in range(len(img1)):
      for j in range(len(img1[0])):
          pimg1 = img1[i][j]
          pimg2 = img2[i][j]
  
          r1 = pimg1[0]
          r2 = pimg2[0]
          g1 = pimg1[1]
          g2 = pimg2[1]
          b1 = pimg1[2]
          b2 = pimg2[2]
  </code></pre>
  
  <p>I then acquire the MSE between the two pixels like:</p>
  
  <pre><code>mse = math.sqrt(((r1 - r2) ** 2) + ((g1 - g2) ** 2) + ((b1 - b2) ** 2))
  </code></pre>
  
  <p>The problem is this is ridiculously slow. Is there a more efficient way to do this?</p>
  
  <hr>
  
  <h2>The final goal</h2>
  
  <p>I am looking to make all pixels that have a certain "thresholded" similarity, between the two images, black. And all pixels that have a larger difference the pixel of <code>img2</code>.</p>
  
  <pre><code>if mse &gt; threshold:
      new_img[i][j] = pimg2
  else:
      new_img[i][j] = [0, 0, 0] # black pixel
  </code></pre>
  
  <h2>background image</h2>
  
  <p><a href="https://i.stack.imgur.com/1M69e.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1M69e.jpg" alt="enter image description here"></a></p>
  
  <h2>entered image</h2>
  
  <p><a href="https://i.stack.imgur.com/6RJgg.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6RJgg.jpg" alt="enter image description here"></a></p>
  
  <hr>
  
  <p>I am capturing images like:</p>
  
  <pre><code>for frame in cam.camera.capture_continuous(raw, format="rgb", use_video_port=True):
      img = frame.array
      cv2.imwrite("image.png", img)
  </code></pre>
  
  <p>I am getting the images like:</p>
  
  <pre><code>dir = 'images/compare/'
  bg = cv2.imread(dir+'bg.png')
  img = cv2.imread(dir+'in.png')
  </code></pre>