depth_crop_control.cpp
Go to the documentation of this file.
1 
5 #include <iostream>
6 
7 // Includes common necessary includes for development using depthai library
8 #include "depthai/depthai.hpp"
9 
10 // Step size ('W','A','S','D' controls)
11 static constexpr float stepSize = 0.02f;
12 
13 int main() {
14  // Create pipeline
15  dai::Pipeline pipeline;
16 
17  // Define sources and outputs
18  auto monoRight = pipeline.create<dai::node::MonoCamera>();
19  auto monoLeft = pipeline.create<dai::node::MonoCamera>();
20  auto manip = pipeline.create<dai::node::ImageManip>();
21  auto stereo = pipeline.create<dai::node::StereoDepth>();
22 
23  auto configIn = pipeline.create<dai::node::XLinkIn>();
24  auto xout = pipeline.create<dai::node::XLinkOut>();
25 
26  configIn->setStreamName("config");
27  xout->setStreamName("depth");
28 
29  // Crop range
30  dai::Point2f topLeft(0.2f, 0.2f);
31  dai::Point2f bottomRight(0.8f, 0.8f);
32 
33  // Properties
34  monoRight->setCamera("right");
35  monoLeft->setCamera("left");
38 
39  manip->initialConfig.setCropRect(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y);
40  manip->setMaxOutputFrameSize(monoRight->getResolutionHeight() * monoRight->getResolutionWidth() * 3);
41  stereo->setDefaultProfilePreset(dai::node::StereoDepth::PresetMode::HIGH_DENSITY);
42 
43  // Linking
44  configIn->out.link(manip->inputConfig);
45  stereo->depth.link(manip->inputImage);
46  manip->out.link(xout->input);
47  monoRight->out.link(stereo->right);
48  monoLeft->out.link(stereo->left);
49 
50  // Connect to device and start pipeline
51  dai::Device device(pipeline);
52 
53  // Queues
54  auto q = device.getOutputQueue(xout->getStreamName(), 4, false);
55  auto configQueue = device.getInputQueue(configIn->getStreamName());
56 
57  bool sendCamConfig = false;
58 
59  while(true) {
60  auto inDepth = q->get<dai::ImgFrame>();
61  cv::Mat depthFrame = inDepth->getFrame(); // depthFrame values are in millimeters
62 
63  // Frame is transformed, the color map will be applied to highlight the depth info
64  cv::Mat depthFrameColor;
65  cv::normalize(depthFrame, depthFrameColor, 255, 0, cv::NORM_INF, CV_8UC1);
66  cv::equalizeHist(depthFrameColor, depthFrameColor);
67  cv::applyColorMap(depthFrameColor, depthFrameColor, cv::COLORMAP_HOT);
68 
69  // Frame is ready to be shown
70  cv::imshow("depth", depthFrameColor);
71 
72  // Update screen (10ms pooling rate)
73  int key = cv::waitKey(9);
74  cv::waitKey(1); // glitch workaround
75  if(key == 'q') {
76  break;
77  } else if(key == 'w') {
78  if(topLeft.y - stepSize >= 0) {
79  topLeft.y -= stepSize;
80  bottomRight.y -= stepSize;
81  sendCamConfig = true;
82  }
83  } else if(key == 'a') {
84  if(topLeft.x - stepSize >= 0) {
85  topLeft.x -= stepSize;
86  bottomRight.x -= stepSize;
87  sendCamConfig = true;
88  }
89  } else if(key == 's') {
90  if(bottomRight.y + stepSize <= 1) {
91  topLeft.y += stepSize;
92  bottomRight.y += stepSize;
93  sendCamConfig = true;
94  }
95  } else if(key == 'd') {
96  if(bottomRight.x + stepSize <= 1) {
97  topLeft.x += stepSize;
98  bottomRight.x += stepSize;
99  sendCamConfig = true;
100  }
101  }
102 
103  // Send new config to camera
104  if(sendCamConfig) {
106  cfg.setCropRect(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y);
107  configQueue->send(cfg);
108  sendCamConfig = false;
109  }
110  }
111  return 0;
112 }
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
StereoDepth node. Compute stereo disparity and depth from left-right image pair.
Definition: StereoDepth.hpp:15
dai::ImageManipConfig::setCropRect
ImageManipConfig & setCropRect(float xmin, float ymin, float xmax, float ymax)
Definition: ImageManipConfig.cpp:18
sendCamConfig
static std::atomic< bool > sendCamConfig
Definition: mono_camera_control.cpp:26
dai::node::MonoCamera
MonoCamera node. For use with grayscale sensors.
Definition: MonoCamera.hpp:17
dai::Point2f
Definition: Point2f.hpp:16
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
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::node::MonoCamera::setResolution
void setResolution(Properties::SensorResolution resolution)
Set sensor resolution.
Definition: MonoCamera.cpp:82
dai::ImgFrame
Definition: ImgFrame.hpp:25
dai::node::ImageManip
ImageManip node. Capability to crop, resize, warp, ... incoming image frames.
Definition: ImageManip.hpp:15
dai::node::StereoDepth::PresetMode::HIGH_DENSITY
@ HIGH_DENSITY
dai::Device
Definition: Device.hpp:21
dai::node::XLinkIn::getStreamName
std::string getStreamName() const
Get stream name.
Definition: XLinkIn.cpp:24
dai::Point2f::x
float x
Definition: Point2f.hpp:19
main
int main()
Definition: depth_crop_control.cpp:13
dai::node::XLinkIn
XLinkIn node. Receives messages over XLink.
Definition: XLinkIn.hpp:14
dai::ImageManipConfig
Definition: ImageManipConfig.hpp:23
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
dai::Point2f::y
float y
Definition: Point2f.hpp:19
stepSize
static constexpr float stepSize
Definition: depth_crop_control.cpp:11


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