一、环境

pycharm+OpenGL+opengame

二、静态及动态增强现实实现

(一)静态(立方体和茶壶)

    1.立方体的实现

     将静态立方体放到键盘上

     效果图:

       Python实现静态和动态增强现实

              (a)

       Python实现静态和动态增强现实

             (b)

       Python实现静态和动态增强现实

             (c)

             图 一

      代码:

      

  1 from pylab import *
  2 from PIL import Image
  3 # If you have PCV installed, these imports should work
  4 from PCV.geometry import homography, camera
  5 from PCV.localdescriptors import sift
  6 
  7 """
  8 This is the augmented reality and pose estimation cube example from Section 4.3.
  9 """
 10 
 11 def draw_teapot(size):
 12     glEnable(GL_LIGHTING)
 13     glEnable(GL_LIGHT0)
 14 def cube_points(c, wid):
 15     """ Creates a list of points for plotting
 16         a cube with plot. (the first 5 points are
 17         the bottom square, some sides repeated). """
 18     p = []
 19     # bottom
 20     p.append([c[0]-wid, c[1]-wid, c[2]-wid])
 21     p.append([c[0]-wid, c[1]+wid, c[2]-wid])
 22     p.append([c[0]+wid, c[1]+wid, c[2]-wid])
 23     p.append([c[0]+wid, c[1]-wid, c[2]-wid])
 24     p.append([c[0]-wid, c[1]-wid, c[2]-wid]) #same as first to close plot
 25     
 26     # top
 27     p.append([c[0]-wid, c[1]-wid, c[2]+wid])
 28     p.append([c[0]-wid, c[1]+wid, c[2]+wid])
 29     p.append([c[0]+wid, c[1]+wid, c[2]+wid])
 30     p.append([c[0]+wid, c[1]-wid, c[2]+wid])
 31     p.append([c[0]-wid, c[1]-wid, c[2]+wid]) #same as first to close plot
 32     
 33     # vertical sides
 34     p.append([c[0]-wid, c[1]-wid, c[2]+wid])
 35     p.append([c[0]-wid, c[1]+wid, c[2]+wid])
 36     p.append([c[0]-wid, c[1]+wid, c[2]-wid])
 37     p.append([c[0]+wid, c[1]+wid, c[2]-wid])
 38     p.append([c[0]+wid, c[1]+wid, c[2]+wid])
 39     p.append([c[0]+wid, c[1]-wid, c[2]+wid])
 40     p.append([c[0]+wid, c[1]-wid, c[2]-wid])
 41     
 42     return array(p).T
 43 
 44 
 45 def my_calibration(sz):
 46     """
 47     Calibration function for the camera (iPhone4) used in this example.
 48     """
 49     row, col = sz
 50     fx = 2555*col/2592
 51     fy = 2586*row/1936
 52     K = diag([fx, fy, 1])
 53     K[0, 2] = 0.5*col
 54     K[1, 2] = 0.5*row
 55     return K
 56 
 57 
 58 
 59 # compute features
 60 sift.process_image('1.png', 'im0.sift')
 61 l0, d0 = sift.read_features_from_file('im0.sift')
 62 
 63 sift.process_image('1.png', 'im1.sift')
 64 l1, d1 = sift.read_features_from_file('im1.sift')
 65 
 66 
 67 # match features and estimate homography
 68 matches = sift.match_twosided(d0, d1)
 69 ndx = matches.nonzero()[0]
 70 fp = homography.make_homog(l0[ndx, :2].T)
 71 ndx2 = [int(matches[i]) for i in ndx]
 72 tp = homography.make_homog(l1[ndx2, :2].T)
 73 
 74 model = homography.RansacModel()
 75 H, inliers = homography.H_from_ransac(fp, tp, model)
 76 
 77 # camera calibration
 78 K = my_calibration((747, 1000))
 79 
 80 # 3D points at plane z=0 with sides of length 0.2
 81 box = cube_points([0, 0, 0.1], 0.1)
 82 
 83 # project bottom square in first image
 84 cam1 = camera.Camera(hstack((K, dot(K, array([[0], [0], [-1]])))))
 85 # first points are the bottom square
 86 box_cam1 = cam1.project(homography.make_homog(box[:, :5]))
 87 
 88 
 89 # use H to transfer points to the second image
 90 box_trans = homography.normalize(dot(H,box_cam1))
 91 
 92 # compute second camera matrix from cam1 and H
 93 cam2 = camera.Camera(dot(H, cam1.P))
 94 A = dot(linalg.inv(K), cam2.P[:, :3])
 95 A = array([A[:, 0], A[:, 1], cross(A[:, 0], A[:, 1])]).T
 96 cam2.P[:, :3] = dot(K, A)
 97 
 98 # project with the second camera
 99 box_cam2 = cam2.project(homography.make_homog(box))
100 
101 
102 
103 # plotting
104 im0 = array(Image.open('1.png'))
105 im1 = array(Image.open('2.png'))
106 
107 figure()
108 imshow(im0)
109 plot(box_cam1[0, :], box_cam1[1, :], linewidth=3)
110 title('2D projection of bottom square')
111 axis('off')
112 
113 figure()
114 imshow(im1)
115 plot(box_trans[0, :], box_trans[1, :], linewidth=3)
116 title('2D projection transfered with H')
117 axis('off')
118 
119 figure()
120 imshow(im1)
121 plot(box_cam2[0, :], box_cam2[1, :], linewidth=3)
122 title('3D points projected in second image')
123 axis('off')
124 
125 show()
立方体

相关文章:

  • 2021-06-18
  • 2021-09-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-16
  • 2021-07-15
猜你喜欢
  • 2021-06-13
  • 2022-01-03
  • 2021-12-22
  • 2022-02-07
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案