Advertisement
dan-masek

custom_undistort_v1

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