From c18cd406a57faa6a75292e2b5f5888f07ab2fc12 Mon Sep 17 00:00:00 2001 From: yitoh Date: Fri, 7 Oct 2016 21:56:19 +0900 Subject: [PATCH] refactored some classes --- main/eye_cameras.cpp | 10 ++-- main/eye_cameras.h | 39 +++++++++++- main/eye_model_updater.cpp | 37 +++++++++++- main/eye_model_updater.h | 21 ++++++- main/eye_util.cpp | 43 -------------- main/eye_util.h | 22 ------- main/main.cpp | 119 ++++++++----------------------------- main/timer.h | 37 ++++++++++++ 8 files changed, 160 insertions(+), 168 deletions(-) delete mode 100644 main/eye_util.cpp delete mode 100644 main/eye_util.h diff --git a/main/eye_cameras.cpp b/main/eye_cameras.cpp index 40eba25..b7c7008 100644 --- a/main/eye_cameras.cpp +++ b/main/eye_cameras.cpp @@ -9,7 +9,7 @@ //#include //#include // Step1: include the header. -namespace eyecamera +namespace eye_tracker { namespace fs = boost::filesystem; @@ -18,7 +18,7 @@ void record_eyecams_mono_interactive(){ // Open and check cameras cv::Mat images[kCcameraNums]; - eyecamera::EyeCamera eyecams[kCcameraNums] = { eyecamera::EyeCamera(0,true)}; + EyeCamera eyecams[kCcameraNums] = { EyeCamera(0,true)}; std::string window_names[kCcameraNums] = { "Cam0" }; for (size_t cam = 0; cam < kCcameraNums; cam++){ // Check if the cameras are opened @@ -99,9 +99,9 @@ void record_eyecams_mono(){ const size_t kCcameraNums = 1; // Open and check cameras - std::vector> eyecams(kCcameraNums); // Image sources + std::vector> eyecams(kCcameraNums); // Image sources cv::Mat images[kCcameraNums]; - eyecams[0] = std::make_unique("Pupil Cam1 ID2"); + eyecams[0] = std::make_unique("Pupil Cam1 ID2"); std::string window_names[kCcameraNums] = { "Cam0" }; for (size_t cam = 0; cam < kCcameraNums; cam++){ // Check if the cameras are opened @@ -187,7 +187,7 @@ void record_eyecams(){ // Open and check cameras cv::Mat images[kCcameraNums]; - eyecamera::EyeCamera eyecams[kCcameraNums] = { eyecamera::EyeCamera(0), eyecamera::EyeCamera(2, true) }; + EyeCamera eyecams[kCcameraNums] = { EyeCamera(0), EyeCamera(2, true) }; std::string window_names[kCcameraNums] = { "Cam0", "Cam1" }; for (size_t cam = 0; cam < kCcameraNums; cam++){ // Check if the cameras are opened diff --git a/main/eye_cameras.h b/main/eye_cameras.h index 8ff54e8..d072032 100644 --- a/main/eye_cameras.h +++ b/main/eye_cameras.h @@ -5,7 +5,7 @@ #include #include "DirectShowFrameGrabber.h" -namespace eyecamera +namespace eye_tracker { void test_eyecam(); void record_eyecams(); @@ -61,5 +61,42 @@ private: EyeCamera& operator=(const EyeCamera& rhs); }; + +/** +* @class CameraUndistorter +* @brief Image undistortion class. This class keeps undistortion map for efficiency +*/ +class CameraUndistorter +{ +public: + CameraUndistorter(const cv::Mat &K, const cv::Vec &distCoeffs) + : K0_(K.clone()), distCoeffs_(distCoeffs) + { + std::cout << "Intrinsic (matrix): " << K0_ << std::endl; + std::cout << "Intrinsic (distortion): " << distCoeffs_ << std::endl; + + } + ~CameraUndistorter() { + } + void init_maps(const cv::Size &s) { + cv::initUndistortRectifyMap(K0_, distCoeffs_, cv::Mat(), K0_, s, CV_32FC1, mapx_, mapy_); + } + void undistort(const cv::Mat &in, cv::Mat &out) { + if (is_dist_map_initialized_ == false) { + init_maps(in.size()); + is_dist_map_initialized_ = true; + } + cv::remap(in, out, mapx_, mapy_, cv::INTER_LINEAR); + } +protected: + // Local variables initialized at the constructor + cv::Mat K0_; + cv::Vec distCoeffs_; // (k1 k2 p1 p2 [k3 [k4 k5 k6]]) + bool is_dist_map_initialized_ = false; + cv::Mat mapx_, mapy_; +private: + // Prevent copying +}; + } // namespace #endif // IRIS_DETECTOR_IR_H \ No newline at end of file diff --git a/main/eye_model_updater.cpp b/main/eye_model_updater.cpp index 0ad7675..047b3b4 100644 --- a/main/eye_model_updater.cpp +++ b/main/eye_model_updater.cpp @@ -1,7 +1,42 @@ - #include "eye_model_updater.h" + namespace eye_tracker{ +// Some utility functions +cv::Point2f toImgCoord(const cv::Point2f& point, const cv::Mat& m, double scale , int shift ) { + return cv::Point2f(static_cast((m.cols / 2 + scale*point.x) * (1 << shift)), + static_cast((m.rows / 2 + scale*point.y) * (1 << shift))); +} +cv::Point toImgCoord(const cv::Point& point, const cv::Mat& m, double scale, int shift ) { + return cv::Point(static_cast((m.cols / 2 + scale*point.x) * (1 << shift)), + static_cast((m.rows / 2 + scale*point.y) * (1 << shift))); +} +cv::RotatedRect toImgCoord(const cv::RotatedRect& rect, const cv::Mat& m, float scale) { + return cv::RotatedRect(toImgCoord(rect.center, m, scale), + cv::Size2f(scale*rect.size.width, + scale*rect.size.height), + rect.angle); +} + +cv::Point2f toImgCoordInv(const cv::Point2f& point, const cv::Mat& m, double scale , int shift ) { + return cv::Point2f( + static_cast((point.x / (1 << shift) - m.cols / 2) / scale), + static_cast((point.y / (1 << shift) - m.rows / 2) / scale) + ); +} +cv::Point toImgCoordInv(const cv::Point& point, const cv::Mat& m, double scale, int shift) { + return cv::Point( + static_cast((point.x / (1 << shift) - m.cols / 2) / scale), + static_cast((point.y / (1 << shift) - m.rows / 2) / scale) + ); +} +cv::RotatedRect toImgCoordInv(const cv::RotatedRect& rect, const cv::Mat& m, float scale) { + return cv::RotatedRect(toImgCoordInv(rect.center, m, scale), + cv::Size2f(rect.size.width/scale, + rect.size.height/scale), + rect.angle); +} + void space_bin_searcher_test() { int w = 640; diff --git a/main/eye_model_updater.h b/main/eye_model_updater.h index c7479b9..76d5007 100644 --- a/main/eye_model_updater.h +++ b/main/eye_model_updater.h @@ -2,7 +2,7 @@ #define EYE_MODEL_UPDATER_H -#define NOMINMAX +//#define NOMINMAX #include #include @@ -12,7 +12,6 @@ #include #include -#include "eye_util.h" //#include #include @@ -24,14 +23,30 @@ #include +//#include "eye_util.h" + + namespace eye_tracker{ + + +// Some utility functions +cv::Point2f toImgCoord(const cv::Point2f& point, const cv::Mat& m, double scale = 1, int shift = 0); +cv::Point toImgCoord(const cv::Point& point, const cv::Mat& m, double scale = 1, int shift = 0); +cv::RotatedRect toImgCoord(const cv::RotatedRect& rect, const cv::Mat& m, float scale = 1); +cv::Point2f toImgCoordInv(const cv::Point2f& point, const cv::Mat& m, double scale = 1, int shift = 0); +cv::Point toImgCoordInv(const cv::Point& point, const cv::Mat& m, double scale = 1, int shift = 0); +cv::RotatedRect toImgCoordInv(const cv::RotatedRect& rect, const cv::Mat& m, float scale = 1); + namespace sef = singleeyefitter; void space_bin_searcher_test(); -// A class helps to pick 2D ellipse samples uniformly over the 2D image space +/** +* @class SpaceBinSearcher +* @brief IA support class to sample 2D ellipse observation uniformly over the 2D image space +*/ class SpaceBinSearcher { public: diff --git a/main/eye_util.cpp b/main/eye_util.cpp deleted file mode 100644 index 05b36c5..0000000 --- a/main/eye_util.cpp +++ /dev/null @@ -1,43 +0,0 @@ - -#include "eye_util.h" - -#include -#include // CUDA - -namespace eye_tracker{ - - -cv::Point2f toImgCoord(const cv::Point2f& point, const cv::Mat& m, double scale , int shift ) { - return cv::Point2f(static_cast((m.cols / 2 + scale*point.x) * (1 << shift)), - static_cast((m.rows / 2 + scale*point.y) * (1 << shift))); -} -cv::Point toImgCoord(const cv::Point& point, const cv::Mat& m, double scale, int shift ) { - return cv::Point(static_cast((m.cols / 2 + scale*point.x) * (1 << shift)), - static_cast((m.rows / 2 + scale*point.y) * (1 << shift))); -} -cv::RotatedRect toImgCoord(const cv::RotatedRect& rect, const cv::Mat& m, float scale) { - return cv::RotatedRect(toImgCoord(rect.center, m, scale), - cv::Size2f(scale*rect.size.width, - scale*rect.size.height), - rect.angle); -} - -cv::Point2f toImgCoordInv(const cv::Point2f& point, const cv::Mat& m, double scale , int shift ) { - return cv::Point2f( - static_cast((point.x / (1 << shift) - m.cols / 2) / scale), - static_cast((point.y / (1 << shift) - m.rows / 2) / scale) - ); -} -cv::Point toImgCoordInv(const cv::Point& point, const cv::Mat& m, double scale, int shift) { - return cv::Point( - static_cast((point.x / (1 << shift) - m.cols / 2) / scale), - static_cast((point.y / (1 << shift) - m.rows / 2) / scale) - ); -} -cv::RotatedRect toImgCoordInv(const cv::RotatedRect& rect, const cv::Mat& m, float scale) { - return cv::RotatedRect(toImgCoordInv(rect.center, m, scale), - cv::Size2f(rect.size.width/scale, - rect.size.height/scale), - rect.angle); -} -} \ No newline at end of file diff --git a/main/eye_util.h b/main/eye_util.h deleted file mode 100644 index b6432a9..0000000 --- a/main/eye_util.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef EYE_UTIL_H -#define EYE_UTIL_H - -#include -#include - -namespace eye_tracker{ - - - typedef std::complex EigenVector3[3]; - - cv::Point2f toImgCoord(const cv::Point2f& point, const cv::Mat& m, double scale = 1, int shift = 0); - cv::Point toImgCoord(const cv::Point& point, const cv::Mat& m, double scale = 1, int shift = 0); - cv::RotatedRect toImgCoord(const cv::RotatedRect& rect, const cv::Mat& m, float scale = 1); - cv::Point2f toImgCoordInv(const cv::Point2f& point, const cv::Mat& m, double scale = 1, int shift = 0); - cv::Point toImgCoordInv(const cv::Point& point, const cv::Mat& m, double scale = 1, int shift = 0); - cv::RotatedRect toImgCoordInv(const cv::RotatedRect& rect, const cv::Mat& m, float scale = 1); - - - -} -#endif // EYE_UTIL_H \ No newline at end of file diff --git a/main/main.cpp b/main/main.cpp index f8c619c..eb9b004 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -26,7 +26,6 @@ #include #include -#define NOMINMAX #include "pupilFitter.h" // 2D pupil detector @@ -37,73 +36,7 @@ -namespace eye_tracker { - -class FrameRateCounter -{ -public: - FrameRateCounter() - :frame_count_(0), kSkipFrameCount(50), fps_(0.0) { - timer_.pause(); - } - ~FrameRateCounter(){} - double fps(){ return fps_; } - size_t frame_count() { return frame_count_; } - void count(){ - if (frame_count_++ == kSkipFrameCount){ - timer_.resume();/// Wait measuring time until the process gets stabilized - } - fps_ = (frame_count_ - kSkipFrameCount) / timer_.elapsed(); - return; - } -protected: - // Local variables initialized at the constructor - double time_total; - size_t frame_count_; - double fps_; - - // Local variables - timer timer_; - const size_t kSkipFrameCount; - -private: - // Prevent copying - FrameRateCounter(const FrameRateCounter& other); - FrameRateCounter& operator=(const FrameRateCounter& rhs); -}; - - -class CameraUndistorter -{ -public: - CameraUndistorter(const cv::Mat &K, const cv::Vec &distCoeffs) - : K0_(K.clone()), distCoeffs_(distCoeffs) - { - std::cout << "Intrinsic (matrix): " << K0_ << std::endl; - std::cout << "Intrinsic (distortion): " << distCoeffs_ << std::endl; - - } - ~CameraUndistorter() { - } - void init_maps(const cv::Size &s) { - cv::initUndistortRectifyMap(K0_, distCoeffs_, cv::Mat(), K0_, s, CV_32FC1, mapx_, mapy_); - } - void undistort(const cv::Mat &in, cv::Mat &out) { - if (is_dist_map_initialized_ == false) { - init_maps(in.size()); - is_dist_map_initialized_ = true; - } - cv::remap(in, out, mapx_, mapy_, cv::INTER_LINEAR); - } -protected: - // Local variables initialized at the constructor - cv::Mat K0_; - cv::Vec distCoeffs_; // (k1 k2 p1 p2 [k3 [k4 k5 k6]]) - bool is_dist_map_initialized_ = false; - cv::Mat mapx_, mapy_; -private: - // Prevent copying -}; +namespace { enum InputMode { CAMERA, CAMERA_MONO, VIDEO, IMAGE }; @@ -118,10 +51,10 @@ int main(int argc, char *argv[]){ bool kVisualization = false; kVisualization = true; - eye_tracker::InputMode input_mode = - eye_tracker::InputMode::VIDEO; // Set a video as a video source - // InputMode::CAMERA; // Set cameras as video sources - // InputMode::CAMERA_MONO; // Set cameras as video sources + InputMode input_mode = + //InputMode::VIDEO; // Set a video as a video source + // InputMode::CAMERA; // Set two cameras as video sources + InputMode::CAMERA_MONO; // Set a camera as video sources // InputMode::IMAGE;// Set an image as a video source @@ -142,20 +75,20 @@ int main(int argc, char *argv[]){ if (media_file_ext == ".avi" || media_file_ext == ".mp4" || media_file_ext == ".wmv") { - input_mode = eye_tracker::InputMode::VIDEO; + input_mode = InputMode::VIDEO; }else{ - input_mode = eye_tracker::InputMode::IMAGE; + input_mode = InputMode::IMAGE; } } else { - if (input_mode == eye_tracker::InputMode::IMAGE || input_mode == eye_tracker::InputMode::VIDEO) { + if (input_mode == InputMode::IMAGE || input_mode == InputMode::VIDEO) { switch (input_mode) { - case eye_tracker::InputMode::IMAGE: + case InputMode::IMAGE: media_file = kDir + "data3/test.png"; media_file_stem = "test"; break; - case eye_tracker::InputMode::VIDEO: + case InputMode::VIDEO: media_file = kDir + "out/test.avi"; media_file_stem = "test"; break; @@ -186,12 +119,12 @@ int main(int argc, char *argv[]){ size_t kCameraNums; switch (input_mode) { - case eye_tracker::InputMode::IMAGE: - case eye_tracker::InputMode::VIDEO: - case eye_tracker::InputMode::CAMERA_MONO: + case InputMode::IMAGE: + case InputMode::VIDEO: + case InputMode::CAMERA_MONO: kCameraNums = 1; break; - case eye_tracker::InputMode::CAMERA: + case InputMode::CAMERA: kCameraNums = 2; break; default: @@ -201,7 +134,7 @@ int main(int argc, char *argv[]){ // Setup of classes that handle monocular/stereo camera setups // We can encapslate them into a wrapper class in future update - std::vector> eyecams(kCameraNums); // Image sources + std::vector> eyecams(kCameraNums); // Image sources std::vector> camera_undistorters(kCameraNums); // Camera undistorters std::vector window_names(kCameraNums); // Window names std::vector images(kCameraNums); // buffer images @@ -213,31 +146,31 @@ int main(int argc, char *argv[]){ try{ switch (input_mode) { - case eye_tracker::InputMode::IMAGE: - eyecams[0] = std::make_unique(media_file, false); + case InputMode::IMAGE: + eyecams[0] = std::make_unique(media_file, false); eye_model_updaters[0] = std::make_unique(focal_length, 5, 0.5); camera_undistorters[0] = std::make_unique(K, distCoeffs); window_names = { "Video/Image" }; file_stems = { media_file_stem }; break; - case eye_tracker::InputMode::VIDEO: - eyecams[0] = std::make_unique(media_file, false); + case InputMode::VIDEO: + eyecams[0] = std::make_unique(media_file, false); eye_model_updaters[0] = std::make_unique(focal_length, 5, 0.5); camera_undistorters[0] = std::make_unique(K, distCoeffs); window_names = { "Video/Image" }; file_stems = { media_file_stem }; break; - case eye_tracker::InputMode::CAMERA: + case InputMode::CAMERA: camera_indices[0] = 0; camera_indices[1] = 2; #if 0 // OpenCV HighGUI frame grabber - eyecams[0] = std::make_unique(camera_indices[0], false); - eyecams[1] = std::make_unique(camera_indices[1], false); + eyecams[0] = std::make_unique(camera_indices[0], false); + eyecams[1] = std::make_unique(camera_indices[1], false); #else // DirectShow frame grabber - eyecams[0] = std::make_unique("Pupil Cam1 ID0"); - eyecams[1] = std::make_unique("Pupil Cam1 ID2"); + eyecams[0] = std::make_unique("Pupil Cam1 ID0"); + eyecams[1] = std::make_unique("Pupil Cam1 ID2"); #endif eye_model_updaters[0] = std::make_unique(focal_length, 5, 0.5); eye_model_updaters[1] = std::make_unique(focal_length, 5, 0.5); @@ -246,8 +179,8 @@ int main(int argc, char *argv[]){ window_names = { "Cam0", "Cam1" }; file_stems = { "cam0", "cam1" }; break; - case eye_tracker::InputMode::CAMERA_MONO: - eyecams[0] = std::make_unique("Pupil Cam1 ID0"); // + case InputMode::CAMERA_MONO: + eyecams[0] = std::make_unique("Pupil Cam1 ID0"); // eye_model_updaters[0] = std::make_unique(focal_length, 5, 0.5); camera_undistorters[0] = std::make_unique(K, distCoeffs); window_names = { "Cam0" }; diff --git a/main/timer.h b/main/timer.h index 17c6bbd..7491f53 100644 --- a/main/timer.h +++ b/main/timer.h @@ -3,6 +3,8 @@ #include "high_resolution_timer.hpp" + +namespace eye_tracker { class timer { @@ -63,4 +65,39 @@ protected: #define PAUSE_TIMER(t) if(timer::pause_guard const& _timer_pause_guard_(t)) {} else +class FrameRateCounter +{ +public: + FrameRateCounter() + :frame_count_(0), kSkipFrameCount(50), fps_(0.0) { + timer_.pause(); + } + ~FrameRateCounter(){} + double fps(){ return fps_; } + size_t frame_count() { return frame_count_; } + void count(){ + if (frame_count_++ == kSkipFrameCount){ + timer_.resume();/// Wait measuring time until the process gets stabilized + } + fps_ = (frame_count_ - kSkipFrameCount) / timer_.elapsed(); + return; + } +protected: + // Local variables initialized at the constructor + double time_total; + size_t frame_count_; + double fps_; + + // Local variables + timer timer_; + const size_t kSkipFrameCount; + +private: + // Prevent copying + FrameRateCounter(const FrameRateCounter& other); + FrameRateCounter& operator=(const FrameRateCounter& rhs); +}; + +} + #endif // __PUPIL_TRACKER_TIMER_H__