Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.01 KB | None | 0 0
  1. import math
  2. import functools
  3. import numpy as np
  4. from skimage import io
  5. from skimage import util
  6. from skimage import color
  7. from skimage import transform
  8. import matplotlib.pyplot as plt
  9.  
  10.  
  11. def normalize(v):
  12.     return v / np.linalg.norm(v)
  13.  
  14.  
  15. def world_to_camera_with_pose(view_pose):
  16.     lookat_pose = view_pose[3:]
  17.     camera_pose = view_pose[:3]
  18.     up = np.array([0, 1, 0])
  19.     R = np.diag(np.ones(4))
  20.     R[2, :3] = normalize(lookat_pose - camera_pose)
  21.     R[0, :3] = normalize(np.cross(R[2, :3], up))
  22.     R[1, :3] = -normalize(np.cross(R[0, :3], R[2, :3]))
  23.     T = np.diag(np.ones(4))
  24.     T[:3, 3] = -camera_pose
  25.     return R.dot(T)
  26.  
  27.  
  28. def camera_intrinsic_transform(vfov=45, hfov=60, pixel_width=320, pixel_height=240):
  29.     camera_intrinsics = np.zeros((4, 4))
  30.     camera_intrinsics[2, 2] = 1
  31.     camera_intrinsics[0, 0] = (pixel_width / 2.0) / math.tan(math.radians(hfov / 2.0))
  32.     camera_intrinsics[0, 2] = pixel_width / 2.0
  33.     camera_intrinsics[1, 1] = (pixel_height / 2.0) / math.tan(math.radians(vfov / 2.0))
  34.     camera_intrinsics[1, 2] = pixel_height / 2.0
  35.     camera_intrinsics[3, 3] = 1
  36.     return camera_intrinsics
  37.  
  38.  
  39. if __name__ == "__main__":
  40.     H, W = 240, 320
  41.     K = camera_intrinsic_transform(pixel_width=W, pixel_height=H)
  42.  
  43.     dst_vec = np.loadtxt("data/125.txt")  # [6]
  44.     dst_Rt = world_to_camera_with_pose(dst_vec)
  45.     dst_P = K @ dst_Rt
  46.     dst_img = util.img_as_float(io.imread("data/125.jpg"))  # [H, W, 3]
  47.     dst_dep = io.imread("data/125.png").astype(np.float32)  # [H, W]
  48.     dst_dep = 1 / (dst_dep * 0.001 + 1e-8)  # depth -> disparity
  49.  
  50.     src_vec = np.loadtxt("data/0.txt")
  51.     src_Rt = world_to_camera_with_pose(src_vec)
  52.     src_P = K @ src_Rt
  53.     src_img = util.img_as_float(io.imread("data/0.jpg"))  # [H, W, 3]
  54.     src_dep = io.imread("data/0.png").astype(np.float32)  # [H, W]
  55.     src_dep = 1 / (src_dep * 0.001 + 1e-8)  # depth -> disparity
  56.  
  57.     M = src_P @ np.linalg.pinv(dst_P)
  58.  
  59.     dst_xx, dst_yy = np.meshgrid(np.arange(W), np.arange(H))
  60.     dst_xx, dst_yy = dst_xx.ravel(), dst_yy.ravel()  # [H * W], [H * W]
  61.     dst_coords = np.stack(
  62.         [dst_xx, dst_yy, np.ones_like(dst_xx), dst_dep[dst_yy, dst_xx]]
  63.     )  # [4, W * H]
  64.     src_coords = M @ dst_coords # [4, W * H]
  65.     src_coords[:2] = src_coords[:2] / (src_coords[2] + 1e-8) # homogeneous normalize
  66.     src_xx, src_yy = src_coords[:2]
  67.  
  68.     coords = np.stack([src_yy, src_xx], axis=0) # [2, H * W]
  69.     coords = coords.reshape(2, H, W) # coords[:, out_r, out_c] = (inp_r, inp_c)
  70.     out_img = np.stack([
  71.         transform.warp(src_img[..., 0], coords), # red
  72.         transform.warp(src_img[..., 1], coords), # green
  73.         transform.warp(src_img[..., 2], coords), # blue
  74.     ], axis=2)
  75.  
  76.     fig, ax = plt.subplots(2, 2)
  77.     ax = ax.ravel()
  78.     ax[0].set_title('dst')
  79.     ax[1].set_title('src')
  80.     ax[2].set_title('out')
  81.     ax[3].set_title('diff')
  82.     ax[0].imshow(dst_img)
  83.     ax[1].imshow(src_img)
  84.     ax[2].imshow(out_img)
  85.     ax[3].imshow(np.max(dst_img - out_img, axis=2))
  86.     plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement