rgb_depth_confidence_aligned.cpp
Go to the documentation of this file.
1 #include <cstdio>
2 #include <iostream>
3 
4 #include "utility.hpp"
5 
6 // Includes common necessary includes for development using depthai library
7 #include "depthai/depthai.hpp"
8 
9 // Optional. If set (true), the ColorCamera is downscaled from 1080p to 720p.
10 // Otherwise (false), the aligned depth is automatically upscaled to 1080p
11 static std::atomic<bool> downscaleColor{true};
12 static constexpr int fps = 30;
13 // The disparity is computed at this resolution, then upscaled to RGB resolution
15 
16 static float rgbWeight = 0.3f;
17 static float depthWeight = 0.3f;
18 static float confWeight = 0.3f;
19 
20 static float rgbWeightNorm = 0.3f;
21 static float depthWeightNorm = 0.3f;
22 static float confWeightNorm = 0.3f;
23 
24 static void updateBlendWeights(int percentRgb, void* weight) {
25  *((float*)weight) = float(percentRgb) / 100.f;
26 }
27 
28 int main() {
29  using namespace std;
30 
31  // Create pipeline
32  dai::Pipeline pipeline;
33  dai::Device device;
34  std::vector<std::string> queueNames;
35 
36  // Define sources and outputs
37  auto camRgb = pipeline.create<dai::node::ColorCamera>();
38  auto left = pipeline.create<dai::node::MonoCamera>();
39  auto right = pipeline.create<dai::node::MonoCamera>();
40  auto stereo = pipeline.create<dai::node::StereoDepth>();
41 
42  auto rgbOut = pipeline.create<dai::node::XLinkOut>();
43  auto depthOut = pipeline.create<dai::node::XLinkOut>();
44  auto confOut = pipeline.create<dai::node::XLinkOut>();
45 
46  rgbOut->setStreamName("rgb");
47  queueNames.push_back("rgb");
48  depthOut->setStreamName("depth");
49  queueNames.push_back("depth");
50  confOut->setStreamName("conf");
51  queueNames.push_back("conf");
52 
53  // Properties
54  camRgb->setBoardSocket(dai::CameraBoardSocket::CAM_A);
56  camRgb->setFps(fps);
57  if(downscaleColor) camRgb->setIspScale(2, 3);
58  // For now, RGB needs fixed focus to properly align with depth.
59  // This value was used during calibration
60  try {
61  auto calibData = device.readCalibration2();
62  auto lensPosition = calibData.getLensPosition(dai::CameraBoardSocket::CAM_A);
63  if(lensPosition) {
64  camRgb->initialControl.setManualFocus(lensPosition);
65  }
66  } catch(const std::exception& ex) {
67  std::cout << ex.what() << std::endl;
68  return 1;
69  }
70 
71  left->setResolution(monoRes);
72  left->setCamera("left");
73  left->setFps(fps);
74  right->setResolution(monoRes);
75  right->setCamera("right");
76  right->setFps(fps);
77 
78  stereo->setDefaultProfilePreset(dai::node::StereoDepth::PresetMode::HIGH_DENSITY);
79  // LR-check is required for depth alignment
80  stereo->setLeftRightCheck(true);
81  stereo->setDepthAlign(dai::CameraBoardSocket::CAM_A);
82 
83  // Linking
84  camRgb->isp.link(rgbOut->input);
85  left->out.link(stereo->left);
86  right->out.link(stereo->right);
87  stereo->disparity.link(depthOut->input);
88  stereo->confidenceMap.link(confOut->input);
89 
90  // Connect to device and start pipeline
91  device.startPipeline(pipeline);
92 
93  // Sets queues size and behavior
94  for(const auto& name : queueNames) {
95  device.getOutputQueue(name, 4, false);
96  }
97 
98  std::unordered_map<std::string, cv::Mat> frame;
99 
100  auto rgbWindowName = "rgb";
101  auto depthWindowName = "depth";
102  auto confWindowName = "conf";
103  auto blendedWindowName = "rgb-depth-confidence";
104  cv::namedWindow(rgbWindowName);
105  cv::namedWindow(depthWindowName);
106  cv::namedWindow(confWindowName);
107  cv::namedWindow(blendedWindowName);
108  int defRgbWeightValue = (int)(rgbWeight * 100);
109  int defDepthWeightValue = (int)(rgbWeight * 100);
110  int defConfWeightValue = (int)(rgbWeight * 100);
111  cv::createTrackbar("RGB Weight %", blendedWindowName, &defRgbWeightValue, 100, updateBlendWeights, &rgbWeight);
112  cv::createTrackbar("Depth Weight %", blendedWindowName, &defDepthWeightValue, 100, updateBlendWeights, &depthWeight);
113  cv::createTrackbar("Confidence Weight %", blendedWindowName, &defConfWeightValue, 100, updateBlendWeights, &confWeight);
114 
115  while(true) {
116  std::unordered_map<std::string, std::shared_ptr<dai::ImgFrame>> latestPacket;
117 
118  auto queueEvents = device.getQueueEvents(queueNames);
119  for(const auto& name : queueEvents) {
120  auto packets = device.getOutputQueue(name)->tryGetAll<dai::ImgFrame>();
121  auto count = packets.size();
122  if(count > 0) {
123  latestPacket[name] = packets[count - 1];
124  }
125  }
126 
127  for(const auto& name : queueNames) {
128  if(latestPacket.find(name) != latestPacket.end()) {
129  if(name == depthWindowName) {
130  frame[name] = latestPacket[name]->getFrame();
131  auto maxDisparity = stereo->initialConfig.getMaxDisparity();
132  // Optional, extend range 0..95 -> 0..255, for a better visualisation
133  if(1) frame[name].convertTo(frame[name], CV_8UC1, 255. / maxDisparity);
134  // Optional, apply false colorization
135  if(1) cv::applyColorMap(frame[name], frame[name], cv::COLORMAP_HOT);
136  } else {
137  frame[name] = latestPacket[name]->getCvFrame();
138  }
139 
140  cv::imshow(name, frame[name]);
141  }
142  }
143 
144  // Blend when all three frames received
145  if(frame.find(rgbWindowName) != frame.end() && frame.find(depthWindowName) != frame.end() && frame.find(confWindowName) != frame.end()) {
146  // Need to have all three frames in BGR format before blending
147  if(frame[depthWindowName].channels() < 3) {
148  cv::cvtColor(frame[depthWindowName], frame[depthWindowName], cv::COLOR_GRAY2BGR);
149  }
150  if(frame[confWindowName].channels() < 3) {
151  cv::cvtColor(frame[confWindowName], frame[confWindowName], cv::COLOR_GRAY2BGR);
152  }
153 
154  float sumWeight = rgbWeight + depthWeight + confWeight;
155  // Normalize the weights so their sum to be <= 1.0
156  if(sumWeight <= 1.0) {
160  } else {
161  rgbWeightNorm = rgbWeight / sumWeight;
162  depthWeightNorm = depthWeight / sumWeight;
163  confWeightNorm = confWeight / sumWeight;
164  }
165 
166  cv::Mat blended1, blended2;
167  cv::addWeighted(frame[rgbWindowName], rgbWeightNorm, frame[depthWindowName], depthWeightNorm, 0, blended1);
168  cv::addWeighted(blended1, rgbWeightNorm + depthWeightNorm, frame[confWindowName], confWeightNorm, 0, blended2);
169  cv::imshow(blendedWindowName, blended2);
170  frame.clear();
171  }
172 
173  int key = cv::waitKey(1);
174  if(key == 'q' || key == 'Q') {
175  return 0;
176  }
177  }
178  return 0;
179 }
dai::node::MonoCamera::out
Output out
Definition: MonoCamera.hpp:47
dai::DeviceBase::startPipeline
bool startPipeline()
Definition: DeviceBase.cpp:1511
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::node::MonoCamera::setFps
void setFps(float fps)
Definition: MonoCamera.cpp:98
dai::ImgFrame::getFrame
void getFrame(T...)
Definition: ImgFrame.hpp:218
dai::node::ColorCamera::setResolution
void setResolution(Properties::SensorResolution resolution)
Set sensor resolution.
Definition: ColorCamera.cpp:169
updateBlendWeights
static void updateBlendWeights(int percentRgb, void *weight)
Definition: rgb_depth_confidence_aligned.cpp:24
dai::node::StereoDepth
StereoDepth node. Compute stereo disparity and depth from left-right image pair.
Definition: StereoDepth.hpp:15
dai::node::ColorCamera::setFps
void setFps(float fps)
Definition: ColorCamera.cpp:176
dai::CameraBoardSocket::CAM_A
@ CAM_A
dai::node::MonoCamera
MonoCamera node. For use with grayscale sensors.
Definition: MonoCamera.hpp:17
dai::node::ColorCamera
ColorCamera node. For use with color sensors.
Definition: ColorCamera.hpp:16
confWeightNorm
static float confWeightNorm
Definition: rgb_depth_confidence_aligned.cpp:22
confWeight
static float confWeight
Definition: rgb_depth_confidence_aligned.cpp:18
depthWeight
static float depthWeight
Definition: rgb_depth_confidence_aligned.cpp:17
depthWeightNorm
static float depthWeightNorm
Definition: rgb_depth_confidence_aligned.cpp:21
dai::Device::getOutputQueue
std::shared_ptr< DataOutputQueue > getOutputQueue(const std::string &name)
Definition: Device.cpp:86
dai::MonoCameraProperties::SensorResolution::THE_720_P
@ THE_720_P
dai::node::XLinkOut::input
Input input
Definition: XLinkOut.hpp:27
depthai.hpp
dai::Pipeline::create
std::shared_ptr< N > create()
Definition: Pipeline.hpp:145
main
int main()
Definition: rgb_depth_confidence_aligned.cpp:28
dai::node::MonoCamera::setResolution
void setResolution(Properties::SensorResolution resolution)
Set sensor resolution.
Definition: MonoCamera.cpp:82
rgbWeightNorm
static float rgbWeightNorm
Definition: rgb_depth_confidence_aligned.cpp:20
dai::ImgFrame
Definition: ImgFrame.hpp:25
dai::ColorCameraProperties::SensorResolution::THE_1080_P
@ THE_1080_P
1920 × 1080
dai::DeviceBase::readCalibration2
CalibrationHandler readCalibration2()
Definition: DeviceBase.cpp:1385
dai::node::StereoDepth::PresetMode::HIGH_DENSITY
@ HIGH_DENSITY
dai::Device
Definition: Device.hpp:21
dai::CalibrationHandler::getLensPosition
uint8_t getLensPosition(CameraBoardSocket cameraId) const
Definition: CalibrationHandler.cpp:343
std
Definition: Node.hpp:366
fps
static constexpr int fps
Definition: rgb_depth_confidence_aligned.cpp:12
dai::Device::getQueueEvents
std::vector< std::string > getQueueEvents(const std::vector< std::string > &queueNames, std::size_t maxNumEvents=std::numeric_limits< std::size_t >::max(), std::chrono::microseconds timeout=std::chrono::microseconds(-1))
Definition: Device.cpp:164
rgbWeight
static float rgbWeight
Definition: rgb_depth_confidence_aligned.cpp:16
dai::Node::Output::link
void link(const Input &in)
Definition: Node.cpp:84
dai::node::XLinkOut::setStreamName
void setStreamName(const std::string &name)
Definition: XLinkOut.cpp:13
monoRes
static constexpr auto monoRes
Definition: rgb_depth_confidence_aligned.cpp:14
downscaleColor
static std::atomic< bool > downscaleColor
Definition: rgb_depth_confidence_aligned.cpp:11
utility.hpp
dai::node::ColorCamera::setCamera
void setCamera(std::string name)
Definition: ColorCamera.cpp:44


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