dan-masek

custom_undistort_v4

Nov 7th, 2019
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.24 KB | None | 0 0
  1. import numpy as np
  2. import time
  3.  
  4. def _extract_camera_matrix(camera_matrix):
  5.     fx = camera_matrix[0,0]
  6.     cx = camera_matrix[0,2]
  7.     fy = camera_matrix[1,1]
  8.     cy = camera_matrix[1,2]
  9.     return fx, cx, fy, cy
  10.    
  11. def _extract_distortion_coefficients(k1, k2, k3, k4, k5, k6, p1, p2):
  12.     return k1, k2, k3, k4, k5, k6, p1, p2
  13.  
  14. def _get_undistort_rectify_maps(distortion_coefficients, camera_matrix, image_width, image_height, step_size = 20):
  15.     start = time.time()
  16.    
  17.     # Prepare coordinates
  18.     coords_x = np.arange(image_width, dtype=np.float32)
  19.     coords_y = np.arange(image_height, dtype=np.float32)
  20.    
  21.     # Apply inverse camera matrix
  22.     camera_matrix_inv = np.linalg.inv(camera_matrix)
  23.     fx_inv, cx_inv, fy_inv, cy_inv = _extract_camera_matrix(camera_matrix_inv)
  24.    
  25.     x = fx_inv * coords_x + cx_inv
  26.     y = fy_inv * coords_y + cy_inv
  27.  
  28.     # Precalculate some common things
  29.     xx = np.multiply(x, x)
  30.     two_xx = 2 * xx
  31.    
  32.     yy = np.multiply(y, y)
  33.  
  34.     # Preallocate result arrays
  35.     x_distorted = np.zeros((image_height, image_width), np.float32)
  36.     y_distorted = np.zeros((image_height, image_width), np.float32)
  37.  
  38.     # Extract the various coefficients
  39.     k1, k2, k3, k4, k5, k6, p1, p2 = _extract_distortion_coefficients(**distortion_coefficients)
  40.     fx, cx, fy, cy = _extract_camera_matrix(camera_matrix)
  41.    
  42.     # Precalculate (minor, but still useful)
  43.     two_p1, two_p2 = (2 * p1), (2 * p2)
  44.    
  45.     for i in range(0, image_height, step_size):
  46.         # Handle case when last chunk is shorter then step_size
  47.         i_e = min(image_height, i + step_size)
  48.        
  49.         yv = y[i:i_e,np.newaxis]
  50.         yyv = yy[i:i_e,np.newaxis]
  51.        
  52.         xy = x * yv
  53.  
  54.         r2 = np.add(xx, yyv)
  55.         r4 = np.multiply(r2, r2)
  56.         r6 = np.multiply(r4, r2)
  57.        
  58.         radial_a = (1 + k1 * r2 + k2 * r4 + k3 * r6)
  59.         radial_b = (1 + k4 * r2 + k5 * r4 + k6 * r6)
  60.         radial = radial_a / radial_b
  61.        
  62.         tangential_x = two_p1 * xy + p2 * (r2 + two_xx)
  63.         tangential_y = p1 * (r2 + 2 * yyv) + two_p2 * xy
  64.        
  65.         x_distorted[i:i_e,:] = fx * (x * radial + tangential_x) + cx
  66.         y_distorted[i:i_e,:] = fy * (yv * radial + tangential_y) + cy
  67.  
  68.     end = time.time()
  69.     print("END: process", end - start)
  70.  
  71.     return (x_distorted, y_distorted)
  72.  
  73.  
  74. if __name__ == "__main__":
  75.     image_width = 4032
  76.     image_height = 3024
  77.  
  78.     distortion_coefficients = {
  79.         "k1": np.float32(0)
  80.         , "k2": np.float32(0)
  81.         , "k3": np.float32(0)
  82.         , "k4": np.float32(0)
  83.         , "k5": np.float32(0)
  84.         , "k6": np.float32(0)
  85.         , "p1": np.float32(0)
  86.         , "p2": np.float32(0)
  87.         }
  88.        
  89.     camera_matrix = np.array([
  90.         [1000, 0, 2016],
  91.         [0, 1000, 1512],
  92.         [0, 0, 1]], dtype=np.float32)
  93.        
  94.     start = time.time()
  95.     map_x, map_y = _get_undistort_rectify_maps(
  96.         distortion_coefficients
  97.         , camera_matrix
  98.         , image_width
  99.         , image_height
  100.         , 1)
  101.     end = time.time()
  102.     print("END: _get_undistort_rectify_maps", end - start)
  103.        
  104.     #np.savetxt("custund_mapx.csv", map_x, delimiter=",")
  105.     #np.savetxt("custund_mapy.csv", map_y, delimiter=",")
Add Comment
Please, Sign In to add comment