diff options
Diffstat (limited to 'ftnoir_tracker_pt')
-rw-r--r-- | ftnoir_tracker_pt/camera.cpp | 10 | ||||
-rw-r--r-- | ftnoir_tracker_pt/camera.h | 4 | ||||
-rw-r--r-- | ftnoir_tracker_pt/ftnoir_tracker_pt.cpp | 50 | ||||
-rw-r--r-- | ftnoir_tracker_pt/ftnoir_tracker_pt.h | 1 | ||||
-rw-r--r-- | ftnoir_tracker_pt/point_extractor.cpp | 3 | ||||
-rw-r--r-- | ftnoir_tracker_pt/pt_video_widget.cpp | 3 | ||||
-rw-r--r-- | ftnoir_tracker_pt/pt_video_widget.h | 11 |
7 files changed, 58 insertions, 24 deletions
diff --git a/ftnoir_tracker_pt/camera.cpp b/ftnoir_tracker_pt/camera.cpp index 9168a3e4..2e745f2a 100644 --- a/ftnoir_tracker_pt/camera.cpp +++ b/ftnoir_tracker_pt/camera.cpp @@ -61,6 +61,16 @@ void Camera::set_res(int x_res, int y_res) } } +CamInfo Camera::get_info() +{ + if (cam_info.res_x == 0 || cam_info.res_y == 0) + { + cv::Mat tmp; + _get_frame(&tmp); + } + return cam_info; +} + bool Camera::get_frame(float dt, cv::Mat* frame) { bool new_frame = _get_frame(frame); diff --git a/ftnoir_tracker_pt/camera.h b/ftnoir_tracker_pt/camera.h index 63614ded..2c42652a 100644 --- a/ftnoir_tracker_pt/camera.h +++ b/ftnoir_tracker_pt/camera.h @@ -54,8 +54,8 @@ public: bool get_frame(float dt, cv::Mat* frame); // WARNING: returned references are valid as long as object - const CamInfo& get_info() const { return cam_info; } - const CamInfo& get_desired() const { return cam_desired; } + CamInfo get_info(); + CamInfo get_desired() const { return cam_desired; } protected: // get a frame from the camera diff --git a/ftnoir_tracker_pt/ftnoir_tracker_pt.cpp b/ftnoir_tracker_pt/ftnoir_tracker_pt.cpp index 474123d0..716fddfd 100644 --- a/ftnoir_tracker_pt/ftnoir_tracker_pt.cpp +++ b/ftnoir_tracker_pt/ftnoir_tracker_pt.cpp @@ -12,6 +12,7 @@ #include <QFile> #include <QCoreApplication> #include "opentrack/camera-names.hpp" +#include "opentrack/opencv-calibration.hpp" using namespace std; using namespace cv; @@ -67,11 +68,14 @@ float Tracker_PT::get_focal_length() } const float diag_fov = static_cast<int>(fov_) * pi / 180.f; + QMutexLocker l(&camera_mtx); + if (!intrinsics.empty()) + return intrinsics.at<float>(0, 0); CamInfo info = camera.get_info(); const int w = info.res_x, h = info.res_y; const double diag = sqrt(w * w + h * h)/w; const double fov = 2.*atan(tan(diag_fov/2.0)/sqrt(1. + diag*diag)); - return .5 / tan(.5 * fov); + return w*.5 / tan(.5 * fov); } void Tracker_PT::run() @@ -105,7 +109,18 @@ void Tracker_PT::run() ever_success |= success; if (success) - point_tracker.track(points, PointModel(s), get_focal_length(), s.dynamic_pose, s.init_phase_timeout); + { + if (!intrinsics.empty()) + { + std::vector<cv::Vec2f> points_; + cv::undistortPoints(points, points_, intrinsics, dist_coeffs); + point_tracker.track(points_, PointModel(s), get_focal_length(), s.dynamic_pose, s.init_phase_timeout); + } + else + { + point_tracker.track(points, PointModel(s), get_focal_length(), s.dynamic_pose, s.init_phase_timeout); + } + } { Affine X_CM = pose(); @@ -114,25 +129,23 @@ void Tracker_PT::run() cv::Vec3f p = X_GH.t; // head (center?) position in global space float fx = get_focal_length(); cv::Vec2f p_(p[0] / p[2] * fx, p[1] / p[2] * fx); // projected to screen - points.push_back(p_); } for (unsigned i = 0; i < points.size(); i++) { auto& p = points[i]; - auto p2 = cv::Point(p[0] * frame.cols + frame.cols/2, -p[1] * frame.cols + frame.rows/2); cv::Scalar color(0, 255, 0); if (i == points.size()-1) color = cv::Scalar(0, 0, 255); cv::line(frame, - cv::Point(p2.x - 20, p2.y), - cv::Point(p2.x + 20, p2.y), + cv::Point(p[0] - 20, p[1]), + cv::Point(p[0] + 20, p[1]), color, 4); cv::line(frame, - cv::Point(p2.x, p2.y - 20), - cv::Point(p2.x, p2.y + 20), + cv::Point(p[0], p[1] - 20), + cv::Point(p[0], p[1] + 20), color, 4); } @@ -152,7 +165,6 @@ void Tracker_PT::apply_settings() { qDebug()<<"Tracker:: Applying settings"; QMutexLocker l(&camera_mtx); - QMutexLocker lock(&mutex); camera.set_device_index(camera_name_to_index("PS3Eye Camera")); int res_x, res_y, cam_fps; switch (s.camera_mode) @@ -182,6 +194,26 @@ void Tracker_PT::apply_settings() camera.set_res(res_x, res_y); camera.set_fps(cam_fps); + cv::Mat intrinsics_ = cv::Mat::eye(3, 3, CV_32FC1); + cv::Mat dist_coeffs_ = cv::Mat::zeros(5, 1, CV_32FC1); + intrinsics = cv::Mat(); + dist_coeffs = cv::Mat(); + int fov; + switch (s.fov) + { + default: + case 0: fov = 56; break; + case 1: fov = 75; break; + } + if (get_camera_calibration("PS3Eye Camera", intrinsics_, dist_coeffs_, res_x, res_y, fov)) + { + intrinsics = intrinsics_.clone(); + dist_coeffs = dist_coeffs_.clone(); + qDebug() << "calibrated"; + } + else + qDebug() << "not calibrated!"; + qDebug()<<"Tracker::apply ends"; } diff --git a/ftnoir_tracker_pt/ftnoir_tracker_pt.h b/ftnoir_tracker_pt/ftnoir_tracker_pt.h index 21da9a2c..aeda7dd7 100644 --- a/ftnoir_tracker_pt/ftnoir_tracker_pt.h +++ b/ftnoir_tracker_pt/ftnoir_tracker_pt.h @@ -78,6 +78,7 @@ private: Timer time; volatile bool ever_success; + cv::Mat intrinsics, dist_coeffs; static constexpr double rad2deg = 180.0/3.14159265; static constexpr double deg2rad = 3.14159265/180.0; diff --git a/ftnoir_tracker_pt/point_extractor.cpp b/ftnoir_tracker_pt/point_extractor.cpp index e81e3aa0..cc9dbce1 100644 --- a/ftnoir_tracker_pt/point_extractor.cpp +++ b/ftnoir_tracker_pt/point_extractor.cpp @@ -211,8 +211,7 @@ 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); - points.push_back(p); + points.push_back(pos); } vector<Mat> channels_; diff --git a/ftnoir_tracker_pt/pt_video_widget.cpp b/ftnoir_tracker_pt/pt_video_widget.cpp index 12f01413..15fc5a86 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" -#include <QDebug> -#include <QHBoxLayout> - using namespace cv; using namespace std; diff --git a/ftnoir_tracker_pt/pt_video_widget.h b/ftnoir_tracker_pt/pt_video_widget.h index 09818254..af1d60fd 100644 --- a/ftnoir_tracker_pt/pt_video_widget.h +++ b/ftnoir_tracker_pt/pt_video_widget.h @@ -1,4 +1,5 @@ /* Copyright (c) 2012 Patrick Ruoff + * Copyright (c) 2014 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 @@ -8,15 +9,9 @@ #pragma once #include <QObject> -#include <QTime> -#include <QDialog> +#include <QWidget> #include <opencv2/core/core.hpp> -#ifndef OPENTRACK_API -# include <QGLWidget> -# include <boost/shared_ptr.hpp> -#else -# include <memory> -#endif +#include <memory> #include <QPainter> #include <QPaintEvent> #include <QTimer> |