# Neural Depth Minimal

Minimal example showing basic [NeuralDepth](https://docs.luxonis.com/software-v3/depthai/depthai-components/nodes/neural_depth.md)
usage with disparity output visualization.

## Pipeline

## Source code

#### Python

```python
#!/usr/bin/env python3

import cv2
import depthai as dai
import numpy as np

FPS = 25

# Create pipeline
with dai.Pipeline() as pipeline:
    cameraLeft = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_B, sensorFps=FPS)
    cameraRight = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_C, sensorFps=FPS)
    leftOutput = cameraLeft.requestFullResolutionOutput()
    rightOutput = cameraRight.requestFullResolutionOutput()

    neuralDepth = pipeline.create(dai.node.NeuralDepth).build(leftOutput, rightOutput, dai.DeviceModelZoo.NEURAL_DEPTH_192X120)

    disparityQueue = neuralDepth.disparity.createOutputQueue()

    # Connect to device and start pipeline
    pipeline.start()
    maxDisparity = 1
    colorMap = cv2.applyColorMap(np.arange(256, dtype=np.uint8), cv2.COLORMAP_JET)
    colorMap[0] = [0, 0, 0]  # to make zero-disparity pixels black

    while pipeline.isRunning():
        disparityData = disparityQueue.get()
        assert isinstance(disparityData, dai.ImgFrame)
        npDisparity = disparityData.getFrame()
        maxDisparity = max(maxDisparity, np.max(npDisparity))
        colorizedDisparity = cv2.applyColorMap(((npDisparity / maxDisparity) * 255).astype(np.uint8), colorMap)
        cv2.imshow("disparity", colorizedDisparity)

        key = cv2.waitKey(1)
        if key == ord('q'):
            pipeline.stop()
            break

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

#### C++

```cpp
#include <atomic>
#include <csignal>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <vector>

#include "depthai/depthai.hpp"

// Global flag to allow for a graceful shutdown
std::atomic<bool> quitEvent(false);

void signalHandler(int signum) {
    quitEvent = true;
}

int main() {
    // Set up signal handlers for clean exit on Ctrl+C
    signal(SIGTERM, signalHandler);
    signal(SIGINT, signalHandler);

    constexpr float FPS = 25.0f;

    // Create the DepthAI pipeline
    dai::Pipeline pipeline;

    // Define camera sources for the stereo pair
    auto cameraLeft = pipeline.create<dai::node::Camera>();
    cameraLeft->build(dai::CameraBoardSocket::CAM_B, std::nullopt, FPS);

    auto cameraRight = pipeline.create<dai::node::Camera>();
    cameraRight->build(dai::CameraBoardSocket::CAM_C, std::nullopt, FPS);

    // Request full resolution output from each camera
    auto* leftOutput = cameraLeft->requestFullResolutionOutput();
    auto* rightOutput = cameraRight->requestFullResolutionOutput();

    // Create and build the NeuralDepth node, linking the camera outputs to it
    auto neuralDepth = pipeline.create<dai::node::NeuralDepth>();
    neuralDepth->build(*leftOutput, *rightOutput, dai::DeviceModelZoo::NEURAL_DEPTH_LARGE);

    // Create an output queue to get the disparity frames from the node
    auto disparityQueue = neuralDepth->disparity.createOutputQueue();

    // Start the pipeline
    pipeline.start();

    // Variables for visualization
    double maxDisparity = 1.0;
    cv::Mat colorMap;

    // Pre-generate the color map for efficiency
    cv::Mat gray(256, 1, CV_8UC1);
    for(int i = 0; i < 256; i++) {
        gray.at<uchar>(i) = i;
    }
    cv::applyColorMap(gray, colorMap, cv::COLORMAP_JET);
    // Set the color for zero-disparity pixels to black, as in the Python example
    colorMap.at<cv::Vec3b>(0) = cv::Vec3b(0, 0, 0);

    while(!quitEvent && pipeline.isRunning()) {
        // Get the disparity data from the queue
        auto disparityData = disparityQueue->get<dai::ImgFrame>();
        cv::Mat npDisparity = disparityData->getFrame();

        // Find the current maximum disparity value to keep the visualization normalized
        double minVal, currentMax;
        cv::minMaxLoc(npDisparity, &minVal, &currentMax);
        if(currentMax > 0) {
            maxDisparity = std::max(maxDisparity, currentMax);
        }

        // Normalize the disparity image to a 0-255 scale for color mapping
        cv::Mat normalized;
        npDisparity.convertTo(normalized, CV_8UC1, 255.0 / maxDisparity);

        // Apply the color map to create a visual representation
        cv::Mat colorizedDisparity;
        cv::applyColorMap(normalized, colorizedDisparity, colorMap);

        // Display the colorized disparity map
        cv::imshow("disparity", colorizedDisparity);

        // Check for keyboard input to quit
        int key = cv::waitKey(1);
        if(key == 'q') {
            break;
        }
    }

    // The pipeline is stopped automatically when the 'pipeline' object goes out of scope
    // at the end of the main function.
    return 0;
}
```

### Need assistance?

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