depth_preview.cpp
Go to the documentation of this file.
1 #include <iostream>
2 
3 // Includes 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):
7 static std::atomic<bool> extended_disparity{false};
8 // Better accuracy for longer distance, fractional disparity 32-levels:
9 static std::atomic<bool> subpixel{false};
10 // Better handling for occlusions:
11 static std::atomic<bool> lr_check{true};
12 
13 int 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
27  monoLeft->setCamera("left");
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 
39  // Linking
40  monoLeft->out.link(depth->left);
41  monoRight->out.link(depth->right);
42  depth->disparity.link(xout->input);
43 
44  // Connect to device and start pipeline
45  dai::Device device(pipeline);
46 
47  // Output queue will be used to get the disparity frames from the outputs defined above
48  auto q = device.getOutputQueue("disparity", 4, false);
49 
50  while(true) {
51  auto inDepth = q->get<dai::ImgFrame>();
52  auto frame = inDepth->getFrame();
53  // Normalization for better visualization
54  frame.convertTo(frame, CV_8UC1, 255 / depth->initialConfig.getMaxDisparity());
55 
56  cv::imshow("disparity", frame);
57 
58  // Available color maps: https://docs.opencv.org/3.4/d3/d50/group__imgproc__colormap.html
59  cv::applyColorMap(frame, frame, cv::COLORMAP_JET);
60  cv::imshow("disparity_color", frame);
61 
62  int key = cv::waitKey(1);
63  if(key == 'q' || key == 'Q') {
64  return 0;
65  }
66  }
67  return 0;
68 }
dai::node::MonoCamera::out
Output out
Definition: MonoCamera.hpp:47
dai::node::XLinkOut
XLinkOut node. Sends messages over XLink.
Definition: XLinkOut.hpp:14
dai::node::MonoCamera::setCamera
void setCamera(std::string name)
Definition: MonoCamera.cpp:36
dai::Pipeline
Represents the pipeline, set of nodes and connections between them.
Definition: Pipeline.hpp:100
dai::ImgFrame::getFrame
void getFrame(T...)
Definition: ImgFrame.hpp:218
dai::node::StereoDepth
StereoDepth node. Compute stereo disparity and depth from left-right image pair.
Definition: StereoDepth.hpp:15
dai::node::MonoCamera
MonoCamera node. For use with grayscale sensors.
Definition: MonoCamera.hpp:17
lr_check
static std::atomic< bool > lr_check
Definition: depth_preview.cpp:11
dai::Device::getOutputQueue
std::shared_ptr< DataOutputQueue > getOutputQueue(const std::string &name)
Definition: Device.cpp:86
dai::MonoCameraProperties::SensorResolution::THE_400_P
@ THE_400_P
depthai.hpp
main
int main()
Definition: depth_preview.cpp:13
dai::Pipeline::create
std::shared_ptr< N > create()
Definition: Pipeline.hpp:145
dai::node::MonoCamera::setResolution
void setResolution(Properties::SensorResolution resolution)
Set sensor resolution.
Definition: MonoCamera.cpp:82
extended_disparity
static std::atomic< bool > extended_disparity
Definition: depth_preview.cpp:7
dai::ImgFrame
Definition: ImgFrame.hpp:25
subpixel
static std::atomic< bool > subpixel
Definition: depth_preview.cpp:9
dai::node::StereoDepth::PresetMode::HIGH_DENSITY
@ HIGH_DENSITY
dai::Device
Definition: Device.hpp:21
dai::MedianFilter::KERNEL_7x7
@ KERNEL_7x7
dai::Node::Output::link
void link(const Input &in)
Definition: Node.cpp:84


depthai
Author(s): Martin Peterlin
autogenerated on Sat Mar 22 2025 02:58:19