edge_detector.cpp
Go to the documentation of this file.
1 #include <iostream>
2 
3 // Includes common necessary includes for development using depthai library
4 #include "depthai/depthai.hpp"
5 
6 int main() {
7  using namespace std;
8 
9  // Create pipeline
10  dai::Pipeline pipeline;
11 
12  // Define sources and outputs
13  auto camRgb = pipeline.create<dai::node::ColorCamera>();
14  auto monoLeft = pipeline.create<dai::node::MonoCamera>();
15  auto monoRight = pipeline.create<dai::node::MonoCamera>();
16 
17  auto edgeDetectorLeft = pipeline.create<dai::node::EdgeDetector>();
18  auto edgeDetectorRight = pipeline.create<dai::node::EdgeDetector>();
19  auto edgeDetectorRgb = pipeline.create<dai::node::EdgeDetector>();
20 
21  auto xoutEdgeLeft = pipeline.create<dai::node::XLinkOut>();
22  auto xoutEdgeRight = pipeline.create<dai::node::XLinkOut>();
23  auto xoutEdgeRgb = pipeline.create<dai::node::XLinkOut>();
24  auto xinEdgeCfg = pipeline.create<dai::node::XLinkIn>();
25 
26  const auto edgeLeftStr = "edge left";
27  const auto edgeRightStr = "edge right";
28  const auto edgeRgbStr = "edge rgb";
29  const auto edgeCfgStr = "edge cfg";
30 
31  xoutEdgeLeft->setStreamName(edgeLeftStr);
32  xoutEdgeRight->setStreamName(edgeRightStr);
33  xoutEdgeRgb->setStreamName(edgeRgbStr);
34  xinEdgeCfg->setStreamName(edgeCfgStr);
35 
36  // Properties
37  camRgb->setBoardSocket(dai::CameraBoardSocket::CAM_A);
39 
41  monoLeft->setCamera("left");
43  monoRight->setCamera("right");
44 
45  edgeDetectorRgb->setMaxOutputFrameSize(camRgb->getVideoWidth() * camRgb->getVideoHeight());
46 
47  // Linking
48  monoLeft->out.link(edgeDetectorLeft->inputImage);
49  monoRight->out.link(edgeDetectorRight->inputImage);
50  camRgb->video.link(edgeDetectorRgb->inputImage);
51 
52  edgeDetectorLeft->outputImage.link(xoutEdgeLeft->input);
53  edgeDetectorRight->outputImage.link(xoutEdgeRight->input);
54  edgeDetectorRgb->outputImage.link(xoutEdgeRgb->input);
55 
56  xinEdgeCfg->out.link(edgeDetectorLeft->inputConfig);
57  xinEdgeCfg->out.link(edgeDetectorRight->inputConfig);
58  xinEdgeCfg->out.link(edgeDetectorRgb->inputConfig);
59 
60  // Connect to device and start pipeline
61  dai::Device device(pipeline);
62 
63  // Output/input queues
64  auto edgeLeftQueue = device.getOutputQueue(edgeLeftStr, 8, false);
65  auto edgeRightQueue = device.getOutputQueue(edgeRightStr, 8, false);
66  auto edgeRgbQueue = device.getOutputQueue(edgeRgbStr, 8, false);
67  auto edgeCfgQueue = device.getInputQueue(edgeCfgStr);
68 
69  std::cout << "Switch between sobel filter kernels using keys '1' and '2'" << std::endl;
70 
71  while(true) {
72  auto edgeLeft = edgeLeftQueue->get<dai::ImgFrame>();
73  auto edgeRight = edgeRightQueue->get<dai::ImgFrame>();
74  auto edgeRgb = edgeRgbQueue->get<dai::ImgFrame>();
75 
76  cv::Mat edgeLeftFrame = edgeLeft->getFrame();
77  cv::Mat edgeRightFrame = edgeRight->getFrame();
78  cv::Mat edgeRgbFrame = edgeRgb->getFrame();
79 
80  // Show the frame
81  cv::imshow(edgeLeftStr, edgeLeftFrame);
82  cv::imshow(edgeRightStr, edgeRightFrame);
83  cv::imshow(edgeRgbStr, edgeRgbFrame);
84 
85  int key = cv::waitKey(1);
86  switch(key) {
87  case 'q':
88  return 0;
89  break;
90 
91  case '1': {
92  std::cout << "Switching sobel filter kernel." << std::endl;
94  std::vector<std::vector<int>> sobelHorizontalKernel = {{1, 0, -1}, {2, 0, -2}, {1, 0, -1}};
95  std::vector<std::vector<int>> sobelVerticalKernel = {{1, 2, 1}, {0, 0, 0}, {-1, -2, -1}};
96  cfg.setSobelFilterKernels(sobelHorizontalKernel, sobelVerticalKernel);
97  edgeCfgQueue->send(cfg);
98  } break;
99 
100  case '2': {
101  std::cout << "Switching sobel filter kernel." << std::endl;
103  std::vector<std::vector<int>> sobelHorizontalKernel = {{3, 0, -3}, {10, 0, -10}, {3, 0, -3}};
104  std::vector<std::vector<int>> sobelVerticalKernel = {{3, 10, 3}, {0, 0, 0}, {-3, -10, -3}};
105  cfg.setSobelFilterKernels(sobelHorizontalKernel, sobelVerticalKernel);
106  edgeCfgQueue->send(cfg);
107  } break;
108 
109  default:
110  break;
111  }
112  }
113  return 0;
114 }
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::ColorCamera::setResolution
void setResolution(Properties::SensorResolution resolution)
Set sensor resolution.
Definition: ColorCamera.cpp:169
dai::EdgeDetectorConfig::setSobelFilterKernels
void setSobelFilterKernels(const std::vector< std::vector< int >> &horizontalKernel, const std::vector< std::vector< int >> &verticalKernel)
Definition: EdgeDetectorConfig.cpp:13
dai::CameraBoardSocket::CAM_A
@ CAM_A
dai::node::MonoCamera
MonoCamera node. For use with grayscale sensors.
Definition: MonoCamera.hpp:17
dai::node::ColorCamera
ColorCamera node. For use with color sensors.
Definition: ColorCamera.hpp:16
dai::EdgeDetectorConfig
Definition: EdgeDetectorConfig.hpp:14
dai::node::EdgeDetector::setMaxOutputFrameSize
void setMaxOutputFrameSize(int maxFrameSize)
Definition: EdgeDetector.cpp:36
main
int main()
Definition: edge_detector.cpp:6
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::EdgeDetector
EdgeDetector node. Performs edge detection using 3x3 Sobel filter.
Definition: EdgeDetector.hpp:19
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::MonoCamera::setResolution
void setResolution(Properties::SensorResolution resolution)
Set sensor resolution.
Definition: MonoCamera.cpp:82
dai::ImgFrame
Definition: ImgFrame.hpp:25
dai::ColorCameraProperties::SensorResolution::THE_1080_P
@ THE_1080_P
1920 × 1080
dai::Device
Definition: Device.hpp:21
std
Definition: Node.hpp:366
dai::node::EdgeDetector::outputImage
Output outputImage
Definition: EdgeDetector.hpp:52
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
dai::node::XLinkOut::setStreamName
void setStreamName(const std::string &name)
Definition: XLinkOut.cpp:13
dai::node::EdgeDetector::inputConfig
Input inputConfig
Definition: EdgeDetector.hpp:42
dai::node::EdgeDetector::inputImage
Input inputImage
Definition: EdgeDetector.hpp:47
dai::node::ColorCamera::setCamera
void setCamera(std::string name)
Definition: ColorCamera.cpp:44


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