spatial_calculator_multi_roi.cpp
Go to the documentation of this file.
1 #include <iostream>
2 
3 #include "utility.hpp"
4 
5 // Includes common necessary includes for development using depthai library
6 #include "depthai/depthai.hpp"
7 
8 static constexpr float stepSize = 0.05f;
9 
10 static std::atomic<bool> newConfig{false};
11 
12 int main() {
13  using namespace std;
14 
15  // Create pipeline
16  dai::Pipeline pipeline;
17 
18  // Define sources and outputs
19  auto monoLeft = pipeline.create<dai::node::MonoCamera>();
20  auto monoRight = pipeline.create<dai::node::MonoCamera>();
21  auto stereo = pipeline.create<dai::node::StereoDepth>();
22  auto spatialLocationCalculator = pipeline.create<dai::node::SpatialLocationCalculator>();
23 
24  auto xoutDepth = pipeline.create<dai::node::XLinkOut>();
25  auto xoutSpatialData = pipeline.create<dai::node::XLinkOut>();
26  auto xinSpatialCalcConfig = pipeline.create<dai::node::XLinkIn>();
27 
28  xoutDepth->setStreamName("depth");
29  xoutSpatialData->setStreamName("spatialData");
30  xinSpatialCalcConfig->setStreamName("spatialCalcConfig");
31 
32  // Properties
34  monoLeft->setCamera("left");
36  monoRight->setCamera("right");
37 
38  stereo->setDefaultProfilePreset(dai::node::StereoDepth::PresetMode::HIGH_DENSITY);
39  stereo->setLeftRightCheck(true);
40  stereo->setExtendedDisparity(true);
41  spatialLocationCalculator->inputConfig.setWaitForMessage(false);
42 
43  // Create 10 ROIs
44  for(int i = 0; i < 10; i++) {
46  config.depthThresholds.lowerThreshold = 200;
47  config.depthThresholds.upperThreshold = 10000;
48  config.roi = dai::Rect(dai::Point2f(i * 0.1, 0.45), dai::Point2f((i + 1) * 0.1, 0.55));
49  spatialLocationCalculator->initialConfig.addROI(config);
50  }
51 
52  // Linking
53  monoLeft->out.link(stereo->left);
54  monoRight->out.link(stereo->right);
55 
56  spatialLocationCalculator->passthroughDepth.link(xoutDepth->input);
57  stereo->depth.link(spatialLocationCalculator->inputDepth);
58 
59  spatialLocationCalculator->out.link(xoutSpatialData->input);
60  xinSpatialCalcConfig->out.link(spatialLocationCalculator->inputConfig);
61 
62  // Connect to device and start pipeline
63  dai::Device device(pipeline);
65 
66  // Output queue will be used to get the depth frames from the outputs defined above
67  auto depthQueue = device.getOutputQueue("depth", 4, false);
68  auto spatialCalcQueue = device.getOutputQueue("spatialData", 4, false);
69  auto color = cv::Scalar(0, 200, 40);
70  auto fontType = cv::FONT_HERSHEY_TRIPLEX;
71 
72  while(true) {
73  auto inDepth = depthQueue->get<dai::ImgFrame>();
74 
75  cv::Mat depthFrame = inDepth->getFrame(); // depthFrame values are in millimeters
76 
77  cv::Mat depthFrameColor;
78  cv::normalize(depthFrame, depthFrameColor, 255, 0, cv::NORM_INF, CV_8UC1);
79  cv::equalizeHist(depthFrameColor, depthFrameColor);
80  cv::applyColorMap(depthFrameColor, depthFrameColor, cv::COLORMAP_HOT);
81 
82  auto spatialData = spatialCalcQueue->get<dai::SpatialLocationCalculatorData>()->getSpatialLocations();
83  for(auto depthData : spatialData) {
84  auto roi = depthData.config.roi;
85  roi = roi.denormalize(depthFrameColor.cols, depthFrameColor.rows);
86 
87  auto xmin = static_cast<int>(roi.topLeft().x);
88  auto ymin = static_cast<int>(roi.topLeft().y);
89  auto xmax = static_cast<int>(roi.bottomRight().x);
90  auto ymax = static_cast<int>(roi.bottomRight().y);
91 
92  auto coords = depthData.spatialCoordinates;
93  auto distance = std::sqrt(coords.x * coords.x + coords.y * coords.y + coords.z * coords.z);
94 
95  cv::rectangle(depthFrameColor, cv::Rect(cv::Point(xmin, ymin), cv::Point(xmax, ymax)), color);
96  std::stringstream depthDistance;
97  depthDistance.precision(2);
98  depthDistance << fixed << static_cast<float>(distance / 1000.0f) << "m";
99  cv::putText(depthFrameColor, depthDistance.str(), cv::Point(xmin + 10, ymin + 20), fontType, 0.5, color);
100  }
101  // Show the frame
102  cv::imshow("depth", depthFrameColor);
103 
104  if(cv::waitKey(1) == 'q') {
105  break;
106  }
107  }
108  return 0;
109 }
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::initialConfig
StereoDepthConfig initialConfig
Definition: StereoDepth.hpp:52
dai::node::SpatialLocationCalculator
SpatialLocationCalculator node. Calculates spatial location data on a set of ROIs on depth map.
Definition: SpatialLocationCalculator.hpp:19
dai::node::StereoDepth
StereoDepth node. Compute stereo disparity and depth from left-right image pair.
Definition: StereoDepth.hpp:15
dai::Node::Input::setWaitForMessage
void setWaitForMessage(bool waitForMessage)
Definition: Node.cpp:116
dai::node::MonoCamera
MonoCamera node. For use with grayscale sensors.
Definition: MonoCamera.hpp:17
dai::SpatialLocationCalculatorConfigThresholds::lowerThreshold
uint32_t lowerThreshold
Definition: RawSpatialLocationCalculatorConfig.hpp:23
dai::Point2f
Definition: Point2f.hpp:16
dai::SpatialLocationCalculatorConfigData::roi
Rect roi
Definition: RawSpatialLocationCalculatorConfig.hpp:45
dai::SpatialLocationCalculatorConfigData
SpatialLocation configuration data structure.
Definition: RawSpatialLocationCalculatorConfig.hpp:39
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
dai::node::XLinkOut::input
Input input
Definition: XLinkOut.hpp:27
depthai.hpp
dai::Pipeline::create
std::shared_ptr< N > create()
Definition: Pipeline.hpp:145
dai::SpatialLocationCalculatorConfigData::depthThresholds
SpatialLocationCalculatorConfigThresholds depthThresholds
Definition: RawSpatialLocationCalculatorConfig.hpp:49
dai::node::MonoCamera::setResolution
void setResolution(Properties::SensorResolution resolution)
Set sensor resolution.
Definition: MonoCamera.cpp:82
dai::DeviceBase::setIrLaserDotProjectorIntensity
bool setIrLaserDotProjectorIntensity(float intensity, int mask=-1)
Definition: DeviceBase.cpp:1244
dai::ImgFrame
Definition: ImgFrame.hpp:25
main
int main()
Definition: spatial_calculator_multi_roi.cpp:12
dai::SpatialLocationCalculatorData
Definition: SpatialLocationCalculatorData.hpp:14
dai::node::StereoDepth::PresetMode::HIGH_DENSITY
@ HIGH_DENSITY
dai::Device
Definition: Device.hpp:21
std
Definition: Node.hpp:366
dai::Rect
Definition: Rect.hpp:18
stepSize
static constexpr float stepSize
Definition: spatial_calculator_multi_roi.cpp:8
dai::node::XLinkIn
XLinkIn node. Receives messages over XLink.
Definition: XLinkIn.hpp:14
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
dai::SpatialLocationCalculatorConfigThresholds::upperThreshold
uint32_t upperThreshold
Definition: RawSpatialLocationCalculatorConfig.hpp:27
dai::node::StereoDepth::inputConfig
Input inputConfig
Definition: StereoDepth.hpp:58
utility.hpp
newConfig
static std::atomic< bool > newConfig
Definition: spatial_calculator_multi_roi.cpp:10


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