title: Surface normal calculation from depth map in python
body: <p>I try to implement following c++ code in python:</p>
  
  <pre><code>depth.convertTo(depth, CV_64FC1); // I do not know why it is needed to be 
  transformed to 64bit image my input is 32bit
  
  Mat nor(depth.size(), CV_64FC3);
  
  for(int x = 1; x &lt; depth.cols - 1; ++x)
  {
     for(int y = 1; y &lt; depth.rows - 1; ++y)
     {
        Vec3d t(x,y-1,depth.at&lt;double&gt;(y-1, x)/*depth(y-1,x)*/);
        Vec3d l(x-1,y,depth.at&lt;double&gt;(y, x-1)/*depth(y,x-1)*/);
        Vec3d c(x,y,depth.at&lt;double&gt;(y, x)/*depth(y,x)*/);
        Vec3d d = (l-c).cross(t-c);
        Vec3d n = normalize(d);
        nor.at&lt;Vec3d&gt;(y,x) = n;
     }
  }
  
  imshow("normals", nor);
  </code></pre>
  
  <p>python code:</p>
  
  <pre><code>d_im = cv2.imread("depth.jpg")
  d_im = d_im.astype("float64")
  
  normals = np.array(d_im, dtype="float32")
  h,w,d = d_im.shape
  for i in range(1,w-1):
    for j in range(1,h-1):
      t = np.array([i,j-1,d_im[j-1,i,0]],dtype="float64")
      f = np.array([i-1,j,d_im[j,i-1,0]],dtype="float64")
      c = np.array([i,j,d_im[j,i,0]] , dtype = "float64")
      d = np.cross(f-c,t-c)
      n = d / np.sqrt((np.sum(d**2)))
      normals[j,i,:] = n
  
  cv2.imwrite("normal.jpg",normals*255)
  </code></pre>
  
  <p>input image:</p>
  
  <p><a href="https://i.stack.imgur.com/gnjFK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gnjFK.png" alt="enter image description here"></a></p>
  
  <p>c++ code output:</p>
  
  <p><a href="https://i.stack.imgur.com/L3Vc3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/L3Vc3.png" alt="enter image description here"></a></p>
  
  <p>my python code output:</p>
  
  <p><a href="https://i.stack.imgur.com/vVhUC.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vVhUC.jpg" alt="enter image description here"></a></p>
  
  <p>i can not find reason of these differences. How i can get c++ code output with python?</p>