Lab 1 Geometric Transformation Using cv2.remap()

Task:

  1. Load the Lenna image.
  2. Create two distinct geometric transformations for the left and right halves of the image using gamma correction:
  3. Use the cv2.remap() function to remap the pixels according to the corrected positions.
  4. Display the original image and the gamma-corrected remapped image side by side.

image.png

Explaination

Original Gamma Correction:

For the y-coordinates of the pixels, gamma correction modifies the positions using the formula:

$$ y_{\text{new}} = y_{\text{old}}^{\gamma}  $$

Exercise caution, as the gamma correction function is valid only for values within the range $[0, 1]$. Therefore, we must also normalize the positions from their original $[0, 0]$ to $[H-1, W-1]$ to a from $[0, 0]$ to $[1, 1]$ range. You will later to scale them back to the original coordinate.

This means:

Inverse Gamma Correction:

Since interpolation is fundamentally about determining the y positions in the original image, we need to apply an inverse transformation i.e., find the mapping below.

$$ y_{\text{new}} \rightarrow y_{\text{old}} $$

def inverse_gamma_mapping(
  x, y, 
  height, width, 
  gamma_left=0.75, 
  gamma_right=1.25):
  if x < 0.5:
      y_corrected = y ** (1 / gamma_left)
  else:  
      y_corrected = y ** (1 / gamma_right)
    
    
  return x, y_corrected

To reverse the gamma correction and restore the y-coordinates to their original values, we simply use the inverse of the gamma function. For example, this can be expressed as:

$$ y_{\text{old}} = y_{\text{new}}^{\frac{1}{\gamma}} $$

Where:

Helper Functions

Codes

In the code, the inverse_gamma_mapping function applies this inverse operation based on the pixel's position:

$$ y_{\text{original}} = y_{\text{corrected}}^{\frac{1}{0.75}} = y_{\text{corrected}}^{1.33}  $$