Ledger Nano X - The secure hardware wallet
SHARE
TWEET

Untitled

a guest Jan 9th, 2018 263 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2.  
  3. '''
  4. Simple example of stereo image matching and point cloud generation.
  5. Resulting .ply file cam be easily viewed using MeshLab ( http://meshlab.sourceforge.net/ )
  6. '''
  7.  
  8. # Python 2/3 compatibility
  9. from __future__ import print_function
  10.  
  11. import numpy as np
  12. import cv2 as cv
  13.  
  14. right = cv.VideoCapture(1)
  15. left = cv.VideoCapture(0)
  16.  
  17. OffsetX = 0.0
  18. OffsetY = -13.3265386728
  19. ply_header = '''ply
  20. format ascii 1.0
  21. element vertex %(vert_num)d
  22. property float x
  23. property float y
  24. property float z
  25. property uchar red
  26. property uchar green
  27. property uchar blue
  28. end_header
  29. '''
  30.  
  31. def rotateImage(image, angle):
  32.     (h, w) = image.shape[:2]
  33.     center = (w / 2, h / 2)
  34.     M = cv.getRotationMatrix2D(center, angle, 1.0)
  35.     rotated = cv.warpAffine(image, M, (w, h))
  36.     return rotated
  37.  
  38. def imageoffset(image, offsetx, offsety):
  39.     img = image
  40.     rows, cols = img.shape[:2]
  41.     M = np.float32([[1, 0, offsetx], [0, 1, offsety]])
  42.     dst = cv.warpAffine(img, M, (cols, rows))
  43.     #cv2.imshow("dst", dst)
  44.     return dst
  45. def rotateImage(image, angle):
  46.     (h, w) = image.shape[:2]
  47.     center = (w / 2, h / 2)
  48.     M = cv.getRotationMatrix2D(center, angle, 1.0)
  49.     rotated = cv.warpAffine(image, M, (w, h))
  50.     return rotated
  51.  
  52. def imageoffset(image, offsetx, offsety):
  53.     img = image
  54.     rows, cols = img.shape[:2]
  55.     M = np.float32([[1, 0, offsetx], [0, 1, offsety]])
  56.     dst = cv.warpAffine(img, M, (cols, rows))
  57.     #cv2.imshow("dst", dst)
  58.     return dst
  59. def get_image1():
  60.  retval, im = left.read()
  61.  return im
  62. def get_image2():
  63.  retval, im = right.read()
  64.  im1 = rotateImage(im, 180)
  65.  return im1
  66.  
  67.  
  68. def write_ply(fn, verts, colors):
  69.     verts = verts.reshape(-1, 3)
  70.     colors = colors.reshape(-1, 3)
  71.     verts = np.hstack([verts, colors])
  72.     with open(fn, 'wb') as f:
  73.         f.write((ply_header % dict(vert_num=len(verts))).encode('utf-8'))
  74.         np.savetxt(f, verts, fmt='%f %f %f %d %d %d ')
  75.  
  76.  
  77. if __name__ == '__main__':
  78.     while True:
  79.         imgL = get_image1()
  80.         imgR = get_image2()
  81.         imgR = imageoffset(imgR, OffsetX, OffsetY)
  82.  
  83.         # disparity range is tuned for 'aloe' image pair
  84.         window_size = 2
  85.         min_disp = (0 * 16)
  86.         num_disp = (12 * 16)-min_disp
  87.         stereo = cv.StereoSGBM_create(minDisparity = min_disp,
  88.             numDisparities = num_disp,
  89.             blockSize = 5,
  90.             P1 = 8*3*window_size**2,
  91.             P2 = 32*3*window_size**2,
  92.             disp12MaxDiff = 10,
  93.             uniquenessRatio = 10,
  94.             speckleWindowSize = 1000,
  95.             speckleRange = 32
  96.         )
  97.  
  98.         disp = stereo.compute(imgL, imgR).astype(np.float32) / 16.0
  99.         if False != False:
  100.             print('generating 3d point cloud...',)
  101.             h, w = imgL.shape[:2]
  102.             f = 0.8*w                          # guess for focal length
  103.             Q = np.float32([[1, 0, 0, -0.5*w],
  104.                             [0,-1, 0,  0.5*h], # turn points 180 deg around x-axis,
  105.                             [0, 0, 0,     -f], # so that y-axis looks up
  106.                             [0, 0, 1,      0]])
  107.             points = cv.reprojectImageTo3D(disp, Q)
  108.             colors = cv.cvtColor(imgL, cv.COLOR_BGR2RGB)
  109.             mask = disp > disp.min()
  110.             out_points = points[mask]
  111.             out_colors = colors[mask]
  112.             out_fn = 'out.ply'
  113.             write_ply('out.ply', out_points, out_colors)
  114.             print('%s saved' % 'out.ply')
  115.  
  116.         cv.imshow('left', imgL)
  117.         cv.imshow('disparity', (disp-min_disp)/num_disp)
  118.         cv.waitKey(5)
  119. cv.destroyAllWindows()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top