title: Exact depth distance from Realsense D435 with X,Y coordinates
body: <p>I would like to calculate the depth distance with the x,y coordinates of a detect object. </p>
  
  <p>In the image below, I used background subtraction in opencv to detect and track a new object that has entered in the cameras view. I am able to get the x,y coordinate with relative ease but have trouble getting the z depth with the realsense sdk. Is it possible in any way for me to input the x,y coordinates into the realsense sdk and get the z depth from there? </p>
  
  <p>I'm using opencv python and realsense sdk 2 for reference. </p>
  
  <p><a href="https://i.stack.imgur.com/UC0VC.png" rel="nofollow noreferrer">Getting the z depth in the midpoint of the bounding box</a></p>
  
  <pre><code>import numpy as np
  import cv2 as cv
  import pyrealsense2 as rs
  
  # Create a pipeline
  pipeline = rs.pipeline()
  config = rs.config()
  config.enable_stream(rs.stream.depth, 1280, 720, rs.format.z16, 30)
  config.enable_stream(rs.stream.color, 1280, 720, rs.format.bgr8, 30)
  
  #Start pipeline
  profile = pipeline.start(config)
  
  erodeKernel = cv.getStructuringElement(cv.MORPH_RECT, (5,5))
  
  fgbg = cv.createBackgroundSubtractorMOG2()
  while True:
      frames = pipeline.wait_for_frames()
      depth_frame = frames.get_depth_frame()
      colour_frame = frames.get_color_frame()
  
      color_image = np.asanyarray(colour_frame.get_data())
      depth_image = np.asanyarray(depth_frame.get_data())
  
      # Apply colormap on depth image (image must be converted to 8-bit per pixel first)
      depth_colormap = cv.applyColorMap(cv.convertScaleAbs(depth_image, alpha=0.03), cv.COLORMAP_JET)
  
      blur = cv.GaussianBlur(color_image,(5,5),0)
      fgmask = fgbg.apply(blur)
  
      im2, contours, hierarchy = cv.findContours(fgmask.copy(), cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
      for c in contours:
          if cv.contourArea(c) &lt; 200:
              continue
  
          (x,y,w,h) = cv.boundingRect(c)
          cv.rectangle(color_image, (x,y), (x + w, y + h), (0,255,0), 2)
  
      cv.imshow('RealSense', color_image)
      cv.imshow("Depth", depth_colormap)
      cv.imshow('Mask', fgmask)
      if cv.waitKey(25) == ord('q'):
          break
  cv.destroyAllWindows()
  pipeline.stop()
  </code></pre>