DepthAI v2 has been superseded by DepthAI v3. You are viewing legacy documentation.
DepthAI 教程
DepthAI API 参考

本页目录

  • Demo
  • 深度滤波器
  • 相似示例:
  • 设置
  • 源代码
  • 流水线

深度后处理

本示例展示了如何在设备本身上运行深度后处理滤波器,以减少噪点、平滑深度图并整体提升深度图质量。后处理可以添加到 StereoDepth 节点中。

Demo

software/depthai/examples/depth_comparison.webp

深度滤波器

中值滤波

这是一种非保边中值滤波器,可用于减少噪声并平滑深度图。中值滤波器由硬件实现,因此是最快的滤波器。

散斑滤波

散斑滤波器用于减少散斑噪声。散斑噪声是指相邻视差/深度像素之间存在巨大差异的区域,散斑滤波器尝试滤除这类区域。
class

depthai.StereoDepthConfig.PostProcessing.SpeckleFilter

method
property
differenceThreshold
Maximum difference between neighbor disparity pixels to put them into the same blob. Units in disparity integer levels.
method
property
enable
Whether to enable or disable the filter.
method
property
speckleRange
Speckle search range.
method

时域滤波

时域滤波器旨在通过基于先前帧操作每个像素值来提升深度数据的持久性。该滤波器对数据执行单次遍历,在调整深度值的同时更新跟踪历史。当像素数据缺失或无效时,滤波器使用用户定义的持久性模式来决定是否用存储的数据来修正缺失值。需要注意的是,由于依赖历史数据,该滤波器可能会引入明显的模糊/拖影伪影,因此最适合静态场景。
class

depthai.StereoDepthConfig.PostProcessing.TemporalFilter

class
PersistencyMode
Persistency algorithm type.  Members:    PERSISTENCY_OFF :     VALID_8_OUT_OF_8 :     VALID_2_IN_LAST_3 :     VALID_2_IN_LAST_4 :     VALID_2_OUT_OF_8 :     VALID_1_IN_LAST_2 :     VALID_1_IN_LAST_5 :     VALID_1_IN_LAST_8 :     PERSISTENCY_INDEFINITELY : 
method
property
alpha
The Alpha factor in an exponential moving average with Alpha=1 - no filter. Alpha = 0 - infinite filter. Determines the extent of the temporal history that should be averaged.
method
property
delta
Step-size boundary. Establishes the threshold used to preserve surfaces (edges). If the disparity value between neighboring pixels exceed the disparity threshold set by this delta parameter, then filtering will be temporarily disabled. Default value 0 means auto: 3 disparity integer levels. In case of subpixel mode it's 3*number of subpixel levels.
method
property
enable
Whether to enable or disable the filter.
method
property
persistencyMode
Persistency mode. If the current disparity/depth value is invalid, it will be replaced by an older value, based on persistency mode.
method
class

depthai.StereoDepthConfig.PostProcessing.TemporalFilter.PersistencyMode

variable
variable
variable
variable
variable
variable
variable
variable
variable
variable
method
method
method
method
method
method
method
method
method
method
property
property

空间滤波

空间保边滤波器会用有效的邻近深度像素填充无效的深度像素。它执行一系列一维水平和垂直遍历(迭代),以增强重建数据的平滑度。该滤波器基于这篇研究论文
class

depthai.StereoDepthConfig.PostProcessing.SpatialFilter

method
property
alpha
The Alpha factor in an exponential moving average with Alpha=1 - no filter. Alpha = 0 - infinite filter. Determines the amount of smoothing.
method
property
delta
Step-size boundary. Establishes the threshold used to preserve "edges". If the disparity value between neighboring pixels exceed the disparity threshold set by this delta parameter, then filtering will be temporarily disabled. Default value 0 means auto: 3 disparity integer levels. In case of subpixel mode it's 3*number of subpixel levels.
method
property
enable
Whether to enable or disable the filter.
method
property
holeFillingRadius
An in-place heuristic symmetric hole-filling mode applied horizontally during the filter passes. Intended to rectify minor artefacts with minimal performance impact. Search radius for hole filling.
method
property
numIterations
Number of iterations over the image in both horizontal and vertical direction.
method

