apriltag.cpp
Go to the documentation of this file.
1 #include <chrono>
2 #include <iostream>
3 
4 // Inludes common necessary includes for development using depthai library
5 #include "depthai/depthai.hpp"
6 
7 int main() {
8  using namespace std;
9  using namespace std::chrono;
10 
11  // Create pipeline
12  dai::Pipeline pipeline;
13 
14  // Define sources and outputs
15  auto monoLeft = pipeline.create<dai::node::MonoCamera>();
16  auto aprilTag = pipeline.create<dai::node::AprilTag>();
17 
18  auto xoutMono = pipeline.create<dai::node::XLinkOut>();
19  auto xoutAprilTag = pipeline.create<dai::node::XLinkOut>();
20 
21  xoutMono->setStreamName("mono");
22  xoutAprilTag->setStreamName("aprilTagData");
23 
24  // Properties
26  monoLeft->setCamera("left");
27 
28  aprilTag->initialConfig.setFamily(dai::AprilTagConfig::Family::TAG_36H11);
29 
30  // Linking
31  aprilTag->passthroughInputImage.link(xoutMono->input);
32  monoLeft->out.link(aprilTag->inputImage);
33  aprilTag->out.link(xoutAprilTag->input);
34  // always take the latest frame as apriltag detections are slow
35  aprilTag->inputImage.setBlocking(false);
36  aprilTag->inputImage.setQueueSize(1);
37 
38  // advanced settings, configurable at runtime
39  auto aprilTagConfig = aprilTag->initialConfig.get();
40  aprilTagConfig.quadDecimate = 4;
41  aprilTagConfig.quadSigma = 0;
42  aprilTagConfig.refineEdges = true;
43  aprilTagConfig.decodeSharpening = 0.25;
44  aprilTagConfig.maxHammingDistance = 1;
45  aprilTagConfig.quadThresholds.minClusterPixels = 5;
46  aprilTagConfig.quadThresholds.maxNmaxima = 10;
47  aprilTagConfig.quadThresholds.criticalDegree = 10;
48  aprilTagConfig.quadThresholds.maxLineFitMse = 10;
49  aprilTagConfig.quadThresholds.minWhiteBlackDiff = 5;
50  aprilTagConfig.quadThresholds.deglitch = false;
51  aprilTag->initialConfig.set(aprilTagConfig);
52 
53  // Connect to device and start pipeline
54  dai::Device device(pipeline);
55 
56  // Output queue will be used to get the mono frames from the outputs defined above
57  auto monoQueue = device.getOutputQueue("mono", 8, false);
58  auto aprilTagQueue = device.getOutputQueue("aprilTagData", 8, false);
59 
60  auto color = cv::Scalar(0, 255, 0);
61 
62  auto startTime = steady_clock::now();
63  int counter = 0;
64  float fps = 0;
65 
66  while(true) {
67  auto inFrame = monoQueue->get<dai::ImgFrame>();
68 
69  counter++;
70  auto currentTime = steady_clock::now();
71  auto elapsed = duration_cast<duration<float>>(currentTime - startTime);
72  if(elapsed > seconds(1)) {
73  fps = counter / elapsed.count();
74  counter = 0;
75  startTime = currentTime;
76  }
77 
78  cv::Mat monoFrame = inFrame->getFrame();
79  cv::Mat frame;
80  cv::cvtColor(monoFrame, frame, cv::COLOR_GRAY2BGR);
81 
82  auto aprilTagData = aprilTagQueue->get<dai::AprilTags>()->aprilTags;
83  for(auto aprilTag : aprilTagData) {
84  auto& topLeft = aprilTag.topLeft;
85  auto& topRight = aprilTag.topRight;
86  auto& bottomRight = aprilTag.bottomRight;
87  auto& bottomLeft = aprilTag.bottomLeft;
88 
89  cv::Point center = cv::Point((topLeft.x + bottomRight.x) / 2, (topLeft.y + bottomRight.y) / 2);
90 
91  cv::line(frame, cv::Point(topLeft.x, topLeft.y), cv::Point(topRight.x, topRight.y), color, 2, cv::LINE_AA, 0);
92  cv::line(frame, cv::Point(topRight.x, topRight.y), cv::Point(bottomRight.x, bottomRight.y), color, 2, cv::LINE_AA, 0);
93  cv::line(frame, cv::Point(bottomRight.x, bottomRight.y), cv::Point(bottomLeft.x, bottomLeft.y), color, 2, cv::LINE_AA, 0);
94  cv::line(frame, cv::Point(bottomLeft.x, bottomLeft.y), cv::Point(topLeft.x, topLeft.y), color, 2, cv::LINE_AA, 0);
95 
96  std::stringstream idStr;
97  idStr << "ID: " << aprilTag.id;
98  cv::putText(frame, idStr.str(), center, cv::FONT_HERSHEY_TRIPLEX, 0.5, color);
99  }
100 
101  std::stringstream fpsStr;
102  fpsStr << "fps:" << std::fixed << std::setprecision(2) << fps;
103  cv::putText(frame, fpsStr.str(), cv::Point(2, inFrame->getHeight() - 4), cv::FONT_HERSHEY_TRIPLEX, 0.4, color);
104 
105  cv::imshow("mono", frame);
106 
107  int key = cv::waitKey(1);
108  if(key == 'q') {
109  return 0;
110  }
111  }
112  return 0;
113 }
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::id
const Id id
Id of node.
Definition: Node.hpp:288
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
fps
static constexpr int fps
Definition: rgb_depth_aligned.cpp:12
dai::node::MonoCamera
MonoCamera node. For use with grayscale sensors.
Definition: MonoCamera.hpp:17
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::XLinkOut::input
Input input
Definition: XLinkOut.hpp:27
depthai.hpp
dai::Pipeline::create
std::shared_ptr< N > create()
Definition: Pipeline.hpp:145
dai::AprilTags
Definition: AprilTags.hpp:14
dai::ImgFrame
Definition: ImgFrame.hpp:25
dai::Device
Definition: Device.hpp:21
std
Definition: Node.hpp:366
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::AprilTag
AprilTag node.
Definition: AprilTag.hpp:16
main
int main()
Definition: apriltag.cpp:7
dai::RawAprilTagConfig::Family::TAG_36H11
@ TAG_36H11


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