diff --git a/src/examples/capture.cpp b/src/examples/capture.cpp index a546369135bbe89d0556a451b6ea5df04bfbef96..19a31ad7742c0d34c7b0dc23da5d9a54c997e239 100644 --- a/src/examples/capture.cpp +++ b/src/examples/capture.cpp @@ -6,24 +6,25 @@ int main(int argc, char *argv[]) { - // Camera object definition + // Open camera cv::VideoCapture cam; CCamUtils cam_fc; - - // Open camera cam_fc.openCamera(0, cam); - for (int nframe = 0; nframe < 1000; ++nframe) + cv::startWindowThread(); + cv::namedWindow("Cam Test", 1); + + for (;;) { // Get frame cv::Mat frame; cam_fc.getFrame(cam, frame); // Show frame - cv::namedWindow("Cam Test"); - cam_fc.showFrame("Cam Test", frame); - - if (cv::waitKey(30) >= 0) - break; + cv::imshow("Cam Test", frame); + cv::waitKey(1); + //if (cv::waitKey(30) >= 0) break; } + + cv::destroyAllWindows(); } diff --git a/src/examples/feature_detection.cpp b/src/examples/feature_detection.cpp index 72925c34c0045089711bc69a952f8ff3bb658e8a..63278867d21348c3ef8a020c5507d8297f72068b 100644 --- a/src/examples/feature_detection.cpp +++ b/src/examples/feature_detection.cpp @@ -6,13 +6,46 @@ int main(int argc, char *argv[]) { - // Camera object definition - cv::VideoCapture cam; - CCamUtils cam_fc; + std::cout << "----------------------------------------" << std::endl; + std::cout << "| Feature detector example |" << std::endl; + std::cout << "----------------------------------------" << std::endl; + std::cout << std::endl; + + std::vector<std::string> options; + options.resize(11); + options[0] = "ORB"; + options[1] = "SIFT"; + options[2] = "SURF"; + options[3] = "AKAZE"; + options[4] = "KAZE"; + options[5] = "BRISK"; + options[6] = "MSER"; + options[7] = "FAST"; + options[8] = "LSD"; + options[9] = "ED"; + options[10] = "AGAST"; + + for (unsigned int ii=0; ii<options.size(); ii++) + std::cout << "[" << ii << "]: " << options[ii] << std::endl; + + std::cout << std::endl << "Which feature do you want to test?: "; + int feat_type; + std::cin >> feat_type; + std::cout << std::endl << "Testing: " << options[feat_type] << std::endl; // Open camera + cv::VideoCapture cam; + CCamUtils cam_fc; cam_fc.openCamera(0, cam); + // Create displays + cv::startWindowThread(); + cv::namedWindow("Original image", cv::WINDOW_NORMAL); + cv::namedWindow("Detections", cv::WINDOW_NORMAL); + + // The following line is used to remove the OpenCV "init done" from the terminal + std::cout << "\e[A" << " " << std::endl; + for (int nframe = 0; nframe < 1000; ++nframe) { // Get frame @@ -20,22 +53,33 @@ int main(int argc, char *argv[]) cam_fc.getFrame(cam, frame); // Show ORIGINAL frame - cv::namedWindow("Original image"); - cam_fc.showFrame("Original image", frame); + cv::imshow("Original image", frame); // Detect features (keypoints) - CFeature_Detector detector(ORB); - CFeature_Detector::KeyPointVector keypoints; - keypoints = detector.detectKeyPoints(frame); + CFeature_Detector detector(feat_type); - // Show frame with features - detector.drawKeyFeatures(frame, keypoints); + if (feat_type!=8 && feat_type!=9) + { + CFeature_Detector::KeyPointVector keypoints; + keypoints = detector.detectKeyPoints(frame); + // Show frame with features + detector.drawKeyFeatures(frame, keypoints); + } + else + { + CFeature_Detector::KeyLineVector keypoints; + keypoints = detector.detectKeyLines(frame); + // Show frame with features + detector.drawKeyFeatures(frame, keypoints); + } - // Show NEW frame - cv::namedWindow("Detections"); - cam_fc.showFrame("Detections", frame); + std::cout << "\e[A" << "Detection time: " << detector.getDetectionTime() << std::endl; - if (cv::waitKey(30) >= 0) - break; + // Show NEW frame + cv::imshow("Detections", frame); + cv::waitKey(1); + // if (cv::waitKey(30) >= 0) break; } + + cv::destroyAllWindows(); } diff --git a/src/feature_detector/feature_detector.cpp b/src/feature_detector/feature_detector.cpp index 760b488d4d7fb471861d621f459878b9cba4259c..baf805b4c9a518a700e4b011df8aa213d0bb850c 100644 --- a/src/feature_detector/feature_detector.cpp +++ b/src/feature_detector/feature_detector.cpp @@ -1,9 +1,9 @@ #include "feature_detector.h" -CFeature_Detector::CFeature_Detector(DETECTOR_TYPE _type) : +CFeature_Detector::CFeature_Detector(const DETECTOR_TYPE& _type) : is_init_(false), is_line_(false), is_limited_(false), keypoints_limit_(-1), detect_time_(0.0) { - init(_type); + is_init_ = init(_type); if (!is_init_) { @@ -12,6 +12,19 @@ CFeature_Detector::CFeature_Detector(DETECTOR_TYPE _type) : } } +CFeature_Detector::CFeature_Detector(const int&_type) : + is_init_(false), is_line_(false), is_limited_(false), keypoints_limit_(-1), detect_time_(0.0) +{ + is_init_ = init( intToDetectorType(_type) ); + + if (!is_init_) + { + std::cerr << "[Feature Detector]: Something went wrong during initialization! Feature Detector not initialized." + << std::endl; + } +} + + CFeature_Detector::~CFeature_Detector() { } @@ -82,7 +95,9 @@ CFeature_Detector::KeyPointVector CFeature_Detector::detectKeyPoints(const cv::M { KeyPointVector kpts; + clock_t tStart = clock(); feature_detector_->detect(_image, kpts); + detect_time_ = (double)(clock() - tStart)/CLOCKS_PER_SEC; if (this->isLimited()) { @@ -98,10 +113,12 @@ CFeature_Detector::KeyLineVector CFeature_Detector::detectKeyLines(const cv::Mat KeyLineVector kls; KeyLineVector kls2; + clock_t tStart = clock(); if (type_ == LSD) lsd_detector_->detect(_image, kls, SCALE_FACTOR_LINE_DETECTOR, NUM_OCTAVE_LINE_DETECTOR); else ed_detector_->detect(_image, kls2); + detect_time_ = (double)(clock() - tStart)/CLOCKS_PER_SEC; kls.insert(kls.end(), kls2.begin(), kls2.end()); return kls; diff --git a/src/feature_detector/feature_detector.h b/src/feature_detector/feature_detector.h index ec8847708f9afa1f433c79e97b7633bb91492daf..761668311735e0a541fb4639c4e9f7ab916243a0 100644 --- a/src/feature_detector/feature_detector.h +++ b/src/feature_detector/feature_detector.h @@ -1,6 +1,8 @@ #ifndef _FEATURE_DETECTOR_H #define _FEATURE_DETECTOR_H +#include <time.h> + // OpenCV stuff #include <opencv2/core/core.hpp> #include <opencv2/core/types.hpp> @@ -20,7 +22,10 @@ typedef cv::Ptr<cv::line_descriptor::BinaryDescriptor> EDDetector; /** * brief Descriptor types */ -enum DETECTOR_TYPE { ORB, SIFT, SURF, AKAZE, KAZE, BRISK, MSER, FAST, LSD, ED, AGAST }; +enum DETECTOR_TYPE +{ + ORB, SIFT, SURF, AKAZE, KAZE, BRISK, MSER, FAST, LSD, ED, AGAST +}; /** * \brief Stream operator for the detector flags. @@ -68,6 +73,42 @@ inline std::ostream& operator<<(std::ostream& _os, DETECTOR_TYPE _f) return _os; } +/** + * \brief Convert an integer to its corresponding descriptor flag. + * + * By default return L2. + */ +inline DETECTOR_TYPE intToDetectorType(const unsigned int _i) +{ + switch (_i) + { + case 0: + return ORB; + case 1: + return SIFT; + case 2: + return SURF; + case 3: + return AKAZE; + case 4: + return KAZE; + case 5: + return BRISK; + case 6: + return MSER; + case 7: + return FAST; + case 8: + return LSD; + case 9: + return ED; + case 10: + return AGAST; + default: + return ORB; + } +} + class CFeature_Detector { public: @@ -75,21 +116,38 @@ class CFeature_Detector typedef std::vector<cv::KeyPoint> KeyPointVector; typedef std::vector<cv::line_descriptor::KeyLine> KeyLineVector; - bool getDetectionTime() { return detect_time_; }; + CFeature_Detector(const DETECTOR_TYPE& _type); + CFeature_Detector(const int& _type); + ~CFeature_Detector(); - bool isLine() { return is_line_; }; - bool isLimited() { return is_limited_; }; - int getKeyPointsLimit() { return keypoints_limit_; }; + double getDetectionTime() + { + return detect_time_; + } + ; + + bool isLine() + { + return is_line_; + } + ; + bool isLimited() + { + return is_limited_; + } + ; + int getKeyPointsLimit() + { + return keypoints_limit_; + } + ; bool limitKeypts(unsigned int _nFeatures); - CFeature_Detector(DETECTOR_TYPE _type); - ~CFeature_Detector(); - KeyPointVector detectKeyPoints(const cv::Mat&); KeyLineVector detectKeyLines(const cv::Mat&); - cv::Mat drawKeyFeatures( const cv::Mat& _image, const std::vector<cv::KeyPoint>& _kp_vec ); - cv::Mat drawKeyFeatures( const cv::Mat& _image, const std::vector<cv::line_descriptor::KeyLine>& _kl_vec ); + cv::Mat drawKeyFeatures(const cv::Mat& _image, const std::vector<cv::KeyPoint>& _kp_vec); + cv::Mat drawKeyFeatures(const cv::Mat& _image, const std::vector<cv::line_descriptor::KeyLine>& _kl_vec); private: