Correction Function to Balance Depth

Question:

I have a depth image with a shape of 11 x 23, and I do want to balance the depth in all cells using the fact that the depth in the four corners is the same, the values are shown in the drawing here:

enter image description here

My first idea was to calculate the difference between the corners and to sum that difference for each cell as follows:

def corr_func(arr):
    """
    Function: corr_func, linear correction of discrete depth values.
    ---
    Parameters:
    @param: arr, ndarray, depth image to be corrected.
    
    ---
    @return: arr, ndarray, corrected depth image.

    """
    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("x_corr = {}, y_corr = {}".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

This approach didn’t work well as the correction value accumulates and makes the extreme corner value higher than it should be.


Edit:


Following the kind suggestion of @ChristophRackwitz
I have treated the depth values as z, calculated the rotation angles on X, and Y axis, and applied the rotation as follows:

def corr_func(arr):
    """
    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.

    """
    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

The results seem to be better than the linear correction, but still looking for better results if possible.


Can you please suggest to me a better correction approach? thanks in advance.

Asked By: Bilal

||

Answers:

As @YvesDaoust suggested, subtracting a Bilinear Model has solved this problem:

    def corr_func(self, arr):
        """
        Empirical Values
        """
        self.y_correc = 0.0 #- 0.5
        self.x_correc = + 4.0
        
        if(self.corr_array is None):
            self.corr_array = np.zeros((arr.shape), dtype=np.int16)
            r, c = arr.shape

            for i in range(r):
                for j in range(c):
                    self.corr_array[i, j] = floor(i*self.x_correc + j*self.y_correc)
                        
        arr = arr.astype(np.int16)
        arr += self.corr_array
        arr[arr<0]=0
        arr = arr//2
        arr = arr.astype(np.uint8)
        return arr
Answered By: Bilal
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.