DepthAI
  • DepthAI Components
    • AprilTags
    • Benchmark
    • Camera
    • Calibration
    • DetectionNetwork
    • Events
    • FeatureTracker
    • Gate
    • HostNodes
    • ImageAlign
    • ImageManip
    • IMU
    • Misc
    • Model Zoo
    • NeuralDepth
    • NeuralNetwork
    • ObjectTracker
    • RecordReplay
    • RGBD
    • Script
    • SpatialDetectionNetwork
    • SpatialLocationCalculator
    • StereoDepth
    • Sync
    • VideoEncoder
    • Visualizer
    • Warp
    • RVC2-specific
      • EdgeDetector
      • NNArchive
      • SystemLogger
      • Thermal
      • Tof
      • VSLAM
  • Advanced Tutorials
  • API Reference
  • Tools
Software Stack

ON THIS PAGE

  • ToF
  • Pipeline
  • Source code

ToF

Supported on:RVC2
This RVC2 example shows a minimal ToF pipeline and visualizes depth with a colormap.
Unlike classic color + mono stereo-pair examples, ToF depth is produced through dai.node.ToF.
For architecture and settings, see the ToF node docs.
For a full runtime-tuning and pointcloud workflow, see ToF pointcloud + runtime filter controls (oak-examples).
This example requires the DepthAI v3 API, see installation instructions.

Pipeline

Source code

Python
C++

Python

Python
GitHub
1#!/usr/bin/env python3
2
3import cv2
4import depthai as dai
5import numpy as np
6
7
8def colorizeDepth(frameDepth: np.ndarray) -> np.ndarray:
9    invalidMask = frameDepth == 0  # zero depth is invalid
10
11    # Log the depth, minDepth and maxDepth
12    try:
13        minDepth = np.percentile(frameDepth[frameDepth != 0], 3)
14        maxDepth = np.percentile(frameDepth[frameDepth != 0], 95)
15        logDepth = np.log(frameDepth, where=frameDepth != 0)
16        logMinDepth = np.log(minDepth)
17        logMaxDepth = np.log(maxDepth)
18        np.nan_to_num(logDepth, copy=False, nan=logMinDepth)
19        # Clip the values to be in the 0-255 range
20        logDepth = np.clip(logDepth, logMinDepth, logMaxDepth)
21
22        # Interpolate only valid logDepth values, setting the rest based on the mask
23        depthFrameColor = np.interp(logDepth, (logMinDepth, logMaxDepth), (0, 255))
24        depthFrameColor = np.nan_to_num(depthFrameColor)
25        depthFrameColor = depthFrameColor.astype(np.uint8)
26        depthFrameColor = cv2.applyColorMap(depthFrameColor, cv2.COLORMAP_JET)
27        # Set invalid depth pixels to black
28        depthFrameColor[invalidMask] = 0
29    except IndexError:
30        # Frame is likely empty
31        depthFrameColor = np.zeros(
32            (frameDepth.shape[0], frameDepth.shape[1], 3), dtype=np.uint8
33        )
34    except Exception as e:
35        raise e
36    return depthFrameColor
37
38
39def main():
40    pipeline = dai.Pipeline()
41
42    # ToF node
43    socket, preset_mode = dai.CameraBoardSocket.AUTO, dai.ImageFiltersPresetMode.TOF_MID_RANGE
44    tof = pipeline.create(dai.node.ToF).build(socket, preset_mode)
45
46    # Output queues
47    depthQueue = tof.depth.createOutputQueue()
48    depthRawQueue = tof.rawDepth.createOutputQueue()
49
50    with pipeline as p:
51        p.start()
52        while p.isRunning():
53            ## Visualize raw depth (unfiltered depth directly from the ToF sensor)
54            depthRaw: dai.ImgFrame = depthRawQueue.get()
55            depthRawImage = colorizeDepth(depthRaw.getFrame())
56            cv2.imshow("depthRaw", depthRawImage)
57
58            ## Visualize depth (which is filtered depthRaw)
59            depth: dai.ImgFrame = depthQueue.get()
60            depthImage = colorizeDepth(depth.getFrame())
61            cv2.imshow("depth", depthImage)
62
63            if cv2.waitKey(1) == ord("q"):
64                break
65
66
67if __name__ == "__main__":
68    main()

Need assistance?

Head over to Discussion Forum for technical support or any other questions you might have.