# ToF Minimal

This example shows a minimal ToF pipeline and visualizes depth with a colormap.For architecture and settings, see the [ToF node
docs](https://docs.luxonis.com/software-v3/depthai/depthai-components/nodes/tof.md).

This example requires the DepthAI v3 API, see [installation instructions](https://docs.luxonis.com/software-v3/depthai.md).

## Pipeline

## Source code

#### Python

```python
#!/usr/bin/env python3
"""Minimal ToF script showing the main output stream.

Displays depth.
For more streams, see tof_all_queues.py.

Press 'q' to quit.
"""

import cv2
import numpy as np
import depthai as dai

def colorizeDepth(frame: np.ndarray, minDepth: float, maxDepth: float) -> np.ndarray:
    invalidMask = frame == 0
    try:
        logDepth = np.log(frame.astype(np.float32) + 1e-6)
        logDepth[invalidMask] = 0.0
        logDepth = np.clip(logDepth, np.log(minDepth + 1e-6), np.log(maxDepth + 1e-6))
        colored = np.interp(logDepth, (logDepth[~invalidMask].min(), logDepth[~invalidMask].max()), (0, 255))
        colored = colored.astype(np.uint8)
        colored = cv2.applyColorMap(colored, cv2.COLORMAP_JET)
        colored[invalidMask] = 0
    except (IndexError, ValueError):
        colored = np.zeros((*frame.shape, 3), dtype=np.uint8)
    return colored

def main():
    pipeline = dai.Pipeline()

    # show depth in range 0.1m - 7m
    minDepth = 100
    maxDepth = 7000

    # choose one of profiles LOW_RANGE / MID_RANGE / HIGH_RANGE
    profile = dai.ToFConfig.Profile.MID_RANGE

    tof = pipeline.create(dai.node.ToF).build(
        boardSocket=dai.CameraBoardSocket.AUTO,
        profile=profile
    )

    depthOutputQueue = tof.depth.createOutputQueue()

    with pipeline as p:
        p.start()
        while p.isRunning():
            depth = depthOutputQueue.get()
            cv2.imshow("depth", colorizeDepth(depth.getCvFrame(), minDepth, maxDepth))

            if cv2.waitKey(1) == ord("q"):
                break

if __name__ == "__main__":
    main()
```

#### C++

```cpp
#include <cmath>
#include <opencv2/opencv.hpp>

#include "depthai/depthai.hpp"

cv::Mat colorizeDepth(const cv::Mat& frame, float minDepth, float maxDepth) {
    cv::Mat depth32f;
    frame.convertTo(depth32f, CV_32F);

    cv::Mat invalidMask = depth32f == 0.0f;

    try {
        cv::Mat logDepth = depth32f + 1e-6f;
        cv::log(logDepth, logDepth);
        logDepth.setTo(0.0f, invalidMask);

        const float logMinDepth = std::log(minDepth + 1e-6f);
        const float logMaxDepth = std::log(maxDepth + 1e-6f);

        cv::min(logDepth, logMaxDepth, logDepth);
        cv::max(logDepth, logMinDepth, logDepth);

        cv::Mat validMask = invalidMask == 0;
        double validMin = 0.0;
        double validMax = 0.0;
        cv::minMaxLoc(logDepth, &validMin, &validMax, nullptr, nullptr, validMask);

        if(validMax <= validMin) {
            return cv::Mat::zeros(frame.size(), CV_8UC3);
        }

        cv::Mat colored;
        logDepth.convertTo(colored, CV_8U, 255.0 / (validMax - validMin), -validMin * 255.0 / (validMax - validMin));
        cv::applyColorMap(colored, colored, cv::COLORMAP_JET);
        colored.setTo(cv::Scalar::all(0), invalidMask);
        return colored;
    } catch(const cv::Exception&) {
        return cv::Mat::zeros(frame.size(), CV_8UC3);
    }
}

int main() {
    auto device = std::make_shared<dai::Device>();
    dai::Pipeline pipeline(device);

    // Show depth in range 0.1 m to 7 m.
    constexpr float minDepth = 100.0f;
    constexpr float maxDepth = 7000.0f;

    // Choose one of the profiles: LOW_RANGE, MID_RANGE, or HIGH_RANGE.
    auto profile = dai::ToFConfig::Profile::MID_RANGE;

    auto tof = pipeline.create<dai::node::ToF>()->build(dai::CameraBoardSocket::AUTO, profile);

    auto depthOutputQueue = tof->depth.createOutputQueue();

    pipeline.start();
    while(pipeline.isRunning()) {
        auto depth = depthOutputQueue->get<dai::ImgFrame>();
        cv::imshow("depth", colorizeDepth(depth->getCvFrame(), minDepth, maxDepth));

        if(cv::waitKey(1) == 'q') {
            break;
        }
    }

    return 0;
}
```

### Need assistance?

Head over to [Discussion Forum](https://discuss.luxonis.com/) for technical support or any other questions you might have.
