Advertisement
nlw

Perspective projection / camera model

nlw
Mar 13th, 2012
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.41 KB | None | 0 0
  1. from pylab import *
  2. from quaternion import Quat
  3.  
  4. ## 3D coordinates of our points in the "world" reference frame.
  5.  
  6. ## This is a cube, plus the origin
  7. r = array([
  8.     [-1,-1,-1],[-1,-1, 1],[-1, 1,-1],[-1, 1, 1],
  9.     [ 1,-1,-1],[ 1,-1, 1],[ 1, 1,-1],[ 1, 1, 1],
  10.     [0,0,0]])
  11.  
  12. ## The camera position (translation vector)
  13. t = array([1.5,0.5,-4.0])
  14. ## The camera orientation, as a quaternion. We caalculate is so it
  15. ## points to the origin, and is "upright"
  16. psi = Quat(0,sin(arctan2(t[0],t[2])/2),0) * \
  17.       Quat(sin(arctan2(t[1],sqrt(t[0]**2+t[2]**2))/2),0,0)
  18. ## Rotation matrix from the quaternion
  19. R = psi.rot()
  20.  
  21. ## Pontos no referencial da camera
  22. rc = dot(r-t, R)
  23.  
  24. ## Intrinsic parameters
  25. oc = array([320,240]) ## Projection and distortion center.
  26. fd = 600          ## focal distance
  27.  
  28. ## Apply perspective distortion
  29. q = rc[:,:2] * fd / rc[:,[2,2]]
  30.  
  31. ## Calculate final image coordinates,
  32. rho2 = (q[:,0]**2 + q[:,1]**2)
  33. kappa = 0.0
  34. p = q / c_[2*[1.0/sqrt(1+kappa*rho2)]].T + oc
  35.  
  36. ion()
  37. subplot(1,2,1)
  38. title('Points in camera reference frame')
  39. plot(rc[:,0], rc[:,1], 'b+')
  40. axis('equal')
  41. grid()
  42.  
  43. ## Plot projected points
  44. subplot(1,2,2)
  45. title('Projected coordinates on the image plane')
  46. plot(p[:,0], p[:,1], 'b+')
  47. ## Plot the cube edges
  48. conn = [[0,1],[0,2],[0,4],[1,3],[1,5],[2,3],
  49.         [2,6],[3,7],[4,5],[4,6],[5,7],[6,7]]
  50. for cc in conn:
  51.     plot(p[cc,0], p[cc,1], 'b-')
  52. axis('equal')
  53. grid()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement