spatial_location_calculator.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 spatialDataCalculator = 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  bool lrcheck = false;
39  bool subpixel = false;
40 
41  stereo->setDefaultProfilePreset(dai::node::StereoDepth::PresetMode::HIGH_DENSITY);
42  stereo->setLeftRightCheck(lrcheck);
43  stereo->setSubpixel(subpixel);
44 
45  // Config
46  dai::Point2f topLeft(0.4f, 0.4f);
47  dai::Point2f bottomRight(0.6f, 0.6f);
48 
50  config.depthThresholds.lowerThreshold = 100;
51  config.depthThresholds.upperThreshold = 10000;
52  auto calculationAlgorithm = dai::SpatialLocationCalculatorAlgorithm::MEDIAN;
53  config.calculationAlgorithm = calculationAlgorithm;
54  config.roi = dai::Rect(topLeft, bottomRight);
55 
56  spatialDataCalculator->inputConfig.setWaitForMessage(false);
57  spatialDataCalculator->initialConfig.addROI(config);
58 
59  // Linking
60  monoLeft->out.link(stereo->left);
61  monoRight->out.link(stereo->right);
62 
63  spatialDataCalculator->passthroughDepth.link(xoutDepth->input);
64  stereo->depth.link(spatialDataCalculator->inputDepth);
65 
66  spatialDataCalculator->out.link(xoutSpatialData->input);
67  xinSpatialCalcConfig->out.link(spatialDataCalculator->inputConfig);
68 
69  // Connect to device and start pipeline
70  dai::Device device(pipeline);
71 
72  // Output queue will be used to get the depth frames from the outputs defined above
73  auto depthQueue = device.getOutputQueue("depth", 8, false);
74  auto spatialCalcQueue = device.getOutputQueue("spatialData", 8, false);
75  auto spatialCalcConfigInQueue = device.getInputQueue("spatialCalcConfig");
76 
77  auto color = cv::Scalar(255, 255, 255);
78 
79  std::cout << "Use WASD keys to move ROI!" << std::endl;
80 
81  while(true) {
82  auto inDepth = depthQueue->get<dai::ImgFrame>();
83 
84  cv::Mat depthFrame = inDepth->getFrame(); // depthFrame values are in millimeters
85  cv::Mat depthFrameColor;
86 
87  cv::normalize(depthFrame, depthFrameColor, 255, 0, cv::NORM_INF, CV_8UC1);
88  cv::equalizeHist(depthFrameColor, depthFrameColor);
89  cv::applyColorMap(depthFrameColor, depthFrameColor, cv::COLORMAP_HOT);
90 
91  auto spatialData = spatialCalcQueue->get<dai::SpatialLocationCalculatorData>()->getSpatialLocations();
92  for(auto depthData : spatialData) {
93  auto roi = depthData.config.roi;
94  roi = roi.denormalize(depthFrameColor.cols, depthFrameColor.rows);
95  auto xmin = (int)roi.topLeft().x;
96  auto ymin = (int)roi.topLeft().y;
97  auto xmax = (int)roi.bottomRight().x;
98  auto ymax = (int)roi.bottomRight().y;
99 
100  auto depthMin = depthData.depthMin;
101  auto depthMax = depthData.depthMax;
102 
103  cv::rectangle(depthFrameColor, cv::Rect(cv::Point(xmin, ymin), cv::Point(xmax, ymax)), color, cv::FONT_HERSHEY_SIMPLEX);
104  std::stringstream depthX;
105  depthX << "X: " << (int)depthData.spatialCoordinates.x << " mm";
106  cv::putText(depthFrameColor, depthX.str(), cv::Point(xmin + 10, ymin + 20), cv::FONT_HERSHEY_TRIPLEX, 0.5, color);
107  std::stringstream depthY;
108  depthY << "Y: " << (int)depthData.spatialCoordinates.y << " mm";
109  cv::putText(depthFrameColor, depthY.str(), cv::Point(xmin + 10, ymin + 35), cv::FONT_HERSHEY_TRIPLEX, 0.5, color);
110  std::stringstream depthZ;
111  depthZ << "Z: " << (int)depthData.spatialCoordinates.z << " mm";
112  cv::putText(depthFrameColor, depthZ.str(), cv::Point(xmin + 10, ymin + 50), cv::FONT_HERSHEY_TRIPLEX, 0.5, color);
113  }
114  // Show the frame
115  cv::imshow("depth", depthFrameColor);
116 
117  int key = cv::waitKey(1);
118  switch(key) {
119  case 'q':
120  return 0;
121  case 'w':
122  if(topLeft.y - stepSize >= 0) {
123  topLeft.y -= stepSize;
124  bottomRight.y -= stepSize;
125  newConfig = true;
126  }
127  break;
128  case 'a':
129  if(topLeft.x - stepSize >= 0) {
130  topLeft.x -= stepSize;
131  bottomRight.x -= stepSize;
132  newConfig = true;
133  }
134  break;
135  case 's':
136  if(bottomRight.y + stepSize <= 1) {
137  topLeft.y += stepSize;
138  bottomRight.y += stepSize;
139  newConfig = true;
140  }
141  break;
142  case 'd':
143  if(bottomRight.x + stepSize <= 1) {
144  topLeft.x += stepSize;
145  bottomRight.x += stepSize;
146  newConfig = true;
147  }
148  break;
149  case '1':
150  calculationAlgorithm = dai::SpatialLocationCalculatorAlgorithm::MEAN;
151  newConfig = true;
152  std::cout << "Switching calculation algorithm to MEAN!" << std::endl;
153  break;
154  case '2':
155  calculationAlgorithm = dai::SpatialLocationCalculatorAlgorithm::MIN;
156  newConfig = true;
157  std::cout << "Switching calculation algorithm to MIN!" << std::endl;
158  break;
159  case '3':
160  calculationAlgorithm = dai::SpatialLocationCalculatorAlgorithm::MAX;
161  newConfig = true;
162  std::cout << "Switching calculation algorithm to MAX!" << std::endl;
163  break;
164  case '4':
165  calculationAlgorithm = dai::SpatialLocationCalculatorAlgorithm::MODE;
166  newConfig = true;
167  std::cout << "Switching calculation algorithm to MODE!" << std::endl;
168  break;
169  case '5':
171  newConfig = true;
172  std::cout << "Switching calculation algorithm to MEDIAN!" << std::endl;
173  break;
174  default:
175  break;
176  }
177 
178  if(newConfig) {
179  config.roi = dai::Rect(topLeft, bottomRight);
180  config.calculationAlgorithm = calculationAlgorithm;
182  cfg.addROI(config);
183  spatialCalcConfigInQueue->send(cfg);
184  newConfig = false;
185  }
186  }
187  return 0;
188 }
dai::node::MonoCamera::out
Output out
Definition: MonoCamera.hpp:47
dai::node::XLinkOut
XLinkOut node. Sends messages over XLink.
Definition: XLinkOut.hpp:14
newConfig
static std::atomic< bool > newConfig
Definition: spatial_location_calculator.cpp:10
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::SpatialLocationCalculatorConfig
Definition: SpatialLocationCalculatorConfig.hpp:14
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::SpatialLocationCalculatorAlgorithm::MEDIAN
@ MEDIAN
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::SpatialLocationCalculatorAlgorithm::MAX
@ MAX
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::SpatialLocationCalculatorConfig::addROI
void addROI(SpatialLocationCalculatorConfigData &ROI)
Definition: SpatialLocationCalculatorConfig.cpp:18
dai::ImgFrame
Definition: ImgFrame.hpp:25
dai::SpatialLocationCalculatorData
Definition: SpatialLocationCalculatorData.hpp:14
stepSize
static constexpr float stepSize
Definition: spatial_location_calculator.cpp:8
dai::node::StereoDepth::PresetMode::HIGH_DENSITY
@ HIGH_DENSITY
subpixel
static std::atomic< bool > subpixel
Definition: depth_post_processing.cpp:9
dai::Device
Definition: Device.hpp:21
dai::SpatialLocationCalculatorAlgorithm::MEAN
@ MEAN
std
Definition: Node.hpp:366
dai::Point2f::x
float x
Definition: Point2f.hpp:19
dai::Rect
Definition: Rect.hpp:18
dai::SpatialLocationCalculatorConfigData::calculationAlgorithm
SpatialLocationCalculatorAlgorithm calculationAlgorithm
Definition: RawSpatialLocationCalculatorConfig.hpp:59
dai::SpatialLocationCalculatorAlgorithm::MODE
@ MODE
dai::node::XLinkIn
XLinkIn node. Receives messages over XLink.
Definition: XLinkIn.hpp:14
dai::Device::getInputQueue
std::shared_ptr< DataInputQueue > getInputQueue(const std::string &name)
Definition: Device.cpp:120
dai::Node::Output::link
void link(const Input &in)
Definition: Node.cpp:84
lrcheck
static std::atomic< bool > lrcheck
Definition: stereo_depth_video.cpp:10
dai::Point2f::y
float y
Definition: Point2f.hpp:19
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
dai::SpatialLocationCalculatorAlgorithm::MIN
@ MIN
main
int main()
Definition: spatial_location_calculator.cpp:12
utility.hpp


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