阈值滤波

阈值滤波器会滤除所有超出配置的最小/最大阈值范围的视差/深度像素。
Python
1class ThresholdFilter:
2'''
3    空间保边滤波器会用有效的邻近深度像素填充无效的深度像素。
4    它执行一系列一维水平和垂直遍历(迭代),以增强重建数据的平滑度。
5    该滤波器基于[这篇研究论文](https://www.inf.ufrgs.br/~eslgastal/DomainTransform/)。
6'''

相似示例:

设置

请运行安装脚本以下载所有必需的依赖项。请注意,此脚本必须在 git 上下文中运行,因此您需要先下载 depthai-python 仓库,然后运行该脚本。
Command Line
1git clone https://github.com/luxonis/depthai-python.git
2cd depthai-python/examples
3python3 install_requirements.py
更多信息,请参考安装指南

源代码

Python

Python
GitHub
1#!/usr/bin/env python3
2
3import cv2
4import depthai as dai
5import numpy as np
6
7# Closer-in minimum depth, disparity range is doubled (from 95 to 190):
8extended_disparity = False
9# Better accuracy for longer distance, fractional disparity 32-levels:
10subpixel = False
11# Better handling for occlusions:
12lr_check = True
13
14# Create pipeline
15pipeline = dai.Pipeline()
16
17# Define sources and outputs
18monoLeft = pipeline.create(dai.node.MonoCamera)
19monoRight = pipeline.create(dai.node.MonoCamera)
20depth = pipeline.create(dai.node.StereoDepth)
21xout = pipeline.create(dai.node.XLinkOut)
22
23xout.setStreamName("disparity")
24
25# Properties
26monoLeft.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)
27monoLeft.setCamera("left")
28monoRight.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)
29monoRight.setCamera("right")
30
31# Create a node that will produce the depth map (using disparity output as it's easier to visualize depth this way)
32depth.setDefaultProfilePreset(dai.node.StereoDepth.PresetMode.HIGH_DENSITY)
33# Options: MEDIAN_OFF, KERNEL_3x3, KERNEL_5x5, KERNEL_7x7 (default)
34depth.initialConfig.setMedianFilter(dai.MedianFilter.KERNEL_7x7)
35depth.setLeftRightCheck(lr_check)
36depth.setExtendedDisparity(extended_disparity)
37depth.setSubpixel(subpixel)
38
39config = depth.initialConfig.get()
40config.postProcessing.speckleFilter.enable = False
41config.postProcessing.speckleFilter.speckleRange = 50
42config.postProcessing.temporalFilter.enable = True
43config.postProcessing.spatialFilter.enable = True
44config.postProcessing.spatialFilter.holeFillingRadius = 2
45config.postProcessing.spatialFilter.numIterations = 1
46config.postProcessing.thresholdFilter.minRange = 400
47config.postProcessing.thresholdFilter.maxRange = 15000
48config.postProcessing.decimationFilter.decimationFactor = 1
49depth.initialConfig.set(config)
50
51# Linking
52monoLeft.out.link(depth.left)
53monoRight.out.link(depth.right)
54depth.disparity.link(xout.input)
55
56# Connect to device and start pipeline
57with dai.Device(pipeline) as device:
58
59    # Output queue will be used to get the disparity frames from the outputs defined above
60    q = device.getOutputQueue(name="disparity", maxSize=4, blocking=False)
61
62    while True:
63        inDisparity = q.get()  # blocking call, will wait until a new data has arrived
64        frame = inDisparity.getFrame()
65        # Normalization for better visualization
66        frame = (frame * (255 / depth.initialConfig.getMaxDisparity())).astype(np.uint8)
67
68        cv2.imshow("disparity", frame)
69
70        # Available color maps: https://docs.opencv.org/3.4/d3/d50/group__imgproc__colormap.html
71        frame = cv2.applyColorMap(frame, cv2.COLORMAP_JET)
72        cv2.imshow("disparity_color", frame)
73
74        if cv2.waitKey(1) == ord('q'):
75            break

C++

1#include <iostream>
2
3// Inludes common necessary includes for development using depthai library
4#include "depthai/depthai.hpp"
5
6// Closer-in minimum depth, disparity range is doubled (from 95 to 190):
7static std::atomic<bool> extended_disparity{false};
8// Better accuracy for longer distance, fractional disparity 32-levels:
9static std::atomic<bool> subpixel{false};
10// Better handling for occlusions:
11static std::atomic<bool> lr_check{true};
12
13int main() {
14    // Create pipeline
15    dai::Pipeline pipeline;
16
17    // Define sources and outputs
18    auto monoLeft = pipeline.create<dai::node::MonoCamera>();
19    auto monoRight = pipeline.create<dai::node::MonoCamera>();
20    auto depth = pipeline.create<dai::node::StereoDepth>();
21    auto xout = pipeline.create<dai::node::XLinkOut>();
22
23    xout->setStreamName("disparity");
24
25    // Properties
26    monoLeft->setResolution(dai::MonoCameraProperties::SensorResolution::THE_400_P);
27    monoLeft->setCamera("left");
28    monoRight->setResolution(dai::MonoCameraProperties::SensorResolution::THE_400_P);
29    monoRight->setCamera("right");
30
31    // Create a node that will produce the depth map (using disparity output as it's easier to visualize depth this way)
32    depth->setDefaultProfilePreset(dai::node::StereoDepth::PresetMode::HIGH_DENSITY);
33    // Options: MEDIAN_OFF, KERNEL_3x3, KERNEL_5x5, KERNEL_7x7 (default)
34    depth->initialConfig.setMedianFilter(dai::MedianFilter::KERNEL_7x7);
35    depth->setLeftRightCheck(lr_check);
36    depth->setExtendedDisparity(extended_disparity);
37    depth->setSubpixel(subpixel);
38    auto config = depth->initialConfig.get();
39    config.postProcessing.speckleFilter.enable = false;
40    config.postProcessing.speckleFilter.speckleRange = 50;
41    config.postProcessing.temporalFilter.enable = true;
42    config.postProcessing.spatialFilter.enable = true;
43    config.postProcessing.spatialFilter.holeFillingRadius = 2;
44    config.postProcessing.spatialFilter.numIterations = 1;
45    config.postProcessing.thresholdFilter.minRange = 400;
46    config.postProcessing.thresholdFilter.maxRange = 15000;
47    config.postProcessing.decimationFilter.decimationFactor = 1;
48    depth->initialConfig.set(config);
49
50    // Linking
51    monoLeft->out.link(depth->left);
52    monoRight->out.link(depth->right);
53    depth->disparity.link(xout->input);
54
55    // Connect to device and start pipeline
56    dai::Device device(pipeline);
57
58    // Output queue will be used to get the disparity frames from the outputs defined above
59    auto q = device.getOutputQueue("disparity", 4, false);
60
61    while(true) {
62        auto inDepth = q->get<dai::ImgFrame>();
63        auto frame = inDepth->getFrame();
64        // Normalization for better visualization
65        frame.convertTo(frame, CV_8UC1, 255 / depth->initialConfig.getMaxDisparity());
66
67        cv::imshow("disparity", frame);
68
69        // Available color maps: https://docs.opencv.org/3.4/d3/d50/group__imgproc__colormap.html
70        cv::applyColorMap(frame, frame, cv::COLORMAP_JET);
71        cv::imshow("disparity_color", frame);
72
73        int key = cv::waitKey(1);
74        if(key == 'q' || key == 'Q') {
75            return 0;
76        }
77    }
78    return 0;
79}

流水线

需要帮助?

请前往 OAKChina 官网 获取技术支持或解答您的任何疑问。