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
33+ monoLeft->setResolution (dai::MonoCameraProperties::SensorResolution::THE_400_P);
34+ monoLeft->setBoardSocket (dai::CameraBoardSocket::LEFT);
35+ monoRight->setResolution (dai::MonoCameraProperties::SensorResolution::THE_400_P);
36+ monoRight->setBoardSocket (dai::CameraBoardSocket::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++) {
45+ dai::SpatialLocationCalculatorConfigData config;
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);
64+ device.setIrLaserDotProjectorBrightness (1000 );
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+ }
109+ return 0 ;
110+ }
0 commit comments