Advertisement
Guest User

Untitled

a guest
Oct 29th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. import pyrealsense2 as rs
  2. import numpy as np
  3. import cv2
  4.  
  5.  
  6. # mouse callback function to display distance on double click
  7. def get_distance(event,x,y,flags,param):
  8. if event == cv2.EVENT_LBUTTONDBLCLK:
  9.  
  10. depth = frames.get_depth_frame()
  11. color = frames.get_color_frame()
  12. pixel_distance_in_meters = depth.get_distance(int(x),int(y))
  13.  
  14. print(pixel_distance_in_meters)
  15.  
  16.  
  17.  
  18.  
  19. return;
  20.  
  21.  
  22. # Configure depth and color streams
  23. pipeline = rs.pipeline()
  24.  
  25. config = rs.config()
  26.  
  27.  
  28. # Start streaming
  29. profile = pipeline.start(config)
  30.  
  31. ctx = rs.context()
  32.  
  33. if len(ctx.devices) > 0:
  34. for d in ctx.devices:
  35. print ('Found device: ',
  36. d.get_info(rs.camera_info.name), ' ',
  37. d.get_info(rs.camera_info.serial_number))
  38. else:
  39. print("No Intel Device connected")
  40.  
  41. try:
  42. while True:
  43.  
  44. # Wait for a coherent pair of frames: depth and color
  45. frames = pipeline.wait_for_frames()
  46. depth_frame = frames.get_depth_frame()
  47. color_frame = frames.get_color_frame()
  48. points = rs.points
  49. pc = rs.pointcloud
  50. pc.map_to(color_frame)
  51. points = pc.calculate(depth_frame)
  52. print("Saving to 1.ply...")
  53. points.export_to_ply("1.ply", color_frame)
  54. print("Done")
  55.  
  56.  
  57. if not depth_frame or not color_frame:
  58. continue
  59.  
  60. # Convert images to numpy arrays
  61. depth_image = np.asanyarray(depth_frame.get_data())
  62. color_image = np.asanyarray(color_frame.get_data())
  63.  
  64.  
  65. # Apply colormap on depth image (image must be converted to 8-bit per pixel first)
  66. depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)
  67.  
  68. # Stack both images horizontally
  69. images = depth_colormap
  70.  
  71. # Show images
  72. cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)
  73. cv2.setMouseCallback('RealSense',get_distance)
  74. cv2.imshow('RealSense', images)
  75. cv2.waitKey(1)
  76.  
  77. finally:
  78.  
  79. # Stop streaming
  80. pipeline.stop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement