DepthAI v2 has been superseded by DepthAI v3. You are viewing legacy documentation.
此页面由 AI 自动翻译。查看英文原版
DepthAI 教程
DepthAI API 参考

本页目录

  • 演示
  • 设置
  • 源代码
  • 流程图

深度裁剪控制

此示例演示了裁剪模式下深度相机的用法,并可以移动裁剪区域。 您可以使用以下按键来操作裁剪帧的移动:
  • w 向上移动裁剪区域
  • a 向左移动裁剪区域
  • s 向下移动裁剪区域
  • d 向右移动裁剪区域

类似示例:

演示

设置

此示例需要 DepthAI v3 API,请参阅 安装说明

源代码

Python

Python
GitHub
1#!/usr/bin/env python3
2
3"""
4This example shows usage of depth camera in crop mode with the possibility to move the crop.
5Use 'WASD' in order to do it.
6"""
7
8import cv2
9import depthai as dai
10import numpy as np
11
12# Step size ('W','A','S','D' controls)
13stepSize = 0.02
14
15# Create pipeline
16pipeline = dai.Pipeline()
17
18# Define sources and outputs
19monoRight = pipeline.create(dai.node.MonoCamera)
20monoLeft = pipeline.create(dai.node.MonoCamera)
21manip = pipeline.create(dai.node.ImageManip)
22stereo = pipeline.create(dai.node.StereoDepth)
23
24configIn = pipeline.create(dai.node.XLinkIn)
25xout = pipeline.create(dai.node.XLinkOut)
26
27configIn.setStreamName('config')
28xout.setStreamName("depth")
29
30# Crop range
31topLeft = dai.Point2f(0.2, 0.2)
32bottomRight = dai.Point2f(0.8, 0.8)
33
34# Properties
35monoRight.setCamera("right")
36monoLeft.setCamera("left")
37monoRight.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)
38monoLeft.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)
39
40manip.initialConfig.setCropRect(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y)
41manip.setMaxOutputFrameSize(monoRight.getResolutionHeight()*monoRight.getResolutionWidth()*3)
42stereo.setDefaultProfilePreset(dai.node.StereoDepth.PresetMode.HIGH_DENSITY)
43stereo.setSubpixel(True)
44
45# Linking
46configIn.out.link(manip.inputConfig)
47stereo.depth.link(manip.inputImage)
48manip.out.link(xout.input)
49monoRight.out.link(stereo.right)
50monoLeft.out.link(stereo.left)
51
52# Connect to device and start pipeline
53with dai.Device(pipeline) as device:
54
55    # Queues
56    q = device.getOutputQueue(xout.getStreamName(), maxSize=4, blocking=False)
57    configQueue = device.getInputQueue(configIn.getStreamName())
58
59    sendCamConfig = False
60
61    while True:
62        inDepth = q.get()
63        depthFrame = inDepth.getFrame() # depthFrame values are in millimeters
64
65        # Frame is transformed, the color map will be applied to highlight the depth info
66        depth_downscaled = depthFrame[::4]
67        if np.all(depth_downscaled == 0):
68            min_depth = 0  # Set a default minimum depth value when all elements are zero
69        else:
70            min_depth = np.percentile(depth_downscaled[depth_downscaled != 0], 1)
71        max_depth = np.percentile(depth_downscaled, 99)
72        depthFrameColor = np.interp(depthFrame, (min_depth, max_depth), (0, 255)).astype(np.uint8)
73        depthFrameColor = cv2.applyColorMap(depthFrameColor, cv2.COLORMAP_HOT)
74
75        # Frame is ready to be shown
76        cv2.imshow("depth", depthFrameColor)
77
78        # Update screen
79        key = cv2.waitKey(10)
80        if key == ord('q'):
81            break
82        elif key == ord('w'):
83            if topLeft.y - stepSize >= 0:
84                topLeft.y -= stepSize
85                bottomRight.y -= stepSize
86                sendCamConfig = True
87        elif key == ord('a'):
88            if topLeft.x - stepSize >= 0:
89                topLeft.x -= stepSize
90                bottomRight.x -= stepSize
91                sendCamConfig = True
92        elif key == ord('s'):
93            if bottomRight.y + stepSize <= 1:
94                topLeft.y += stepSize
95                bottomRight.y += stepSize
96                sendCamConfig = True
97        elif key == ord('d'):
98            if bottomRight.x + stepSize <= 1:
99                topLeft.x += stepSize
100                bottomRight.x += stepSize
101                sendCamConfig = True
102
103        # Send new config to camera
104        if sendCamConfig:
105            cfg = dai.ImageManipConfig()
106            cfg.setCropRect(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y)
107            configQueue.send(cfg)
108            sendCamConfig = False

