dan-masek

custom_undistort_v2

Nov 7th, 2019
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.08 KB | None | 0 0
  1. import numpy as np
  2. import time
  3.  
  4. def _distort_z_1_impl(x, y, k1, k2, k3, k4, k5, k6, p1, p2):
  5.     xv = x
  6.     yv = y
  7.    
  8.     x2 = np.multiply(x, x)
  9.     y2 = np.multiply(y, y)
  10.     xy = np.multiply(x, y)
  11.  
  12.     r2 = np.add(x2, y2)
  13.     r4 = np.multiply(r2, r2)
  14.     r6 = np.multiply(r4, r2)
  15.  
  16.     radial_a = (1 + k1 * r2 + k2 * r4 + k3 * r6)
  17.     radial_b = (1 + k4 * r2 + k5 * r4 + k6 * r6)
  18.     radial = radial_a / radial_b
  19.  
  20.     tangential_x = (2 * p1) * xy + p2 * (r2 + 2 * x2)
  21.     tangential_y = p1 * (r2 + 2 * y2) + (2 * p2) * xy
  22.  
  23.     x_distorted = x * radial + tangential_x
  24.     y_distorted = y * radial + tangential_y
  25.  
  26.     return x_distorted, y_distorted
  27.    
  28. def _distort_z_1(x, y, step_size, k1, k2, k3, k4, k5, k6, p1, p2):
  29.     x_distorted = np.zeros_like(x)
  30.     y_distorted = np.zeros_like(y)
  31.    
  32.     for r in range(0, x.shape[0], step_size):
  33.         rend = min(x.shape[0], r + step_size)
  34.         x_distorted[r:rend,:], y_distorted[r:rend,:] = _distort_z_1_impl(x[r:rend,:], y[r:rend,:], k1, k2, k3, k4, k5, k6, p1, p2)
  35.    
  36.     return x_distorted, y_distorted
  37.  
  38.  
  39. # Change dimension from [2 x H x W] to [H x W x 3 x 1] to correctly multiply with [3 x 3] matrix
  40. def _homogeneous_reshape(points_x, points_y):
  41.     points_homogeneous_reshaped = (
  42.         # Add extra axis to change from [H x W x 3] to [H x W x 3 x 1]
  43.         np.expand_dims(
  44.             # Change from [3 x H x W] to [H x W x 3]
  45.             np.transpose(
  46.                 # Change from [2 x H x W] to [3 x H x W] (homogeneous coordinates)
  47.                 np.stack(
  48.                     np.broadcast_arrays(points_x, points_y, np.float32(1))),
  49.                 (1, 2, 0)),
  50.             -1))
  51.            
  52.     return points_homogeneous_reshaped
  53.  
  54.  
  55. def _homogeneous_reshape_back(points_homogeneous_reshaped):
  56.     points_homogeneous = (
  57.         # Get back from [H x W x 3] to [3 x H x W]
  58.         np.transpose(
  59.             # Remove extra axis: [H x W x 3 x 1] to [H x W x 3]
  60.             np.squeeze(
  61.                 points_homogeneous_reshaped),
  62.             (2, 0, 1)))
  63.  
  64.     # Get back from homogeneous coordinates
  65.     points_x, points_y, _ = points_homogeneous
  66.  
  67.     return points_x, points_y
  68.  
  69.  
  70. def _get_undistort_rectify_maps(distortion_coefficients, camera_matrix, image_width, image_height, step_size = 20):
  71.     image_points = np.meshgrid(np.arange(image_width, dtype=np.float32), np.arange(image_height, dtype=np.float32))
  72.    
  73.     # print("BEGIN: _homogeneous_reshape")
  74.     start = time.time()
  75.     image_points_homogeneous_reshaped = _homogeneous_reshape(*image_points)
  76.     end = time.time()
  77.     print("END: _homogeneous_reshape", end - start)
  78.    
  79.     camera_matrix_inv = np.linalg.inv(camera_matrix)
  80.    
  81.     # print("BEGIN: camera_matrix_inv @ image_points_homogeneous_reshaped")
  82.     start = time.time()
  83.     image_points_homogeneous_z_1_reshaped = np.matmul(camera_matrix_inv, image_points_homogeneous_reshaped)
  84.     end = time.time()
  85.     print("END: camera_matrix_inv @ image_points_homogeneous_reshaped", end - start)
  86.    
  87.     # print("BEGIN: _homogeneous_reshape_back")
  88.     start = time.time()
  89.     image_points_z_1 = _homogeneous_reshape_back(image_points_homogeneous_z_1_reshaped)
  90.     end = time.time()
  91.     print("END: _homogeneous_reshape_back", end - start)
  92.    
  93.     # print("BEGIN: _distort_z_1")
  94.     start = time.time()
  95.     x_distorted, y_distorted = _distort_z_1(
  96.         *image_points_z_1
  97.         , step_size=step_size
  98.         , **distortion_coefficients)
  99.     end = time.time()
  100.     print("END: _distort_z_1", end - start)
  101.    
  102.     # print("BEGIN: _homogeneous_reshape")
  103.     start = time.time()
  104.     points_homogeneous_z_1_distorted_reshaped = _homogeneous_reshape(x_distorted, y_distorted)
  105.     end = time.time()
  106.     print("END: _homogeneous_reshape", end - start)
  107.  
  108.     # print("BEGIN: _homogeneous_reshape")
  109.     start = time.time()
  110.     points_homogeneous_distorted_reshaped = np.matmul(camera_matrix, points_homogeneous_z_1_distorted_reshaped)
  111.     end = time.time()
  112.     print("END: camera_matrix @ points_homogeneous_z_1_distorted_reshaped", end - start)
  113.  
  114.     # print("BEGIN: _homogeneous_reshape_back")
  115.     start = time.time()
  116.     points_homogeneous_distorted = _homogeneous_reshape_back(points_homogeneous_distorted_reshaped)
  117.     end = time.time()
  118.     print("END: _homogeneous_reshape_back", end - start)
  119.  
  120.     return (map.astype(np.float32) for map in points_homogeneous_distorted)
  121.  
  122.  
  123. if __name__ == "__main__":
  124.     image_width = 4032
  125.     image_height = 3024
  126.  
  127.     distortion_coefficients = {
  128.         "k1": 0, "k2": 0, "k3": 0, "k4": 0, "k5": 0, "k6": 0,
  129.         "p1": 0, "p2": 0}
  130.  
  131.     camera_matrix = np.array([
  132.         [1000, 0, 2016],
  133.         [0, 1000, 1512],
  134.         [0, 0, 1]], dtype=np.float32)
  135.  
  136.     start = time.time()
  137.     map_x, map_y = _get_undistort_rectify_maps(
  138.         distortion_coefficients
  139.         , camera_matrix
  140.         , image_width
  141.         , image_height
  142.         , 20)
  143.     end = time.time()
  144.     print("END: _get_undistort_rectify_maps", end - start)
  145.    
  146.     #np.savetxt("custund_mapx.csv", map_x, delimiter=",")
  147.     #np.savetxt("custund_mapy.csv", map_y, delimiter=",")
Add Comment
Please, Sign In to add comment