summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2017-10-13 18:53:28 +0200
committerStanislaw Halik <sthalik@misaki.pl>2017-10-13 18:53:28 +0200
commit9da6dff1800d6b6598e6ad04465ee8b9cabb4167 (patch)
tree2f1692597748ce37d2ce147e8ed9f0c7d71d00e9
parent535e81402a65fa410e98899cd1780784d2f9815a (diff)
tracker/pt, options: fix threshold slider
It's only the tie_setting(slider_value, QSlider) that has race-free slider updates. Needed to update the threshold slider representation. Remove the tie_setting(int, QSlider) overload since it doesn't have the logic. Add a migration. Add base_value::notify() for use-cases like the checkbox updating the label.
-rw-r--r--migration/20171013_00-tracker-pt-threshold.cpp54
-rw-r--r--options/base-value.cpp5
-rw-r--r--options/base-value.hpp2
-rw-r--r--options/slider.hpp4
-rw-r--r--options/tie.cpp8
-rw-r--r--options/tie.hpp14
-rw-r--r--tracker-pt/ftnoir_tracker_pt_dialog.cpp38
-rw-r--r--tracker-pt/ftnoir_tracker_pt_dialog.h2
-rw-r--r--tracker-pt/ftnoir_tracker_pt_settings.h7
-rw-r--r--tracker-pt/point_extractor.cpp7
10 files changed, 112 insertions, 29 deletions
diff --git a/migration/20171013_00-tracker-pt-threshold.cpp b/migration/20171013_00-tracker-pt-threshold.cpp
new file mode 100644
index 00000000..4408ccc0
--- /dev/null
+++ b/migration/20171013_00-tracker-pt-threshold.cpp
@@ -0,0 +1,54 @@
+/* Copyright (c) 2017, 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.
+ */
+
+#include "migration.hpp"
+#include "options/options.hpp"
+#include "compat/util.hpp"
+//#include "tracker-pt/ftnoir_tracker_pt_settings.h"
+
+using namespace options;
+using namespace migrations;
+
+static constexpr const char* old_name = "threshold-primary";
+static constexpr const char* new_name = "threshold-slider";
+static constexpr const char* bundle_name = "tracker-pt";
+
+struct move_int_to_slider : migration
+{
+ QString unique_date() const override
+ {
+ return "20171013_00";
+ }
+
+
+ QString name() const override
+ {
+ return "tracker/pt threshold slider (int -> slider_value)";
+ }
+
+ bool should_run() const override
+ {
+ bundle b = make_bundle("tracker-pt");
+
+ return b->contains(old_name) && !b->contains(new_name);
+ }
+
+ void run() override
+ {
+ bundle b = make_bundle("tracker-pt");
+
+ value<int> old_val(b, old_name, 128);
+ value<slider_value> new_val(b, new_name, slider_value(128, 0, 255));
+
+ new_val = slider_value(old_val.to<int>(), 0, 255);
+
+ b->save();
+ }
+};
+
+OPENTRACK_MIGRATION(move_int_to_slider);
diff --git a/options/base-value.cpp b/options/base-value.cpp
index 29f4b496..a07886bb 100644
--- a/options/base-value.cpp
+++ b/options/base-value.cpp
@@ -16,6 +16,11 @@ base_value::~base_value()
b->on_value_destructed(self_name, this);
}
+void base_value::notify() const
+{
+ bundle_value_changed();
+}
+
void base_value::store(const QVariant& datum)
{
b->store_kv(self_name, datum);
diff --git a/options/base-value.hpp b/options/base-value.hpp
index 5106908d..dfb22670 100644
--- a/options/base-value.hpp
+++ b/options/base-value.hpp
@@ -41,6 +41,8 @@ public:
return static_cast<signal_sig<t>>(&base_value::valueChanged);
}
+ void notify() const;
+
signals:
OPENTRACK_DEFINE_SIGNAL(double);
OPENTRACK_DEFINE_SIGNAL(float);
diff --git a/options/slider.hpp b/options/slider.hpp
index bda1d398..da6bf214 100644
--- a/options/slider.hpp
+++ b/options/slider.hpp
@@ -8,6 +8,9 @@
#pragma once
#include "export.hpp"
+
+#include "compat/util.hpp"
+
#include <QMetaType>
#include <QDataStream>
#include <QDebug>
@@ -31,6 +34,7 @@ namespace options
slider_value& operator=(const slider_value& v);
bool operator==(const slider_value& v) const;
operator double() const { return cur_; }
+ explicit operator int() const { return iround(cur_); }
double cur() const { return cur_; }
double min() const { return min_; }
double max() const { return max_; }
diff --git a/options/tie.cpp b/options/tie.cpp
index 162a0f56..c0552d71 100644
--- a/options/tie.cpp
+++ b/options/tie.cpp
@@ -87,14 +87,6 @@ OTR_OPTIONS_EXPORT void tie_setting(value<int>& v, QSpinBox* sb)
base_value::connect(&v, SIGNAL(valueChanged(int)), sb, SLOT(setValue(int)), v.SAFE_CONNTYPE);
}
-OTR_OPTIONS_EXPORT void tie_setting(value<int>& v, QSlider* sl)
-{
- sl->setValue(v);
- v = sl->value();
- base_value::connect(sl, &QSlider::valueChanged, &v, base_value::signal_fun<int>(), v.DIRECT_CONNTYPE);
- base_value::connect(&v, base_value::signal_fun<int>(), sl, &QSlider::setValue, v.SAFE_CONNTYPE);
-}
-
OTR_OPTIONS_EXPORT void tie_setting(value<QString>& v, QLineEdit* le)
{
le->setText(v);
diff --git a/options/tie.hpp b/options/tie.hpp
index 0a4ace74..b6bf32e9 100644
--- a/options/tie.hpp
+++ b/options/tie.hpp
@@ -63,13 +63,25 @@ void tie_setting(value<t>& v, QLabel* lb, F&& fun)
v.SAFE_CONNTYPE);
}
+template<typename t, typename F>
+void tie_setting(value<t>& v, QObject* obj, F&& fun)
+{
+ if (obj == nullptr)
+ abort();
+
+ fun(v());
+
+ base_value::connect(&v, base_value::signal_fun<t>(),
+ obj, fun,
+ v.DIRECT_CONNTYPE);
+}
+
OTR_OPTIONS_EXPORT void tie_setting(value<int>& v, QComboBox* cb);
OTR_OPTIONS_EXPORT void tie_setting(value<QString>& v, QComboBox* cb);
OTR_OPTIONS_EXPORT void tie_setting(value<QVariant>& v, QComboBox* cb);
OTR_OPTIONS_EXPORT void tie_setting(value<bool>& v, QCheckBox* cb);
OTR_OPTIONS_EXPORT void tie_setting(value<double>& v, QDoubleSpinBox* dsb);
OTR_OPTIONS_EXPORT void tie_setting(value<int>& v, QSpinBox* sb);
-OTR_OPTIONS_EXPORT void tie_setting(value<int>& v, QSlider* sl);
OTR_OPTIONS_EXPORT void tie_setting(value<QString>& v, QLineEdit* le);
OTR_OPTIONS_EXPORT void tie_setting(value<QString>& v, QLabel* lb);
OTR_OPTIONS_EXPORT void tie_setting(value<int>& v, QTabWidget* t);
diff --git a/tracker-pt/ftnoir_tracker_pt_dialog.cpp b/tracker-pt/ftnoir_tracker_pt_dialog.cpp
index a4b8c668..4094f4dc 100644
--- a/tracker-pt/ftnoir_tracker_pt_dialog.cpp
+++ b/tracker-pt/ftnoir_tracker_pt_dialog.cpp
@@ -28,7 +28,7 @@ TrackerDialog_PT::TrackerDialog_PT()
tie_setting(s.cam_res_y, ui.res_y_spin);
tie_setting(s.cam_fps, ui.fps_spin);
- tie_setting(s.threshold, ui.threshold_slider);
+ tie_setting(s.threshold_slider, ui.threshold_slider);
tie_setting(s.min_point_size, ui.mindiam_spin);
tie_setting(s.max_point_size, ui.maxdiam_spin);
@@ -94,22 +94,32 @@ TrackerDialog_PT::TrackerDialog_PT()
tie_setting(s.blob_color, ui.blob_color);
- tie_setting(s.threshold, ui.threshold_value_display, [this](int x) {
- if (!s.auto_threshold)
- return tr("Brightness %1/255").arg(x);
- else
- {
- CamInfo info;
- int w = 640, h = 480;
+ tie_setting(s.threshold_slider, ui.threshold_value_display, [this](const slider_value& val) {
+ return threshold_display_text(int(val));
+ });
- if (tracker && tracker->get_cam_info(&info) && info.res_x * info.res_y != 0)
- w = info.res_x, h = info.res_y;
+ // refresh threshold display on auto-threshold checkbox state change
+ tie_setting(s.auto_threshold,
+ this,
+ [this](bool) { s.threshold_slider.notify(); });
+}
- double value = PointExtractor::threshold_radius_value(w, h, x);
+QString TrackerDialog_PT::threshold_display_text(int threshold_value)
+{
+ if (!s.auto_threshold)
+ return tr("Brightness %1/255").arg(threshold_value);
+ else
+ {
+ CamInfo info;
+ int w = 640, h = 480;
- return tr("LED radius %1 pixels").arg(value, 0, 'f', 2);
- }
- });
+ if (tracker && tracker->get_cam_info(&info) && info.res_x * info.res_y != 0)
+ w = info.res_x, h = info.res_y;
+
+ double value = PointExtractor::threshold_radius_value(w, h, threshold_value);
+
+ return tr("LED radius %1 pixels").arg(value, 0, 'f', 2);
+ }
}
void TrackerDialog_PT::startstop_trans_calib(bool start)
diff --git a/tracker-pt/ftnoir_tracker_pt_dialog.h b/tracker-pt/ftnoir_tracker_pt_dialog.h
index 59519601..c1c25fb0 100644
--- a/tracker-pt/ftnoir_tracker_pt_dialog.h
+++ b/tracker-pt/ftnoir_tracker_pt_dialog.h
@@ -38,6 +38,8 @@ public slots:
signals:
void poll_tracker_info();
private:
+ QString threshold_display_text(int threshold_value);
+
settings_pt s;
Tracker_PT* tracker;
QTimer timer, calib_timer;
diff --git a/tracker-pt/ftnoir_tracker_pt_settings.h b/tracker-pt/ftnoir_tracker_pt_settings.h
index 6bbba16b..8250edba 100644
--- a/tracker-pt/ftnoir_tracker_pt_settings.h
+++ b/tracker-pt/ftnoir_tracker_pt_settings.h
@@ -26,8 +26,7 @@ struct settings_pt : opts
value<QString> camera_name;
value<int> cam_res_x,
cam_res_y,
- cam_fps,
- threshold;
+ cam_fps;
value<double> min_point_size, max_point_size;
value<int> m01_x, m01_y, m01_z;
@@ -45,13 +44,15 @@ struct settings_pt : opts
value<bool> auto_threshold;
value<pt_color_type> blob_color;
+ value<slider_value> threshold_slider;
+
settings_pt() :
opts("tracker-pt"),
camera_name(b, "camera-name", ""),
cam_res_x(b, "camera-res-width", 640),
cam_res_y(b, "camera-res-height", 480),
cam_fps(b, "camera-fps", 30),
- threshold(b, "threshold-primary", 128),
+ threshold_slider(b, "threshold-slider", slider_value(128, 0, 255)),
min_point_size(b, "min-point-size", 1),
max_point_size(b, "max-point-size", 50),
m01_x(b, "m_01-x", 0),
diff --git a/tracker-pt/point_extractor.cpp b/tracker-pt/point_extractor.cpp
index adb23d52..40054084 100644
--- a/tracker-pt/point_extractor.cpp
+++ b/tracker-pt/point_extractor.cpp
@@ -180,10 +180,11 @@ void PointExtractor::extract_points(const cv::Mat& frame, cv::Mat& preview_frame
const double region_size_min = s.min_point_size;
const double region_size_max = s.max_point_size;
+ const int threshold_slider_value = s.threshold_slider.to<int>();
+
if (!s.auto_threshold)
{
- const int thres = s.threshold;
- cv::threshold(frame_gray, frame_bin, thres, 255, cv::THRESH_BINARY);
+ cv::threshold(frame_gray, frame_bin, threshold_slider_value, 255, cv::THRESH_BINARY);
}
else
{
@@ -200,7 +201,7 @@ void PointExtractor::extract_points(const cv::Mat& frame, cv::Mat& preview_frame
(int const*) &hist_size,
&ranges);
- const double radius = threshold_radius_value(frame.cols, frame.rows, s.threshold);
+ const double radius = threshold_radius_value(frame.cols, frame.rows, threshold_slider_value);
float const* restrict_ptr ptr = reinterpret_cast<float const* restrict_ptr>(hist.ptr(0));
const unsigned area = uround(3 * M_PI * radius*radius);