mono_camera_control.cpp
Go to the documentation of this file.
1 
10 #include <iostream>
11 
12 // Includes common necessary includes for development using depthai library
13 #include "depthai/depthai.hpp"
14 
15 // Step size ('W','A','S','D' controls)
16 static constexpr float stepSize = 0.02f;
17 
18 // Manual exposure/focus set step
19 static constexpr int EXP_STEP = 500; // us
20 static constexpr int ISO_STEP = 50;
21 
22 static int clamp(int num, int v0, int v1) {
23  return std::max(v0, std::min(num, v1));
24 }
25 
26 static std::atomic<bool> sendCamConfig{false};
27 
28 int main() {
29  // Create pipeline
30  dai::Pipeline pipeline;
31 
32  // Define sources and outputs
33  auto monoRight = pipeline.create<dai::node::MonoCamera>();
34  auto monoLeft = pipeline.create<dai::node::MonoCamera>();
35  auto manipRight = pipeline.create<dai::node::ImageManip>();
36  auto manipLeft = pipeline.create<dai::node::ImageManip>();
37 
38  auto controlIn = pipeline.create<dai::node::XLinkIn>();
39  auto configIn = pipeline.create<dai::node::XLinkIn>();
40  auto manipOutRight = pipeline.create<dai::node::XLinkOut>();
41  auto manipOutLeft = pipeline.create<dai::node::XLinkOut>();
42 
43  controlIn->setStreamName("control");
44  configIn->setStreamName("config");
45  manipOutRight->setStreamName("right");
46  manipOutLeft->setStreamName("left");
47 
48  // Crop range
49  dai::Point2f topLeft(0.2f, 0.2f);
50  dai::Point2f bottomRight(0.8f, 0.8f);
51 
52  // Properties
53  monoRight->setCamera("right");
54  monoLeft->setCamera("left");
57  manipRight->initialConfig.setCropRect(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y);
58  manipLeft->initialConfig.setCropRect(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y);
59 
60  // Linking
61  monoRight->out.link(manipRight->inputImage);
62  monoLeft->out.link(manipLeft->inputImage);
63  controlIn->out.link(monoRight->inputControl);
64  controlIn->out.link(monoLeft->inputControl);
65  configIn->out.link(manipRight->inputConfig);
66  configIn->out.link(manipLeft->inputConfig);
67  manipRight->out.link(manipOutRight->input);
68  manipLeft->out.link(manipOutLeft->input);
69 
70  // Connect to device and start pipeline
71  dai::Device device(pipeline);
72 
73  // Output queues will be used to get the grayscale frames
74  auto qRight = device.getOutputQueue(manipOutRight->getStreamName(), 4, false);
75  auto qLeft = device.getOutputQueue(manipOutLeft->getStreamName(), 4, false);
76  auto controlQueue = device.getInputQueue(controlIn->getStreamName());
77  auto configQueue = device.getInputQueue(configIn->getStreamName());
78 
79  // Defaults and limits for manual focus/exposure controls
80  int exp_time = 20000;
81  int exp_min = 1;
82  int exp_max = 33000;
83 
84  int sens_iso = 800;
85  int sens_min = 100;
86  int sens_max = 1600;
87 
88  while(true) {
89  auto inRight = qRight->get<dai::ImgFrame>();
90  auto inLeft = qLeft->get<dai::ImgFrame>();
91  cv::imshow("right", inRight->getCvFrame());
92  cv::imshow("left", inLeft->getCvFrame());
93 
94  // Update screen (1ms pooling rate)
95  int key = cv::waitKey(1);
96  if(key == 'q') {
97  break;
98  } else if(key == 'e') {
99  printf("Autoexposure enable\n");
100  dai::CameraControl ctrl;
101  ctrl.setAutoExposureEnable();
102  controlQueue->send(ctrl);
103  } else if(key == 'i' || key == 'o' || key == 'k' || key == 'l') {
104  if(key == 'i') exp_time -= EXP_STEP;
105  if(key == 'o') exp_time += EXP_STEP;
106  if(key == 'k') sens_iso -= ISO_STEP;
107  if(key == 'l') sens_iso += ISO_STEP;
108  exp_time = clamp(exp_time, exp_min, exp_max);
109  sens_iso = clamp(sens_iso, sens_min, sens_max);
110  printf("Setting manual exposure, time: %d, iso: %d\n", exp_time, sens_iso);
111  dai::CameraControl ctrl;
112  ctrl.setManualExposure(exp_time, sens_iso);
113  controlQueue->send(ctrl);
114  } else if(key == 'w') {
115  if(topLeft.y - stepSize >= 0) {
116  topLeft.y -= stepSize;
117  bottomRight.y -= stepSize;
118  sendCamConfig = true;
119  }
120  } else if(key == 'a') {
121  if(topLeft.x - stepSize >= 0) {
122  topLeft.x -= stepSize;
123  bottomRight.x -= stepSize;
124  sendCamConfig = true;
125  }
126  } else if(key == 's') {
127  if(bottomRight.y + stepSize <= 1) {
128  topLeft.y += stepSize;
129  bottomRight.y += stepSize;
130  sendCamConfig = true;
131  }
132  } else if(key == 'd') {
133  if(bottomRight.x + stepSize <= 1) {
134  topLeft.x += stepSize;
135  bottomRight.x += stepSize;
136  sendCamConfig = true;
137  }
138  }
139 
140  // Send new config to camera
141  if(sendCamConfig) {
143  cfg.setCropRect(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y);
144  configQueue->send(cfg);
145  sendCamConfig = false;
146  }
147  }
148  return 0;
149 }
dai::node::XLinkIn::out
Output out
Definition: XLinkIn.hpp:25
dai::node::MonoCamera::out
Output out
Definition: MonoCamera.hpp:47
dai::node::XLinkOut
XLinkOut node. Sends messages over XLink.
Definition: XLinkOut.hpp:14
EXP_STEP
static constexpr int EXP_STEP
Definition: mono_camera_control.cpp:19
dai::node::XLinkOut::getStreamName
std::string getStreamName() const
Get stream name.
Definition: XLinkOut.cpp:25
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::node::ImageManip::inputImage
Input inputImage
Definition: ImageManip.hpp:46
clamp
static int clamp(int num, int v0, int v1)
Definition: mono_camera_control.cpp:22
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::CameraControl::setManualExposure
CameraControl & setManualExposure(uint32_t exposureTimeUs, uint32_t sensitivityIso)
Definition: CameraControl.cpp:137
dai::node::MonoCamera
MonoCamera node. For use with grayscale sensors.
Definition: MonoCamera.hpp:17
dai::CameraControl
Definition: CameraControl.hpp:33
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_720_P
@ THE_720_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::node::XLinkIn::setStreamName
void setStreamName(const std::string &name)
Definition: XLinkIn.cpp:12
dai::node::ImageManip::inputConfig
Input inputConfig
Definition: ImageManip.hpp:40
dai::node::MonoCamera::setResolution
void setResolution(Properties::SensorResolution resolution)
Set sensor resolution.
Definition: MonoCamera.cpp:82
dai::node::ImageManip::out
Output out
Definition: ImageManip.hpp:51
dai::ImgFrame::getCvFrame
void getCvFrame(T...)
Definition: ImgFrame.hpp:222
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::MonoCamera::inputControl
Input inputControl
Definition: MonoCamera.hpp:40
dai::Device
Definition: Device.hpp:21
main
int main()
Definition: mono_camera_control.cpp:28
stepSize
static constexpr float stepSize
Definition: mono_camera_control.cpp:16
dai::node::ImageManip::initialConfig
ImageManipConfig initialConfig
Definition: ImageManip.hpp:34
dai::node::XLinkIn::getStreamName
std::string getStreamName() const
Get stream name.
Definition: XLinkIn.cpp:24
dai::Point2f::x
float x
Definition: Point2f.hpp:19
dai::CameraControl::setAutoExposureEnable
CameraControl & setAutoExposureEnable()
Definition: CameraControl.cpp:101
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
dai::node::XLinkOut::setStreamName
void setStreamName(const std::string &name)
Definition: XLinkOut.cpp:13
ISO_STEP
static constexpr int ISO_STEP
Definition: mono_camera_control.cpp:20


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