C++

1/**
2 * This example shows usage of depth camera in crop mode with the possibility to move the crop.
3 * Use 'WASD' in order to do it.
4 */
5#include <iostream>
6
7// Includes common necessary includes for development using depthai library
8#include "depthai/depthai.hpp"
9
10// Step size ('W','A','S','D' controls)
11static constexpr float stepSize = 0.02f;
12
13int main() {
14    // Create pipeline
15    dai::Pipeline pipeline;
16
17    // Define sources and outputs
18    auto monoRight = pipeline.create<dai::node::MonoCamera>();
19    auto monoLeft = pipeline.create<dai::node::MonoCamera>();
20    auto manip = pipeline.create<dai::node::ImageManip>();
21    auto stereo = pipeline.create<dai::node::StereoDepth>();
22
23    auto configIn = pipeline.create<dai::node::XLinkIn>();
24    auto xout = pipeline.create<dai::node::XLinkOut>();
25
26    configIn->setStreamName("config");
27    xout->setStreamName("depth");
28
29    // Crop range
30    dai::Point2f topLeft(0.2f, 0.2f);
31    dai::Point2f bottomRight(0.8f, 0.8f);
32
33    // Properties
34    monoRight->setCamera("right");
35    monoLeft->setCamera("left");
36    monoRight->setResolution(dai::MonoCameraProperties::SensorResolution::THE_400_P);
37    monoLeft->setResolution(dai::MonoCameraProperties::SensorResolution::THE_400_P);
38
39    manip->initialConfig.setCropRect(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y);
40    manip->setMaxOutputFrameSize(monoRight->getResolutionHeight() * monoRight->getResolutionWidth() * 3);
41    stereo->setDefaultProfilePreset(dai::node::StereoDepth::PresetMode::HIGH_DENSITY);
42
43    // Linking
44    configIn->out.link(manip->inputConfig);
45    stereo->depth.link(manip->inputImage);
46    manip->out.link(xout->input);
47    monoRight->out.link(stereo->right);
48    monoLeft->out.link(stereo->left);
49
50    // Connect to device and start pipeline
51    dai::Device device(pipeline);
52
53    // Queues
54    auto q = device.getOutputQueue(xout->getStreamName(), 4, false);
55    auto configQueue = device.getInputQueue(configIn->getStreamName());
56
57    bool sendCamConfig = false;
58
59    while(true) {
60        auto inDepth = q->get<dai::ImgFrame>();
61        cv::Mat depthFrame = inDepth->getFrame();  // depthFrame values are in millimeters
62
63        // Frame is transformed, the color map will be applied to highlight the depth info
64        cv::Mat depthFrameColor;
65        cv::normalize(depthFrame, depthFrameColor, 255, 0, cv::NORM_INF, CV_8UC1);
66        cv::equalizeHist(depthFrameColor, depthFrameColor);
67        cv::applyColorMap(depthFrameColor, depthFrameColor, cv::COLORMAP_HOT);
68
69        // Frame is ready to be shown
70        cv::imshow("depth", depthFrameColor);
71
72        // Update screen (10ms pooling rate)
73        int key = cv::waitKey(9);
74        cv::waitKey(1);  // glitch workaround
75        if(key == 'q') {
76            break;
77        } else if(key == 'w') {
78            if(topLeft.y - stepSize >= 0) {
79                topLeft.y -= stepSize;
80                bottomRight.y -= stepSize;
81                sendCamConfig = true;
82            }
83        } else if(key == 'a') {
84            if(topLeft.x - stepSize >= 0) {
85                topLeft.x -= stepSize;
86                bottomRight.x -= stepSize;
87                sendCamConfig = true;
88            }
89        } else if(key == 's') {
90            if(bottomRight.y + stepSize <= 1) {
91                topLeft.y += stepSize;
92                bottomRight.y += stepSize;
93                sendCamConfig = true;
94            }
95        } else if(key == 'd') {
96            if(bottomRight.x + stepSize <= 1) {
97                topLeft.x += stepSize;
98                bottomRight.x += stepSize;
99                sendCamConfig = true;
100            }
101        }
102
103        // Send new config to camera
104        if(sendCamConfig) {
105            dai::ImageManipConfig cfg;
106            cfg.setCropRect(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y);
107            configQueue->send(cfg);
108            sendCamConfig = false;
109        }
110    }
111    return 0;
112}

流程图

需要帮助?

请前往 Discussion Forum 获取技术支持或提出您可能有的任何其他问题。