Obtaining distance of point with mouse click

I am trying to display the depth image, similar to the Depth Viewer and be able to print the distance to the terminal at any point with mouse click.

When I attempt following the OpenCV tutorial page, I get the following error (I think it is outdated): AttributeError: 'pyzed.sl.CameraInformation' object has no attribute 'camera_resolution'

I have made better progress using the following code:

import cv2
import pyzed.sl as sl

# Create a ZED camera object
zed = sl.Camera()

# Set configuration parameters
init_params = sl.InitParameters()
init_params.camera_resolution = sl.RESOLUTION.HD720
init_params.coordinate_units = sl.UNIT.MILLIMETER
#init_params.depth_mode = sl.DEPTH_MODE.NEURAL

# Open the camera
err = zed.open(init_params)
if err != sl.ERROR_CODE.SUCCESS:
    print("Failed to open ZED camera")

# Create and set RuntimeParameters after opening the camera
runtime_parameters = sl.RuntimeParameters()

# Create an OpenCV window
cv2.namedWindow("Depth Map")

# Mouse click event handler
def onMouseClick(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDOWN:
        depth_map = param
        depth_value = depth_map.get_value(x, y)
        print("Depth value at mouse click: {} millimeters".format(depth_value))

# Main loop
while True:
    # Capture a new frame
    if zed.grab() == sl.ERROR_CODE.SUCCESS:
        # Retrieve depth map
        depth_map = sl.Mat()
        zed.retrieve_measure(depth_map, sl.MEASURE.DEPTH)

        # Display depth map
        depth_image = depth_map.get_data()
        cv2.imshow("Depth Map", depth_image)

        # Handle mouse click event
        cv2.setMouseCallback("Depth Map", onMouseClick, depth_map)

        # Wait for key press
        if cv2.waitKey(10) == 27:

# Close the camera

The code launches a depth map that is difficult to see but the mouse click event returns a working distance. When I set the initalisation parameter of the resolution to HD2K the display is even more white. How can I display a better depth map and also show the same colour depth map displayed by the Depth Viewer?


Having the same color scheme as we have in depth viewer is not possible directly from the SDK, however you can improve the depth by changing what you display in imshow, using retrieveimage with the DEPTH view.

        dispdepth = sl.Mat()

        # Display depth map
        #depth_image = depth_map.get_data()
        cv2.imshow("Depth Map", dispdepth.get_data())

Awesome, thanks @JPlou !! :raised_hands:

1 Like