# ToF All Queues

This example shows all of the main ToF output queues at once, displaying `depth`, `amplitude`, and `intensity` on both platforms,
plus the platform-specific `rawDepth` (RVC2) or `confidence` (RVC4) output. The detected platform is printed at startup.For the
full output/architecture reference, 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).

## Source code

#### Python

```python
#!/usr/bin/env python3
"""Simple ToF script showing all main ToF output queues.

RVC2 displays: depth, amplitude, intensity, rawDepth.
RVC4 displays: depth, amplitude, intensity, confidence.

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 normalizeFrame(frame: np.ndarray) -> np.ndarray:
    return cv2.normalize(frame, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)

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
    )

    with pipeline as p:
        device = p.getDefaultDevice()
        isRVC2 = device.getPlatform() == dai.Platform.RVC2

        outputQueues = {
            "depth": tof.depth.createOutputQueue(maxSize=1, blocking=False),
            "amplitude": tof.amplitude.createOutputQueue(maxSize=1, blocking=False),
            "intensity": tof.intensity.createOutputQueue(maxSize=1, blocking=False),
        }
        if isRVC2:
            # rawDepth are only supported on RVC2
            outputQueues["rawDepth"] = tof.rawDepth.createOutputQueue(maxSize=1, blocking=False)
        else:
            # confidence is only supported on RVC4
            outputQueues["confidence"] = tof.confidence.createOutputQueue(maxSize=1, blocking=False)

        platformName = "RVC2" if isRVC2 else "RVC4"
        print(f"Detected {platformName} - showing queues: {', '.join(outputQueues)}")

        p.start()
        while p.isRunning():
            for name, queue in outputQueues.items():
                frame = queue.tryGet()
                if frame is None:
                    continue

                if name in {"depth", "rawDepth"}:
                    display = colorizeDepth(frame.getCvFrame(), minDepth, maxDepth)
                else:
                    display = normalizeFrame(frame.getCvFrame())
                cv2.imshow(name, display)

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

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

#### C++

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

#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);
    }
}

cv::Mat normalizeFrame(const cv::Mat& frame) {
    cv::Mat normalized;
    cv::normalize(frame, normalized, 0, 255, cv::NORM_MINMAX, CV_8U);
    return normalized;
}

int main() {
    dai::Pipeline pipeline;

    // show depth in range 0.1m - 7m
    constexpr float minDepth = 100.0f;
    constexpr float maxDepth = 7000.0f;

    // choose one of profiles LOW_RANGE / MID_RANGE / HIGH_RANGE
    auto profile = dai::ToFConfig::Profile::MID_RANGE;

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

    std::map<std::string, std::shared_ptr<dai::MessageQueue>> outputQueues = {
        {"depth", tof->depth.createOutputQueue(1, false)},
        {"amplitude", tof->amplitude.createOutputQueue(1, false)},
        {"intensity", tof->intensity.createOutputQueue(1, false)},
        // {"rawDepth", tof->rawDepth.createOutputQueue(1, false)},  // not supported on RVC4
        // {"confidence", tof->confidence.createOutputQueue(1, false)},  // not supported on RVC2
    };

    pipeline.start();
    while(pipeline.isRunning()) {
        for(const auto& [name, queue] : outputQueues) {
            auto frame = queue->tryGet<dai::ImgFrame>();
            if(frame == nullptr) {
                continue;
            }

            cv::Mat displayFrame;

            if(name == "depth" || name == "rawDepth") {
                displayFrame = colorizeDepth(frame->getCvFrame(), minDepth, maxDepth);
            } else {
                displayFrame = normalizeFrame(frame->getCvFrame());
            }

            cv::imshow(name, displayFrame);
        }

        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.
