title: Correction Function to Balance Depth
body: <p>I have a depth image with a shape of <code>11 x 23</code>, and I do want to balance the depth in all cells using the fact that <strong>the depth in the four corners is the same</strong>, the values are shown in the drawing here:</p>
  <p><a href="https://i.stack.imgur.com/a2sdl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/a2sdl.png" alt="enter image description here" /></a></p>
  <p>My first idea was to calculate the difference between the corners and to sum that difference for each cell as follows:</p>
  <pre class="lang-py prettyprint-override"><code>def corr_func(arr):
      &quot;&quot;&quot;
      Function: corr_func, linear correction of discrete depth values.
      ---
      Parameters:
      @param: arr, ndarray, depth image to be corrected.
      
      ---
      @return: arr, ndarray, corrected depth image.
  
      &quot;&quot;&quot;
      c_00 = int(arr[0][0])
      c_01 = int(arr[0][-1])
      c_10 = int(arr[-1][0])
  
      y_corr = (c_01 - c_00)/arr.shape[1]
      x_corr = (c_10 - c_00)/arr.shape[0]
      print(&quot;x_corr = {}, y_corr = {}&quot;.format(x_corr, y_corr))
  
      r, c = arr.shape[:2]
  
      for i in range(r):
          for j in range(c):
              corr = math.floor(j*y_corr + i*x_corr)
              arr[i,j] -= corr
      return arr
  </code></pre>
  <p>This approach didn't work well as the correction value accumulates and makes the extreme corner value higher than it should be.</p>
  <hr />
  <h2>Edit:</h2>
  <hr />
  <p>Following the kind suggestion of @ChristophRackwitz
  I have treated the depth values as <code>z</code>, calculated the rotation angles on <code>X</code>, and <code>Y</code> axis, and applied the rotation as follows:</p>
  <pre class="lang-py prettyprint-override"><code>def corr_func(arr):
      &quot;&quot;&quot;
      Function: corr_func, non-linear correction of discrete depth values.
      ---
      Parameters:
      @param: arr, ndarray, depth image to be corrected.
      
      ---
      @return: arr, ndarray, corrected depth image.
  
      &quot;&quot;&quot;
      c_00 = int(arr[0][0])
      c_01 = int(arr[0][-1])
      c_10 = int(arr[-1][0])
  
      alpha = atan2((c_01 - c_00), arr.shape[1])
      beta  = atan2((c_10 - c_00), arr.shape[0])
  
      arr = arr * cos(alpha) * cos (beta)
      arr = arr.astype(np.uint8)
      return arr
  </code></pre>
  <p>The results seem to be better than the linear correction, but still looking for better results if possible.</p>
  <hr />
  <p>Can you please suggest to me a better correction approach? thanks in advance.</p>