Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. from matplotlib import pyplot as plt
  2. import numpy as np
  3. from random import random
  4.  
  5.  
  6. fx = 0.0176 # focal length[m]
  7. fy = 0.0176 # focal length[m]
  8. Nu = 1920 # number of horizontal[pixels]
  9. Nv = 1200 # number of vertical[pixels]
  10. ppx = 5.86e-6 # horizontal pixel pitch[m / pixel]
  11. ppy = ppx # vertical pixel pitch[m / pixel]
  12. fpx = fx / ppx # horizontal focal length[pixels]
  13. fpy = fy / ppy # vertical focal length[pixels]
  14.  
  15. K = [[fpx, 0, Nu / 2],
  16. [0, fpy, Nv / 2],
  17. [0, 0, 1]]
  18. K = np.array(K)
  19.  
  20. def quat2dcm(q):
  21. # Computing direction cosine matrix from quaternion
  22. # adapted from PyNav
  23.  
  24. # normalizing quaternion
  25. q = q / np.linalg.norm(q)
  26.  
  27. q0 = q[0]
  28. q1 = q[1]
  29. q2 = q[2]
  30. q3 = q[3]
  31.  
  32. dcm = np.zeros((3, 3))
  33.  
  34. dcm[0, 0] = 2 * q0 ** 2 - 1 + 2 * q1 ** 2
  35. dcm[1, 1] = 2 * q0 ** 2 - 1 + 2 * q2 ** 2
  36. dcm[2, 2] = 2 * q0 ** 2 - 1 + 2 * q3 ** 2
  37.  
  38. dcm[0, 1] = 2 * q1 * q2 + 2 * q0 * q3
  39. dcm[0, 2] = 2 * q1 * q3 - 2 * q0 * q2
  40.  
  41. dcm[1, 0] = 2 * q1 * q2 - 2 * q0 * q3
  42. dcm[1, 2] = 2 * q2 * q3 + 2 * q0 * q1
  43.  
  44. dcm[2, 0] = 2 * q1 * q3 + 2 * q0 * q2
  45. dcm[2, 1] = 2 * q2 * q3 - 2 * q0 * q1
  46.  
  47. return dcm
  48.  
  49. # Rotate points in body frame
  50. def rotate(points, q):
  51. k = np.transpose(quat2dcm(q)).dot(np.transpose(points))
  52. return np.transpose(np.transpose(quat2dcm(q)).dot(np.transpose(points)))
  53.  
  54. # translate in body frame
  55. def translate(points, r):
  56. return points + r
  57.  
  58. def ortho(points, q):
  59. X = np.transpose(quat2dcm(q)).dot(np.transpose(points)[:3])
  60. x, y = (X[0], X[1])
  61. return x, y
  62.  
  63. def pinhole(points, q, r):
  64.  
  65. hom_points = np.hstack([points, np.ones((points.shape[0], 1))])
  66. points_body = np.transpose(hom_points)
  67. # points_body[:-1, :] = np.multiply(points_body[:-1, :], scale) # scaling homogeneous coordinates
  68.  
  69. # transformation to camera frame
  70. poseMat = np.hstack((np.transpose(quat2dcm(q)), np.expand_dims(r, 1)))
  71. p_cam = np.dot(poseMat, points_body)
  72.  
  73. # getting homogeneous coordinates
  74. X = p_cam / p_cam[2]
  75.  
  76. # projection to image plane
  77. X = K.dot(X)
  78. x, y = (X[0], X[1])
  79. return x, y
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement