summaryrefslogtreecommitdiffhomepage
path: root/tracker-aruco/ftnoir_tracker_aruco.h
blob: 1d6fd107d19df3a55e25277660dd61a7f5ddd702 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* Copyright (c) 2013-2015 Stanislaw Halik <sthalik@misaki.pl>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 */

#pragma once

#include "ui_aruco-trackercontrols.h"
#include "options/options.hpp"
#include "cv/translation-calibrator.hpp"
#include "api/plugin-api.hpp"
#include "cv/video-widget.hpp"
#include "compat/timer.hpp"

#include "aruco/markerdetector.h"

#include <QObject>
#include <QThread>
#include <QMutex>
#include <QHBoxLayout>
#include <QDialog>
#include <QTimer>

#include <memory>
#include <cinttypes>

#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>

// value 0->1
//#define DEBUG_UNSHARP_MASKING .75

//canny thresholding
//#define USE_EXPERIMENTAL_CANNY

using namespace options;

enum aruco_fps
{
    fps_default = 0,
    fps_30      = 1,
    fps_60      = 2,
    fps_75      = 3,
    fps_125     = 4,
    fps_200     = 5,
    fps_50      = 6,
    fps_100     = 7,
    fps_120     = 8,
    fps_MAX     = 9,
};

struct settings : opts {
    value<double> headpos_x { b, "headpos-x", 0 },
                  headpos_y { b, "headpos-y", 0 },
                  headpos_z { b, "headpos-z", 0 };

    value<QString> camera_name { b, "camera-name", ""};
    value<int> resolution { b, "force-resolution", 0 };
    value<int> fov { b, "field-of-view", 56 };
    value<aruco_fps> force_fps { b, "force-fps", fps_default };

    settings();
};

class aruco_tracker : protected virtual QThread, public ITracker
{
    Q_OBJECT
    static constexpr float c_search_window = 1.3f;
public:
    aruco_tracker();
    ~aruco_tracker() override;
    module_status start_tracker(QFrame* frame) override;
    void data(double *data) override;
    void run() override;

    void getRT(cv::Matx33d &r, cv::Vec3d &t);
    QMutex camera_mtx;
    cv::VideoCapture camera;

private:
    bool detect_with_roi();
    bool detect_without_roi();
    bool open_camera();
    void set_intrinsics();
    void update_fps();
    void draw_ar(bool ok);
    void clamp_last_roi();
    void set_points();
    void draw_centroid();
    void set_last_roi();
    void set_rmat();
    void set_roi_from_projection();
    void set_detector_params();
    void cycle_detection_params();

    QMutex mtx;
    std::unique_ptr<cv_video_widget> videoWidget;
    std::unique_ptr<QHBoxLayout> layout;
    settings s;
    double pose[6] {}, fps = 0;
    double no_detection_timeout = 0;
    cv::Matx33d r;
    cv::Matx33d intrinsics = cv::Matx33d::eye();
    cv::Vec3d t;
    cv::Vec3d rvec, tvec;
    cv::Matx33d m_r, m_q, rmat = cv::Matx33d::eye();
    cv::Vec3d euler;
    std::vector<cv::Point3f> roi_points {4};
    std::vector<cv::Point2f> roi_projection;
    std::vector<cv::Point2f> repr2;
    std::vector<cv::Point3f> obj_points {4};
    aruco::MarkerDetector detector;
    std::vector<aruco::Marker> markers;
    cv::Mat frame, grayscale, color;
    cv::Rect last_roi { 65535, 65535, 0, 0 };
    Timer fps_timer, last_detection_timer;
    unsigned adaptive_size_pos { 0 };
    bool use_otsu = false;

#if !defined USE_EXPERIMENTAL_CANNY
    static constexpr int adaptive_thres = 6;
#else
    static constexpr int adaptive_thres = 3;
#endif

    static constexpr double timeout = .35;
    static constexpr double timeout_backoff_c = .25;

    static constexpr float size_min = 0.05f;
    static constexpr float size_max = 0.5f;

    static constexpr double RC = .25;

#ifdef DEBUG_UNSHARP_MASKING
    static constexpr double gauss_kernel_size = 3;
    cv::Mat blurred;
#endif
};

class aruco_dialog : public ITrackerDialog
{
    Q_OBJECT
public:
    aruco_dialog();
    void register_tracker(ITracker * x) override { tracker = static_cast<aruco_tracker*>(x); }
    void unregister_tracker() override { tracker = nullptr; }
private:
    void make_fps_combobox();

    Ui::Form ui;
    aruco_tracker* tracker;
    settings s;
    TranslationCalibrator calibrator;
    QTimer calib_timer;
private Q_SLOTS:
    void doOK();
    void doCancel();
    void toggleCalibrate();
    void cleanupCalib();
    void update_tracker_calibration();
    void camera_settings();
    void update_camera_settings_state(const QString& name);
};

class aruco_metadata : public Metadata
{
    Q_OBJECT
    QString name() override { return QString("aruco -- paper marker tracker"); }
    QIcon icon() override { return QIcon(":/images/aruco.png"); }
};