From e60aee2a43b7d99e1a7363425c19533024096e77 Mon Sep 17 00:00:00 2001 From: Tristan Walter <twalter@orn.mpg.de> Date: Tue, 27 Oct 2020 22:25:44 +0100 Subject: [PATCH] grab::averaging_method_t::Class --- .../src/commons/common/video/GenericVideo.cpp | 7 ++--- Application/src/grabber/default_config.cpp | 4 ++- Application/src/grabber/default_config.h | 3 +++ Application/src/grabber/grabber.cpp | 8 +++--- Application/src/tracker/VideoOpener.cpp | 26 ++++++++++++++----- Application/src/tracker/VideoOpener.h | 4 +-- 6 files changed, 36 insertions(+), 16 deletions(-) diff --git a/Application/src/commons/common/video/GenericVideo.cpp b/Application/src/commons/common/video/GenericVideo.cpp index fbfcc0c..c3078ad 100644 --- a/Application/src/commons/common/video/GenericVideo.cpp +++ b/Application/src/commons/common/video/GenericVideo.cpp @@ -1,6 +1,7 @@ #include "GenericVideo.h" #include <misc/Timer.h> #include <misc/GlobalSettings.h> +#include <grabber/default_config.h> using namespace cmn; @@ -111,8 +112,8 @@ void GenericVideo::generate_average(cv::Mat &av, uint64_t frameIndex) { gpuMat f, ref; std::vector<gpuMat> vec; - std::string averaging_method = GlobalSettings::has("averaging_method") ? utils::lowercase(SETTING(averaging_method).value<std::string>()) : "mean"; - bool use_mean = averaging_method != "max" && averaging_method != "min"; + auto averaging_method = GlobalSettings::has("averaging_method") ? SETTING(averaging_method).value<grab::averaging_method_t::Class>() : grab::averaging_method_t::mean; + bool use_mean = averaging_method != grab::averaging_method_t::max && averaging_method != grab::averaging_method_t::min; Debug("Use max: %d", !use_mean); if(average.empty() || average.cols != size().width || average.rows != size().height) { @@ -146,7 +147,7 @@ void GenericVideo::generate_average(cv::Mat &av, uint64_t frameIndex) { } else { ref.copyTo(local); - if(averaging_method == "max") + if(averaging_method == grab::averaging_method_t::max) av = cv::max(av, local); else av = cv::min(av, local); diff --git a/Application/src/grabber/default_config.cpp b/Application/src/grabber/default_config.cpp index 0a2afc6..a52e473 100644 --- a/Application/src/grabber/default_config.cpp +++ b/Application/src/grabber/default_config.cpp @@ -12,6 +12,8 @@ namespace grab { + ENUM_CLASS_DOCS(averaging_method_t, "Mean", "Mode", "Min", "Max"); + #ifndef WIN32 struct passwd *pw = getpwuid(getuid()); const char *homedir = pw->pw_dir; @@ -118,7 +120,7 @@ namespace default_config { CONFIG("correct_luminance", false, "Attempts to correct for badly lit backgrounds by evening out luminance across the background.", STARTUP); CONFIG("equalize_histogram", false, "Equalizes the histogram of the image before thresholding and background subtraction."); CONFIG("quit_after_average", false, "If set to true, this will terminate the program directly after generating (or loading) a background average image.", STARTUP); - CONFIG("averaging_method", std::string("mean"), "This can be either 'mean', 'mode', 'min' or 'max'. All accumulated background images (to be used for generating an average background) will be combined using the max or mean function."); + CONFIG("averaging_method", averaging_method_t::mean, "Determines the way in which the background samples are combined. The background generated in the process will be used to subtract background from foreground objects during conversion."); CONFIG("average_samples", int(100), "Number of samples taken to generate an average image. Usually has to be less if `average_method` is set to max."); CONFIG("reset_average", false, "If set to true, the average will be regenerated using the live stream of images (video or camera)."); diff --git a/Application/src/grabber/default_config.h b/Application/src/grabber/default_config.h index ac98d9a..767f4b7 100644 --- a/Application/src/grabber/default_config.h +++ b/Application/src/grabber/default_config.h @@ -4,6 +4,9 @@ #include <misc/GlobalSettings.h> namespace grab { + ENUM_CLASS(averaging_method_t, mean, mode, max, min); + ENUM_CLASS_HAS_DOCS(averaging_method_t); + namespace default_config { using namespace cmn; diff --git a/Application/src/grabber/grabber.cpp b/Application/src/grabber/grabber.cpp index 318570a..b186cf5 100644 --- a/Application/src/grabber/grabber.cpp +++ b/Application/src/grabber/grabber.cpp @@ -791,8 +791,8 @@ bool FrameGrabber::add_image_to_average(const Image_t& current) { _current_image = nullptr; } - static std::string averaging_method = GlobalSettings::has("averaging_method") ? utils::lowercase(SETTING(averaging_method).value<std::string>()) : "mean"; - static bool use_mean = averaging_method != "max" && averaging_method != "min"; + static auto averaging_method = GlobalSettings::has("averaging_method") ? utils::lowercase(SETTING(averaging_method).value<grab::averaging_method_t::Class>()) : grab::averaging_method_t::mean; + static bool use_mean = averaging_method != grab::averaging_method_t::max && averaging_method != grab::averaging_method_t::max; static gpuMat empty_image; if(empty_image.empty()) empty_image = gpuMat::zeros(_cropped_size.height, _cropped_size.width, CV_8UC1); @@ -815,9 +815,9 @@ bool FrameGrabber::add_image_to_average(const Image_t& current) { _current_average.copyTo(local_av); empty_image.copyTo(local); - if(averaging_method == "max") + if(averaging_method == grab::averaging_method_t::max) _current_average = cv::max(local, local_av); - else if(averaging_method == "min") + else if(averaging_method == grab::averaging_method_t::min) _current_average = cv::min(local, local_av); } } diff --git a/Application/src/tracker/VideoOpener.cpp b/Application/src/tracker/VideoOpener.cpp index 807031e..0a03eaa 100644 --- a/Application/src/tracker/VideoOpener.cpp +++ b/Application/src/tracker/VideoOpener.cpp @@ -37,6 +37,7 @@ VideoOpener::VideoOpener() { _raw_info->set_policy(gui::VerticalLayout::LEFT); _screenshot = std::make_shared<gui::ExternalImage>(); _text_fields.clear(); + _text_fields["output_name"] = LabeledField("output name"); _text_fields["output_name"]._text_field->set_text(TEMP_SETTING(output_name).get().valueString()); _text_fields["output_name"]._text_field->on_text_changed([this](){ @@ -54,11 +55,10 @@ VideoOpener::VideoOpener() { if(_buffer) { _buffer->_threshold = number; } - - } catch(...) { - - } + + } catch(...) {} }); + _text_fields["average_samples"] = LabeledField("average_samples"); _text_fields["average_samples"]._text_field->set_text(TEMP_SETTING(average_samples).get().valueString()); _text_fields["average_samples"]._text_field->on_text_changed([this](){ @@ -69,9 +69,23 @@ VideoOpener::VideoOpener() { if(_buffer) { _buffer->restart_background(); } - } catch(...) { - + + } catch(...) {} + }); + + _text_fields["averaging_method"] = LabeledField("averaging_method"); + _text_fields["averaging_method"]._text_field->set_text(TEMP_SETTING(average_samples).get().valueString()); + _text_fields["averaging_method"]._text_field->on_text_changed([this]() { + try { + auto number = Meta::fromStr<grab::averaging_method_t::Class>(_text_fields["averaging_method"]._text_field->text()); + TEMP_SETTING(average_samples) = number; + + if (_buffer) { + _buffer->restart_background(); + } + } + catch (...) {} }); std::vector<Layout::Ptr> objects{}; diff --git a/Application/src/tracker/VideoOpener.h b/Application/src/tracker/VideoOpener.h index a680da7..70d9572 100644 --- a/Application/src/tracker/VideoOpener.h +++ b/Application/src/tracker/VideoOpener.h @@ -31,7 +31,7 @@ public: cv::Mat _local; gpuMat _flt, _img, _mask, _diff, _alpha, _output; cv::Mat _accumulator, _background_copy; - bool _set_copy_background; + bool _set_copy_background = false; uint64_t _background_samples = 0; uint64_t _background_video_index = 0; @@ -42,7 +42,7 @@ public: std::atomic<bool> _terminate = false, _terminate_background = false; std::atomic<double> _playback_index = 0; Timer _video_timer; - double _seconds_between_frames; + double _seconds_between_frames = 0; std::atomic<uint32_t> _threshold = 0; -- GitLab