From e4eeeb6ac25deb2265f687d552ffbdf3e69dc43b Mon Sep 17 00:00:00 2001 From: Phuntsok Drak-pa Date: Thu, 16 May 2019 23:46:56 +0200 Subject: [PATCH] optimized difference computing --- include/genimg/methods.hh | 9 +++------ src/methods.cc | 24 ++---------------------- 2 files changed, 5 insertions(+), 28 deletions(-) diff --git a/include/genimg/methods.hh b/include/genimg/methods.hh index 403fd39..885958a 100644 --- a/include/genimg/methods.hh +++ b/include/genimg/methods.hh @@ -58,10 +58,6 @@ protected: private: // methods ////////////////////////////////////////////////////////////////// - /// \brief Calculates the euclidian distance between two images - [[nodiscard]] auto euclidian_distance(cv::Mat const &t_img) const noexcept - -> double; - /// \brief Creates and returns a random color [[nodiscard]] auto random_color() const noexcept; @@ -119,8 +115,9 @@ private: mutable std::mutex colors_mutex_{}; /*!< Thread mutex for color set generation */ std::string const output_path_{}; /*!< Write path for the generated image */ - double diff_{euclidian_distance(generated_image_)}; /*!< Euclidian difference - between \ref reference_ and \ref generated_image_ */ + double diff_{ + cv::norm(reference_, generated_image_)}; /*!< Euclidian difference + between \ref reference_ and \ref generated_image_ */ int const total_iterations_{0}; /*!< Number of iterations to perform */ int remaining_iter_{ total_iterations_}; /*!< Remaining iterations to perform */ diff --git a/src/methods.cc b/src/methods.cc index d09ae2c..9e1fe1c 100644 --- a/src/methods.cc +++ b/src/methods.cc @@ -133,26 +133,6 @@ void ImageManipulator::exec_method(int const t_nb_method, // methods //////////////////////////////////////////////////////////////////// -/** - * Calculates the euclidian distance between the reference image and the image - * passed as an argument - * - * \param t_img Image with which the distance is computed - * \return double - */ -[[nodiscard]] auto -ImageManipulator::euclidian_distance(cv::Mat const &t_img) const noexcept - -> double -{ - double euclidian = 0.0; - for (auto itr1 = reference_.begin(), itr2 = t_img.begin(); - itr1 != reference_.end() && itr2 != t_img.end(); - ++itr1, ++itr2) { - euclidian += std::pow(*itr1 - *itr2, 2); - } - return std::sqrt(euclidian); -} - /** * \return cv::Scalar */ @@ -203,7 +183,7 @@ ImageManipulator::create_candidate(bool const t_controlled_size = false) draw_shape(temp_img, cv::Scalar{static_cast(color[0]), static_cast(color[1]), static_cast(color[2])}); - auto new_diff = euclidian_distance(temp_img); + const auto new_diff = cv::norm(reference_, temp_img); return (new_diff < diff_) ? std::optional>{std::make_pair( std::move(temp_img), new_diff)} @@ -339,7 +319,7 @@ void ImageManipulator::method1() auto temp_image = generated_image_.clone(); create_shape(); draw_shape(temp_image, random_color()); - if (auto const new_diff = euclidian_distance(temp_image); + if (auto const new_diff = cv::norm(reference_, temp_image); new_diff < diff_) { update_gen_image(temp_image, new_diff); }