diff --git a/include/genimg/methods.hh b/include/genimg/methods.hh index 7bde168..762a63d 100644 --- a/include/genimg/methods.hh +++ b/include/genimg/methods.hh @@ -20,7 +20,9 @@ namespace methods_private { cv::Mat const& t_ref, std::vector> const& t_colors, double const diff, - bool const t_controlled_size); + bool const t_controlled_size, + int const t_init_iter, + int const t_iter); void adjustSize(cv::Mat const& t_process_img, cv::Point& t_top_left, int t_size); @@ -41,4 +43,6 @@ void method2(cv::Mat const&, cv::Mat&, int); void method3(cv::Mat const&, cv::Mat&, int); +void method4(cv::Mat const&, cv::Mat&, int); + #endif /* GENETIC_IMAGE_INCLUDE_GENIMG_METHODS_HH_ */ diff --git a/src/main.cc b/src/main.cc index 20c6c8c..f45c933 100644 --- a/src/main.cc +++ b/src/main.cc @@ -29,6 +29,10 @@ int main(int ac, char** av) method3(input_image, process_image, iterations); break; } + case 4: { + method4(input_image, process_image, iterations); + break; + } default: spdlog::error("Requested method {} is not implemented."); std::exit(-1); diff --git a/src/methods.cc b/src/methods.cc index 68243db..f0fb5aa 100644 --- a/src/methods.cc +++ b/src/methods.cc @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include #include #include @@ -201,3 +203,42 @@ void method3(cv::Mat const& t_reference, cv::Mat& t_output, int t_iterations) } } } + +void method4(cv::Mat const& t_reference, cv::Mat& t_output, int t_iterations) +{ + auto const init_iter = t_iterations; + auto diff = euclidian_distance(t_reference, t_output); + spdlog::debug("Beginning method2, initial difference: {}", diff); + spdlog::debug("Running {} threads.", thread_nbr); + auto const colors = methods_private::getColorSet(t_reference); + spdlog::debug("{} colors detected.", colors.size()); + + while (t_iterations > 0) { + std::vector>>> + results{}; + std::vector> values{}; + for (unsigned i = 0; i < thread_nbr; ++i) { + results.push_back( + std::async(std::launch::async, methods_private::createCandidate, + std::ref(t_output), std::ref(t_reference), + std::ref(colors), diff, true, init_iter, t_iterations)); + } + for (auto& elem : results) { + if (auto res = elem.get(); res.has_value() && res->second < diff) { + values.push_back(*res); + } + } + if(values.size() > 0) { + unsigned best = 0; + for(unsigned i = 0; i < values.size(); ++i) { + if(values[i].second < values[best].second) { + best = i; + } + } + diff = values[best].second; + values[best].first.copyTo(t_output); + --t_iterations; + spdlog::debug("iteration:{} diff:{}", t_iterations, diff); + } + } +}