tof_depth.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <chrono>
3 #include <opencv2/opencv.hpp>
4 #include <depthai/depthai.hpp>
5 #include <vector>
6 #include <string>
9 
10 constexpr auto SHAPE = 720;
11 
12 std::shared_ptr<dai::ToFConfig> createConfig(dai::RawToFConfig configRaw) {
13  auto config = std::make_shared<dai::ToFConfig>();
14  config->set(std::move(configRaw));
15  return config;
16 }
17 
18 cv::Mat colorizeDepth(const cv::Mat& frameDepth) {
19  cv::Mat invalidMask = (frameDepth == 0);
20  cv::Mat depthFrameColor;
21  try {
22  double minDepth = 0.0;
23  double maxDepth = 0.0;
24  cv::minMaxIdx(frameDepth, &minDepth, &maxDepth, nullptr, nullptr, ~invalidMask);
25  if(minDepth == maxDepth) {
26  depthFrameColor = cv::Mat::zeros(frameDepth.size(), CV_8UC3);
27  return depthFrameColor;
28  }
29  cv::Mat logDepth;
30  frameDepth.convertTo(logDepth, CV_32F);
31  cv::log(logDepth, logDepth);
32  logDepth.setTo(log(minDepth), invalidMask);
33  cv::normalize(logDepth, logDepth, 0, 255, cv::NORM_MINMAX, CV_8UC1);
34  cv::applyColorMap(logDepth, depthFrameColor, cv::COLORMAP_JET);
35  depthFrameColor.setTo(cv::Scalar(0, 0, 0), invalidMask);
36  } catch(const std::exception& e) {
37  depthFrameColor = cv::Mat::zeros(frameDepth.size(), CV_8UC3);
38  }
39  return depthFrameColor;
40 }
41 
42 int main() {
43  dai::Pipeline pipeline;
44  cv::Mat cvColorMap;
45 
46  auto tof = pipeline.create<dai::node::ToF>();
47 
48  // Configure the ToF node
49  auto tofConfig = tof->initialConfig.get();
50 
51  tofConfig.enableOpticalCorrection = true;
52  tofConfig.enablePhaseShuffleTemporalFilter = true;
53  tofConfig.phaseUnwrappingLevel = 4;
54  tofConfig.phaseUnwrapErrorThreshold = 300;
55 
56  tofConfig.enableTemperatureCorrection = false; // Not yet supported
57 
58  auto xinTofConfig = pipeline.create<dai::node::XLinkIn>();
59  xinTofConfig->setStreamName("tofConfig");
60  xinTofConfig->out.link(tof->inputConfig);
61 
62  tof->initialConfig.set(tofConfig);
63 
64  auto camTof = pipeline.create<dai::node::Camera>();
65  camTof->setFps(30);
66  camTof->setBoardSocket(dai::CameraBoardSocket::CAM_A);
67  camTof->raw.link(tof->input);
68 
69  auto xout = pipeline.create<dai::node::XLinkOut>();
70  xout->setStreamName("depth");
71  tof->depth.link(xout->input);
72 
73  tofConfig = tof->initialConfig.get();
74 
75 
76  dai::Device device(pipeline);
77  std::cout << "Connected cameras: " << device.getConnectedCameraFeatures().size() << std::endl;
78  auto qDepth = device.getOutputQueue("depth");
79 
80  auto tofConfigInQueue = device.getInputQueue("tofConfig");
81 
82  int counter = 0;
83  while (true) {
84  auto start = std::chrono::high_resolution_clock::now();
85  int key = cv::waitKey(1);
86  if (key == 'f') {
87  tofConfig.enableFPPNCorrection = !tofConfig.enableFPPNCorrection;
88  tofConfigInQueue->send(createConfig(tofConfig));
89  } else if (key == 'o') {
90  tofConfig.enableOpticalCorrection = !tofConfig.enableOpticalCorrection;
91  tofConfigInQueue->send(createConfig(tofConfig));
92  } else if (key == 'w') {
93  tofConfig.enableWiggleCorrection = !tofConfig.enableWiggleCorrection;
94  tofConfigInQueue->send(createConfig(tofConfig));
95  } else if (key == 't') {
96  tofConfig.enableTemperatureCorrection = !tofConfig.enableTemperatureCorrection;
97  tofConfigInQueue->send(createConfig(tofConfig));
98  } else if (key == 'q') {
99  break;
100  } else if (key == '0') {
101  tofConfig.enablePhaseUnwrapping = false;
102  tofConfig.phaseUnwrappingLevel = 0;
103  tofConfigInQueue->send(createConfig(tofConfig));
104  } else if (key == '1') {
105  tofConfig.enablePhaseUnwrapping = true;
106  tofConfig.phaseUnwrappingLevel = 1;
107  tofConfigInQueue->send(createConfig(tofConfig));
108  } else if (key == '2') {
109  tofConfig.enablePhaseUnwrapping = true;
110  tofConfig.phaseUnwrappingLevel = 2;
111  tofConfigInQueue->send(createConfig(tofConfig));
112  } else if (key == '3') {
113  tofConfig.enablePhaseUnwrapping = true;
114  tofConfig.phaseUnwrappingLevel = 3;
115  tofConfigInQueue->send(createConfig(tofConfig));
116  } else if (key == '4') {
117  tofConfig.enablePhaseUnwrapping = true;
118  tofConfig.phaseUnwrappingLevel = 4;
119  tofConfigInQueue->send(createConfig(tofConfig));
120  } else if (key == '5') {
121  tofConfig.enablePhaseUnwrapping = true;
122  tofConfig.phaseUnwrappingLevel = 5;
123  tofConfigInQueue->send(createConfig(tofConfig));
124  } else if (key == 'm') {
126  auto currentMedian = tofConfig.median;
127  auto nextMedian = medianSettings[(std::find(medianSettings.begin(), medianSettings.end(), currentMedian) - medianSettings.begin() + 1) % medianSettings.size()];
128  tofConfig.median = nextMedian;
129  tofConfigInQueue->send(createConfig(tofConfig));
130  }
131 
132  auto imgFrame = qDepth->get<dai::ImgFrame>(); // blocking call, will wait until new data has arrived
133  auto depthColorized = colorizeDepth(imgFrame->getFrame());
134 
135  cv::imshow("Colorized depth", depthColorized);
136  counter++;
137  }
138 
139  return 0;
140 }
dai::node::XLinkOut
XLinkOut node. Sends messages over XLink.
Definition: XLinkOut.hpp:14
dai::Pipeline
Represents the pipeline, set of nodes and connections between them.
Definition: Pipeline.hpp:100
SHAPE
constexpr auto SHAPE
Definition: tof_depth.cpp:10
dai::CameraBoardSocket::CAM_A
@ CAM_A
RawToFConfig.hpp
main
int main()
Definition: tof_depth.cpp:42
dai::MedianFilter::MEDIAN_OFF
@ MEDIAN_OFF
dai::ToFConfig::get
dai::RawToFConfig get() const
Definition: ToFConfig.cpp:12
dai::node::ToF::initialConfig
ToFConfig initialConfig
Definition: ToF.hpp:36
dai::logger::log
void log(spdlog::source_loc source, spdlog::level::level_enum lvl, const FormatString &fmt, Args &&...args)
Definition: Logging.hpp:54
dai::DeviceBase::getConnectedCameraFeatures
std::vector< CameraFeatures > getConnectedCameraFeatures()
Definition: DeviceBase.cpp:1109
dai::Device::getOutputQueue
std::shared_ptr< DataOutputQueue > getOutputQueue(const std::string &name)
Definition: Device.cpp:86
dai::RawToFConfig
RawToFConfig configuration structure.
Definition: RawToFConfig.hpp:14
depthai.hpp
dai::Pipeline::create
std::shared_ptr< N > create()
Definition: Pipeline.hpp:145
dai::node::XLinkIn::setStreamName
void setStreamName(const std::string &name)
Definition: XLinkIn.cpp:12
dai::MedianFilter::KERNEL_3x3
@ KERNEL_3x3
dai::ImgFrame
Definition: ImgFrame.hpp:25
dai::node::Camera::setFps
void setFps(float fps)
Definition: Camera.cpp:129
createConfig
std::shared_ptr< dai::ToFConfig > createConfig(dai::RawToFConfig configRaw)
Definition: tof_depth.cpp:12
dai::Device
Definition: Device.hpp:21
colorizeDepth
cv::Mat colorizeDepth(const cv::Mat &frameDepth)
Definition: tof_depth.cpp:18
ToFConfig.hpp
dai::node::XLinkIn
XLinkIn node. Receives messages over XLink.
Definition: XLinkIn.hpp:14
dai::MedianFilter::KERNEL_7x7
@ KERNEL_7x7
dai::Device::getInputQueue
std::shared_ptr< DataInputQueue > getInputQueue(const std::string &name)
Definition: Device.cpp:120
dai::node::XLinkOut::setStreamName
void setStreamName(const std::string &name)
Definition: XLinkOut.cpp:13
dai::MedianFilter::KERNEL_5x5
@ KERNEL_5x5
dai::node::Camera
Camera node. Experimental node, for both mono and color types of sensors.
Definition: Camera.hpp:18
dai::node::ToF
ToF node.
Definition: ToF.hpp:16


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