From 38f0a455cd15823a225c9d9306a3b3528e336e8e Mon Sep 17 00:00:00 2001 From: Petr Pulc Date: Wed, 12 Sep 2018 11:40:24 +0100 Subject: [PATCH 1/2] Visualize tracking history --- Feature.cpp | 4 ++++ Feature.h | 2 ++ Layer.h | 2 ++ Motion.cpp | 10 ++++++++++ Motion.h | 2 ++ PointLayer.cpp | 11 +++++++++++ PointLayer.h | 2 ++ main.cpp | 1 + 8 files changed, 34 insertions(+) diff --git a/Feature.cpp b/Feature.cpp index cc8781e..cfec29a 100644 --- a/Feature.cpp +++ b/Feature.cpp @@ -1,5 +1,7 @@ #include "Feature.h" +cv::RNG rng(123); + Feature::Feature(float *point, const cv::Mat &description) { // Relative motion in last frame; zero for new point d[0] = 0; @@ -13,6 +15,8 @@ Feature::Feature(float *point, const cv::Mat &description) { desc = description; // Assign default uncertainty u = UNCERTAINTY; + + color = cv::Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)); } void Feature::add_match(const float *point, const cv::Mat &description, const float *upper_estimation) { diff --git a/Feature.h b/Feature.h index 5dbd7cf..d8b916b 100644 --- a/Feature.h +++ b/Feature.h @@ -18,6 +18,8 @@ struct Feature { // ORB descriptor cv::Mat desc; + cv::Scalar color; + Feature(float *point, const cv::Mat &description); void add_match(const float *point, const cv::Mat &description, const float *upper_estimation); diff --git a/Layer.h b/Layer.h index 4b21d43..ae1db55 100644 --- a/Layer.h +++ b/Layer.h @@ -15,6 +15,8 @@ class Layer { // Pair points with estimation from layer above virtual void pair(Layer *layer_above) {}; + + virtual void draw(const cv::Mat &frame) {}; }; #endif //GPU_ORB_TRACKER_LAYER_H diff --git a/Motion.cpp b/Motion.cpp index b2ffd2d..3334214 100644 --- a/Motion.cpp +++ b/Motion.cpp @@ -39,3 +39,13 @@ void Motion::add_frame(const std::vector &points, const cv::Mat &d layers[i - 1]->pair(layers[i]); } } + +void Motion::show(const cv::Mat &frame) { + cv::Mat color; + cv::cvtColor(frame,color,cv::COLOR_GRAY2BGR); + for (unsigned i = OCTAVES; i > 0; i--) { + layers[i]->draw(color); + } + cv::imshow("Hierarchical",color); + cv::waitKey(1); +} diff --git a/Motion.h b/Motion.h index 722ac38..8a716e4 100644 --- a/Motion.h +++ b/Motion.h @@ -16,6 +16,8 @@ class Motion { ~Motion(); void add_frame(const std::vector &points, const cv::Mat &descriptors); + + void show(const cv::Mat &frame); }; #endif //GPU_ORB_TRACKER_MOTION_H diff --git a/PointLayer.cpp b/PointLayer.cpp index c3e4149..c7aa39e 100644 --- a/PointLayer.cpp +++ b/PointLayer.cpp @@ -105,3 +105,14 @@ void PointLayer::estimate(float *in, float *out) { out[1] = 0; } } + +void PointLayer::draw(const cv::Mat &frame) { + for (auto &i:active) { + for (auto h = i.history.begin(), h_end = i.history.end();;) { + auto h_old = h++; + if (h == h_end) { break; } + cv::arrowedLine(frame, h_old->second, h->second, + i.color, 1, cv::LINE_AA, 0, 0.2); + } + } +} \ No newline at end of file diff --git a/PointLayer.h b/PointLayer.h index 047e9b2..0086ca7 100644 --- a/PointLayer.h +++ b/PointLayer.h @@ -47,6 +47,8 @@ class PointLayer : public Layer { void estimate(float *in, float *out) override; void pair(Layer *layer_above) override; + + void draw(const cv::Mat &frame) override; }; #endif //GPU_ORB_TRACKER_POINTLAYER_H diff --git a/main.cpp b/main.cpp index d75d147..50ccd2e 100644 --- a/main.cpp +++ b/main.cpp @@ -50,6 +50,7 @@ int main(int argc, char *argv[]) { gpu_desc.download(cpu_desc); motion.add_frame(points, cpu_desc); + motion.show(cpu_gray); frame_number++; } From 7f6e565ea6c9ad6a799bc1cb4beac0093e411474 Mon Sep 17 00:00:00 2001 From: Petr Pulc Date: Tue, 11 Sep 2018 09:01:58 +0100 Subject: [PATCH 2/2] CPU only processing for demo --- main.cpp | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/main.cpp b/main.cpp index d75d147..6abb7f2 100644 --- a/main.cpp +++ b/main.cpp @@ -16,38 +16,35 @@ int main(int argc, char *argv[]) { const std::string filename(argv[1]); // declare GPU frame storage - cv::cuda::GpuMat gpu_frame, gpu_gray; + cv::Mat cpu_frame, cpu_gray; // declare feature point and description storage std::vector points; - cv::cuda::GpuMat gpu_desc; cv::Mat cpu_desc; - // setup GPU-accelerated ORB detector - cv::Ptr gpu_detector = cv::cuda::ORB::create(std::stoi(argv[2]), 2.0f, OCTAVES, 31, 0, 2, - cv::cuda::ORB::HARRIS_SCORE, 31, 50, true); + // setup ORB detector + cv::Ptr cpu_detector = cv::ORB::create(std::stoi(argv[2]), 2.0f, OCTAVES, 31, 0, 2, + cv::ORB::HARRIS_SCORE, 31, 50); - // open file on GPU - cv::Ptr gpu_reader = cv::cudacodec::createVideoReader(filename); + // open file on CPU + cv::VideoCapture cpu_reader(filename); //introduce motion object Motion motion; // main loop for (;;) { - if (!gpu_reader->nextFrame(gpu_frame)) + if (!cpu_reader.read(cpu_frame)) break; if (frame_width == 0) { - frame_width = gpu_frame.cols; + frame_width = cpu_frame.cols; } //for each frame until end of input: // - convert to gray scale - cv::cuda::cvtColor(gpu_frame, gpu_gray, cv::COLOR_BGRA2GRAY); + cv::cvtColor(cpu_frame, cpu_gray, cv::COLOR_BGRA2GRAY); // - detect feature points and compute their descriptors - gpu_detector->detectAndCompute(gpu_gray, cv::noArray(), points, gpu_desc); - // - download descriptors to CPU RAM - gpu_desc.download(cpu_desc); + cpu_detector->detectAndCompute(cpu_gray, cv::noArray(), points, cpu_desc); motion.add_frame(points, cpu_desc);