feature_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 #include "deque"
6 #include "unordered_map"
7 #include "unordered_set"
8 
9 static void drawFeatures(cv::Mat& frame, std::vector<dai::TrackedFeature>& features) {
10  static const auto pointColor = cv::Scalar(0, 0, 255);
11  static const int circleRadius = 2;
12  for(auto& feature : features) {
13  cv::circle(frame, cv::Point(feature.position.x, feature.position.y), circleRadius, pointColor, -1, cv::LINE_AA, 0);
14  }
15 }
16 
17 int main() {
18  using namespace std;
19 
20  // Create pipeline
21  dai::Pipeline pipeline;
22 
23  // Define sources and outputs
24  auto monoLeft = pipeline.create<dai::node::MonoCamera>();
25  auto monoRight = pipeline.create<dai::node::MonoCamera>();
26  auto featureTrackerLeft = pipeline.create<dai::node::FeatureTracker>();
27  auto featureTrackerRight = pipeline.create<dai::node::FeatureTracker>();
28 
29  auto xoutPassthroughFrameLeft = pipeline.create<dai::node::XLinkOut>();
30  auto xoutTrackedFeaturesLeft = pipeline.create<dai::node::XLinkOut>();
31  auto xoutPassthroughFrameRight = pipeline.create<dai::node::XLinkOut>();
32  auto xoutTrackedFeaturesRight = pipeline.create<dai::node::XLinkOut>();
33  auto xinTrackedFeaturesConfig = pipeline.create<dai::node::XLinkIn>();
34 
35  xoutPassthroughFrameLeft->setStreamName("passthroughFrameLeft");
36  xoutTrackedFeaturesLeft->setStreamName("trackedFeaturesLeft");
37  xoutPassthroughFrameRight->setStreamName("passthroughFrameRight");
38  xoutTrackedFeaturesRight->setStreamName("trackedFeaturesRight");
39  xinTrackedFeaturesConfig->setStreamName("trackedFeaturesConfig");
40 
41  // Properties
43  monoLeft->setCamera("left");
45  monoRight->setCamera("right");
46 
47  // Disable optical flow
48  featureTrackerLeft->initialConfig.setMotionEstimator(false);
49  featureTrackerRight->initialConfig.setMotionEstimator(false);
50 
51  // Linking
52  monoLeft->out.link(featureTrackerLeft->inputImage);
53  featureTrackerLeft->passthroughInputImage.link(xoutPassthroughFrameLeft->input);
54  featureTrackerLeft->outputFeatures.link(xoutTrackedFeaturesLeft->input);
55  xinTrackedFeaturesConfig->out.link(featureTrackerLeft->inputConfig);
56 
57  monoRight->out.link(featureTrackerRight->inputImage);
58  featureTrackerRight->passthroughInputImage.link(xoutPassthroughFrameRight->input);
59  featureTrackerRight->outputFeatures.link(xoutTrackedFeaturesRight->input);
60  xinTrackedFeaturesConfig->out.link(featureTrackerRight->inputConfig);
61 
62  auto featureTrackerConfig = featureTrackerRight->initialConfig.get();
63 
64  printf("Press 's' to switch between Harris and Shi-Thomasi corner detector! \n");
65 
66  // Connect to device and start pipeline
67  dai::Device device(pipeline);
68 
69  // Output queues used to receive the results
70  auto passthroughImageLeftQueue = device.getOutputQueue("passthroughFrameLeft", 8, false);
71  auto outputFeaturesLeftQueue = device.getOutputQueue("trackedFeaturesLeft", 8, false);
72  auto passthroughImageRightQueue = device.getOutputQueue("passthroughFrameRight", 8, false);
73  auto outputFeaturesRightQueue = device.getOutputQueue("trackedFeaturesRight", 8, false);
74 
75  auto inputFeatureTrackerConfigQueue = device.getInputQueue("trackedFeaturesConfig");
76 
77  const auto leftWindowName = "left";
78  const auto rightWindowName = "right";
79 
80  while(true) {
81  auto inPassthroughFrameLeft = passthroughImageLeftQueue->get<dai::ImgFrame>();
82  cv::Mat passthroughFrameLeft = inPassthroughFrameLeft->getFrame();
83  cv::Mat leftFrame;
84  cv::cvtColor(passthroughFrameLeft, leftFrame, cv::COLOR_GRAY2BGR);
85 
86  auto inPassthroughFrameRight = passthroughImageRightQueue->get<dai::ImgFrame>();
87  cv::Mat passthroughFrameRight = inPassthroughFrameRight->getFrame();
88  cv::Mat rightFrame;
89  cv::cvtColor(passthroughFrameRight, rightFrame, cv::COLOR_GRAY2BGR);
90 
91  auto trackedFeaturesLeft = outputFeaturesLeftQueue->get<dai::TrackedFeatures>()->trackedFeatures;
92  drawFeatures(leftFrame, trackedFeaturesLeft);
93 
94  auto trackedFeaturesRight = outputFeaturesRightQueue->get<dai::TrackedFeatures>()->trackedFeatures;
95  drawFeatures(rightFrame, trackedFeaturesRight);
96 
97  // Show the frame
98  cv::imshow(leftWindowName, leftFrame);
99  cv::imshow(rightWindowName, rightFrame);
100 
101  int key = cv::waitKey(1);
102  if(key == 'q') {
103  break;
104  } else if(key == 's') {
105  if(featureTrackerConfig.cornerDetector.type == dai::FeatureTrackerConfig::CornerDetector::Type::HARRIS) {
106  featureTrackerConfig.cornerDetector.type = dai::FeatureTrackerConfig::CornerDetector::Type::SHI_THOMASI;
107  printf("Switching to Shi-Thomasi \n");
108  } else {
109  featureTrackerConfig.cornerDetector.type = dai::FeatureTrackerConfig::CornerDetector::Type::HARRIS;
110  printf("Switching to Harris \n");
111  }
112  auto cfg = dai::FeatureTrackerConfig();
113  cfg.set(featureTrackerConfig);
114  inputFeatureTrackerConfigQueue->send(cfg);
115  }
116  }
117  return 0;
118 }
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
pointColor
static const auto pointColor
Definition: feature_tracker.cpp:10
dai::RawFeatureTrackerConfig::CornerDetector::Type::HARRIS
@ HARRIS
dai::node::MonoCamera
MonoCamera node. For use with grayscale sensors.
Definition: MonoCamera.hpp:17
dai::FeatureTrackerConfig::get
dai::RawFeatureTrackerConfig get() const
Definition: FeatureTrackerConfig.cpp:13
dai::node::FeatureTracker::inputConfig
Input inputConfig
Definition: FeatureTracker.hpp:43
dai::FeatureTrackerConfig::setMotionEstimator
FeatureTrackerConfig & setMotionEstimator(bool enable)
Definition: FeatureTrackerConfig.cpp:27
dai::TrackedFeatures
Definition: TrackedFeatures.hpp:14
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::FeatureTracker::outputFeatures
Output outputFeatures
Definition: FeatureTracker.hpp:53
dai::node::FeatureTracker::passthroughInputImage
Output passthroughInputImage
Definition: FeatureTracker.hpp:59
dai::RawFeatureTrackerConfig::CornerDetector::Type::SHI_THOMASI
@ SHI_THOMASI
dai::FeatureTrackerConfig
Definition: FeatureTrackerConfig.hpp:14
dai::node::MonoCamera::setResolution
void setResolution(Properties::SensorResolution resolution)
Set sensor resolution.
Definition: MonoCamera.cpp:82
dai::ImgFrame
Definition: ImgFrame.hpp:25
dai::Device
Definition: Device.hpp:21
std
Definition: Node.hpp:366
dai::node::FeatureTracker::inputImage
Input inputImage
Definition: FeatureTracker.hpp:48
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
main
int main()
Definition: feature_detector.cpp:17
drawFeatures
static void drawFeatures(cv::Mat &frame, std::vector< dai::TrackedFeature > &features)
Definition: feature_detector.cpp:9
dai::node::FeatureTracker::initialConfig
FeatureTrackerConfig initialConfig
Definition: FeatureTracker.hpp:37
dai::node::FeatureTracker
FeatureTracker node. Performs feature tracking and reidentification using motion estimation between 2...
Definition: FeatureTracker.hpp:20


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