summaryrefslogtreecommitdiffhomepage
path: root/ftnoir_tracker_pt
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2015-07-25 11:43:10 +0200
committerStanislaw Halik <sthalik@misaki.pl>2015-07-25 11:43:10 +0200
commit10ac081b3c4ebb9b29b81b30f5efe6622f4212bc (patch)
treefedfe7efc494145d0b46e91ba5a94c47cb0c1b6f /ftnoir_tracker_pt
parent6baa8ca1070612e5d7cbae7aa62cbe4a9e51ad40 (diff)
parent24538cf3a3a91481851618791b11be81437563e4 (diff)
Merge branch 'unstable' into trackhat-ui
Diffstat (limited to 'ftnoir_tracker_pt')
-rw-r--r--ftnoir_tracker_pt/camera.cpp12
-rw-r--r--ftnoir_tracker_pt/ftnoir_tracker_pt.cpp17
-rw-r--r--ftnoir_tracker_pt/ftnoir_tracker_pt.h2
-rw-r--r--ftnoir_tracker_pt/point_extractor.cpp42
-rw-r--r--ftnoir_tracker_pt/point_tracker.cpp71
-rw-r--r--ftnoir_tracker_pt/point_tracker.h2
-rw-r--r--ftnoir_tracker_pt/pt_video_widget.cpp3
7 files changed, 67 insertions, 82 deletions
diff --git a/ftnoir_tracker_pt/camera.cpp b/ftnoir_tracker_pt/camera.cpp
index 2989c1fe..9e11b815 100644
--- a/ftnoir_tracker_pt/camera.cpp
+++ b/ftnoir_tracker_pt/camera.cpp
@@ -8,9 +8,7 @@
#include "camera.h"
#include <string>
#include <QDebug>
-#include "opentrack/sleep.hpp"
-
-using namespace cv;
+#include "opentrack-compat/sleep.hpp"
void Camera::set_device_index(int index)
{
@@ -74,7 +72,7 @@ void CVCamera::start()
{
if (cap)
delete cap;
- cap = new VideoCapture(desired_index);
+ cap = new cv::VideoCapture(desired_index);
_set_res();
_set_fps();
// extract camera info
@@ -103,11 +101,11 @@ void CVCamera::stop()
active = false;
}
-bool CVCamera::_get_frame(Mat* frame)
+bool CVCamera::_get_frame(cv::Mat* frame)
{
if (cap && cap->isOpened())
{
- Mat img;
+ cv::Mat img;
for (int i = 0; i < 100 && !cap->read(img); i++)
;;
@@ -142,5 +140,5 @@ void CVCamera::_set_device_index()
cap->release();
delete cap;
}
- cap = new VideoCapture(desired_index);
+ cap = new cv::VideoCapture(desired_index);
}
diff --git a/ftnoir_tracker_pt/ftnoir_tracker_pt.cpp b/ftnoir_tracker_pt/ftnoir_tracker_pt.cpp
index 8c478824..07600646 100644
--- a/ftnoir_tracker_pt/ftnoir_tracker_pt.cpp
+++ b/ftnoir_tracker_pt/ftnoir_tracker_pt.cpp
@@ -13,9 +13,6 @@
#include <QCoreApplication>
#include "opentrack/camera-names.hpp"
-using namespace std;
-using namespace cv;
-
//#define PT_PERF_LOG //log performance
//-----------------------------------------------------------------------------
@@ -114,7 +111,7 @@ void Tracker_PT::run()
{
Affine X_CM = pose();
- Affine X_MH(Matx33f::eye(), cv::Vec3f(s.t_MH_x, s.t_MH_y, s.t_MH_z)); // just copy pasted these lines from below
+ Affine X_MH(cv::Matx33f::eye(), cv::Vec3f(s.t_MH_x, s.t_MH_y, s.t_MH_z)); // just copy pasted these lines from below
Affine X_GH = X_CM * X_MH;
cv::Vec3f p = X_GH.t; // head (center?) position in global space
float fx = get_focal_length();
@@ -210,17 +207,17 @@ void Tracker_PT::data(double *data)
{
Affine X_CM = pose();
- Affine X_MH(Matx33f::eye(), cv::Vec3f(s.t_MH_x, s.t_MH_y, s.t_MH_z));
+ Affine X_MH(cv::Matx33f::eye(), cv::Vec3f(s.t_MH_x, s.t_MH_y, s.t_MH_z));
Affine X_GH = X_CM * X_MH;
- Matx33f R = X_GH.R;
- Vec3f t = X_GH.t;
+ cv::Matx33f R = X_GH.R;
+ cv::Vec3f t = X_GH.t;
// translate rotation matrix from opengl (G) to roll-pitch-yaw (E) frame
// -z -> x, y -> z, x -> -y
- Matx33f R_EG(0, 0,-1,
- -1, 0, 0,
- 0, 1, 0);
+ cv::Matx33f R_EG(0, 0,-1,
+ -1, 0, 0,
+ 0, 1, 0);
R = R_EG * R * R_EG.t();
// extract rotation angles
diff --git a/ftnoir_tracker_pt/ftnoir_tracker_pt.h b/ftnoir_tracker_pt/ftnoir_tracker_pt.h
index b303a913..3291c7fc 100644
--- a/ftnoir_tracker_pt/ftnoir_tracker_pt.h
+++ b/ftnoir_tracker_pt/ftnoir_tracker_pt.h
@@ -14,7 +14,7 @@
#include "point_extractor.h"
#include "point_tracker.h"
#include "pt_video_widget.h"
-#include "opentrack/timer.hpp"
+#include "opentrack-compat/timer.hpp"
#include "opentrack/opencv-camera-dialog.hpp"
#include <QThread>
diff --git a/ftnoir_tracker_pt/point_extractor.cpp b/ftnoir_tracker_pt/point_extractor.cpp
index e81e3aa0..7174d719 100644
--- a/ftnoir_tracker_pt/point_extractor.cpp
+++ b/ftnoir_tracker_pt/point_extractor.cpp
@@ -9,13 +9,9 @@
#include <QDebug>
#ifdef DEBUG_EXTRACTION
-# include "opentrack/timer.hpp"
+# include "opentrack-compat/timer.hpp"
#endif
-using namespace cv;
-using namespace std;
-
-
PointExtractor::PointExtractor(){
//if (!AllocConsole()){}
//else SetConsoleTitle("debug");
@@ -23,20 +19,20 @@ PointExtractor::PointExtractor(){
//freopen("CON", "w", stderr);
}
// ----------------------------------------------------------------------------
-std::vector<Vec2f> PointExtractor::extract_points(Mat& frame)
+std::vector<cv::Vec2f> PointExtractor::extract_points(cv::Mat& frame)
{
- const int W = frame.cols;
- const int H = frame.rows;
+ const int W = frame.cols;
+ const int H = frame.rows;
+
+ // convert to grayscale
+ cv::Mat frame_gray;
+ cv::cvtColor(frame, frame_gray, cv::COLOR_RGB2GRAY);
- // convert to grayscale
- Mat frame_gray;
- cvtColor(frame, frame_gray, cv::COLOR_RGB2GRAY);
-
int min_size = s.min_point_size;
int max_size = s.max_point_size;
- unsigned int region_size_min = 3.14*min_size*min_size/4.0;
- unsigned int region_size_max = 3.14*max_size*max_size/4.0;
+ unsigned int region_size_min = 3.14*min_size*min_size/4.0;
+ unsigned int region_size_max = 3.14*max_size*max_size/4.0;
// testing indicates threshold difference of 45 from lowest to highest
// that's applicable to poor lighting conditions.
@@ -144,7 +140,7 @@ std::vector<Vec2f> PointExtractor::extract_points(Mat& frame)
};
// mask for everything that passes the threshold (or: the upper threshold of the hysteresis)
- Mat frame_bin = cv::Mat::zeros(H, W, CV_8U);
+ cv::Mat frame_bin = cv::Mat::zeros(H, W, CV_8U);
const int min = std::max(0, thres - diff/2);
const int max = std::min(255, thres + diff/2);
@@ -155,8 +151,8 @@ std::vector<Vec2f> PointExtractor::extract_points(Mat& frame)
// this code is based on OpenCV SimpleBlobDetector
for (int i = min; i < max; i += step)
{
- Mat frame_bin_;
- threshold(frame_gray, frame_bin_, i, 255, THRESH_BINARY);
+ cv::Mat frame_bin_;
+ cv::threshold(frame_gray, frame_bin_, i, 255, cv::THRESH_BINARY);
frame_bin.setTo(170, frame_bin_);
std::vector<std::vector<cv::Point>> contours;
@@ -211,19 +207,19 @@ std::vector<Vec2f> PointExtractor::extract_points(Mat& frame)
for (auto& b : simple_blob::merge(blobs))
{
auto pos = b.effective_pos();
- Vec2f p((pos[0] - W/2)/W, -(pos[1] - H/2)/W);
+ cv::Vec2f p((pos[0] - W/2)/W, -(pos[1] - H/2)/W);
points.push_back(p);
}
- vector<Mat> channels_;
+ std::vector<cv::Mat> channels_;
cv::split(frame, channels_);
// draw output image
- Mat frame_bin_ = frame_bin * .5;
- vector<Mat> channels;
+ cv::Mat frame_bin_ = frame_bin * .5;
+ std::vector<cv::Mat> channels;
channels.push_back(channels_[0] + frame_bin_);
channels.push_back(channels_[1] - frame_bin_);
channels.push_back(channels_[2] - frame_bin_);
- merge(channels, frame);
+ cv::merge(channels, frame);
- return points;
+ return points;
}
diff --git a/ftnoir_tracker_pt/point_tracker.cpp b/ftnoir_tracker_pt/point_tracker.cpp
index b4283d37..cedf1979 100644
--- a/ftnoir_tracker_pt/point_tracker.cpp
+++ b/ftnoir_tracker_pt/point_tracker.cpp
@@ -13,46 +13,43 @@
#include <QDebug>
-using namespace cv;
-using namespace std;
-
const float PI = 3.14159265358979323846f;
// ----------------------------------------------------------------------------
-static void get_row(const Matx33f& m, int i, Vec3f& v)
+static void get_row(const cv::Matx33f& m, int i, cv::Vec3f& v)
{
v[0] = m(i,0);
v[1] = m(i,1);
v[2] = m(i,2);
}
-static void set_row(Matx33f& m, int i, const Vec3f& v)
+static void set_row(cv::Matx33f& m, int i, const cv::Vec3f& v)
{
m(i,0) = v[0];
m(i,1) = v[1];
m(i,2) = v[2];
}
-static bool d_vals_sort(const pair<float,int> a, const pair<float,int> b)
+static bool d_vals_sort(const std::pair<float,int> a, const std::pair<float,int> b)
{
return a.first < b.first;
}
void PointModel::get_d_order(const std::vector<cv::Vec2f>& points, int d_order[], cv::Vec2f d) const
{
- // fit line to orthographically projected points
- vector< pair<float,int> > d_vals;
+ // fit line to orthographically projected points
+ std::vector<std::pair<float,int>> d_vals;
// get sort indices with respect to d scalar product
- for (unsigned i = 0; i<points.size(); ++i)
- d_vals.push_back(pair<float, int>(d.dot(points[i]), i));
+ for (unsigned i = 0; i<points.size(); ++i)
+ d_vals.push_back(std::pair<float, int>(d.dot(points[i]), i));
std::sort(d_vals.begin(),
d_vals.end(),
d_vals_sort
);
- for (unsigned i = 0; i<points.size(); ++i)
- d_order[i] = d_vals[i].second;
+ for (unsigned i = 0; i<points.size(); ++i)
+ d_order[i] = d_vals[i].second;
}
@@ -60,10 +57,10 @@ PointTracker::PointTracker() : init_phase(true)
{
}
-PointTracker::PointOrder PointTracker::find_correspondences_previous(const vector<Vec2f>& points, const PointModel& model, float f)
+PointTracker::PointOrder PointTracker::find_correspondences_previous(const std::vector<cv::Vec2f>& points, const PointModel& model, float f)
{
PointTracker::PointOrder p;
- p.points[0] = project(Vec3f(0,0,0), f);
+ p.points[0] = project(cv::Vec3f(0,0,0), f);
p.points[1] = project(model.M01, f);
p.points[2] = project(model.M02, f);
@@ -79,7 +76,7 @@ PointTracker::PointOrder PointTracker::find_correspondences_previous(const vecto
// find closest point to projected model point i
for (int j=0; j<PointModel::N_POINTS; ++j)
{
- Vec2f d = p.points[i]-points[j];
+ cv::Vec2f d = p.points[i]-points[j];
float sdist = d.dot(d);
if (sdist < min_sdist || j==0)
{
@@ -99,7 +96,7 @@ PointTracker::PointOrder PointTracker::find_correspondences_previous(const vecto
return p;
}
-void PointTracker::track(const vector<Vec2f>& points, const PointModel& model, float f, bool dynamic_pose, int init_phase_timeout)
+void PointTracker::track(const std::vector<cv::Vec2f>& points, const PointModel& model, float f, bool dynamic_pose, int init_phase_timeout)
{
PointOrder order;
@@ -129,9 +126,9 @@ PointTracker::PointOrder PointTracker::find_correspondences(const std::vector<cv
model.get_d_order(points, point_d_order, d);
// calculate d and d_order for simple freetrack-like point correspondence
model.get_d_order(std::vector<cv::Vec2f> {
- Vec2f{0,0},
- Vec2f(model.M01[0], model.M01[1]),
- Vec2f(model.M02[0], model.M02[1])
+ cv::Vec2f{0,0},
+ cv::Vec2f(model.M01[0], model.M01[1]),
+ cv::Vec2f(model.M02[0], model.M02[1])
},
model_d_order,
d);
@@ -151,10 +148,10 @@ int PointTracker::POSIT(const PointModel& model, const PointOrder& order_, float
// The expected rotation used for resolving the ambiguity in POSIT:
// In every iteration step the rotation closer to R_expected is taken
- Matx33f R_expected = Matx33f::eye();
+ cv::Matx33f R_expected = cv::Matx33f::eye();
// initial pose = last (predicted) pose
- Vec3f k;
+ cv::Vec3f k;
get_row(R_expected, 2, k);
float Z0 = 1000.f;
@@ -163,17 +160,17 @@ int PointTracker::POSIT(const PointModel& model, const PointOrder& order_, float
float epsilon_1 = 1;
float epsilon_2 = 1;
- Vec3f I0, J0;
- Vec2f I0_coeff, J0_coeff;
+ cv::Vec3f I0, J0;
+ cv::Vec2f I0_coeff, J0_coeff;
- Vec3f I_1, J_1, I_2, J_2;
- Matx33f R_1, R_2;
- Matx33f* R_current;
+ cv::Vec3f I_1, J_1, I_2, J_2;
+ cv::Matx33f R_1, R_2;
+ cv::Matx33f* R_current;
const int MAX_ITER = 100;
const float EPS_THRESHOLD = 1e-4;
- const cv::Vec2f* order = order_.points;
+ const cv::Vec2f* order = order_.points;
int i=1;
for (; i<MAX_ITER; ++i)
@@ -182,10 +179,10 @@ int PointTracker::POSIT(const PointModel& model, const PointOrder& order_, float
epsilon_2 = k.dot(model.M02)/Z0;
// vector of scalar products <I0, M0i> and <J0, M0i>
- Vec2f I0_M0i(order[1][0]*(1.0 + epsilon_1) - order[0][0],
- order[2][0]*(1.0 + epsilon_2) - order[0][0]);
- Vec2f J0_M0i(order[1][1]*(1.0 + epsilon_1) - order[0][1],
- order[2][1]*(1.0 + epsilon_2) - order[0][1]);
+ cv::Vec2f I0_M0i(order[1][0]*(1.0 + epsilon_1) - order[0][0],
+ order[2][0]*(1.0 + epsilon_2) - order[0][0]);
+ cv::Vec2f J0_M0i(order[1][1]*(1.0 + epsilon_1) - order[0][1],
+ order[2][1]*(1.0 + epsilon_2) - order[0][1]);
// construct projection of I, J onto M0i plane: I0 and J0
I0_coeff = model.P * I0_M0i;
@@ -199,7 +196,7 @@ int PointTracker::POSIT(const PointModel& model, const PointOrder& order_, float
float JJ0 = J0.dot(J0);
float rho, theta;
if (JJ0 == II0) {
- rho = sqrt(abs(2*IJ0));
+ rho = std::sqrt(std::abs(2*IJ0));
theta = -PI/4;
if (IJ0<0) theta *= -1;
}
@@ -217,7 +214,7 @@ int PointTracker::POSIT(const PointModel& model, const PointOrder& order_, float
J_1 = J0 + rho*sin(theta)*model.u;
J_2 = J0 - rho*sin(theta)*model.u;
- float norm_const = 1.0/norm(I_1); // all have the same norm
+ float norm_const = 1.0/cv::norm(I_1); // all have the same norm
// create rotation matrices
I_1 *= norm_const; J_1 *= norm_const;
@@ -236,8 +233,8 @@ int PointTracker::POSIT(const PointModel& model, const PointOrder& order_, float
// pick the rotation solution closer to the expected one
// in simple metric d(A,B) = || I - A * B^T ||
- float R_1_deviation = norm(Matx33f::eye() - R_expected * R_1.t());
- float R_2_deviation = norm(Matx33f::eye() - R_expected * R_2.t());
+ float R_1_deviation = cv::norm(cv::Matx33f::eye() - R_expected * R_1.t());
+ float R_2_deviation = cv::norm(cv::Matx33f::eye() - R_expected * R_2.t());
if (R_1_deviation < R_2_deviation)
R_current = &R_1;
@@ -247,7 +244,7 @@ int PointTracker::POSIT(const PointModel& model, const PointOrder& order_, float
get_row(*R_current, 2, k);
// check for convergence condition
- if (abs(epsilon_1 - old_epsilon_1) + abs(epsilon_2 - old_epsilon_2) < EPS_THRESHOLD)
+ if (std::abs(epsilon_1 - old_epsilon_1) + std::abs(epsilon_2 - old_epsilon_2) < EPS_THRESHOLD)
break;
old_epsilon_1 = epsilon_1;
old_epsilon_2 = epsilon_2;
@@ -259,7 +256,7 @@ int PointTracker::POSIT(const PointModel& model, const PointOrder& order_, float
X_CM.t[1] = order[0][1] * Z0/focal_length;
X_CM.t[2] = Z0;
- //qDebug() << "iter:" << i;
+ //qDebug() << "iter:" << i;
return i;
}
diff --git a/ftnoir_tracker_pt/point_tracker.h b/ftnoir_tracker_pt/point_tracker.h
index 49ff577e..96a448de 100644
--- a/ftnoir_tracker_pt/point_tracker.h
+++ b/ftnoir_tracker_pt/point_tracker.h
@@ -15,7 +15,7 @@
# include <memory>
#endif
#include <vector>
-#include "opentrack/timer.hpp"
+#include "opentrack-compat/timer.hpp"
#include "ftnoir_tracker_pt_settings.h"
#include <QObject>
diff --git a/ftnoir_tracker_pt/pt_video_widget.cpp b/ftnoir_tracker_pt/pt_video_widget.cpp
index 15fc5a86..9f2b90f6 100644
--- a/ftnoir_tracker_pt/pt_video_widget.cpp
+++ b/ftnoir_tracker_pt/pt_video_widget.cpp
@@ -9,9 +9,6 @@
#include "pt_video_widget.h"
-using namespace cv;
-using namespace std;
-
void PTVideoWidget::update_image(const cv::Mat& frame)
{
QMutexLocker foo(&mtx);