METHOD 4 FAST!!

This commit is contained in:
Phuntsok Drak-pa 2019-04-08 02:48:25 +02:00
parent 629c2e52c1
commit 41cbb90213
3 changed files with 50 additions and 1 deletions

View File

@ -20,7 +20,9 @@ namespace methods_private {
cv::Mat const& t_ref,
std::vector<std::array<uchar, 3>> 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_ */

View File

@ -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);

View File

@ -4,6 +4,8 @@
#include <algorithm>
#include <array>
#include <cstdlib>
#include <future>
#include <iostream>
#include <optional>
#include <thread>
#include <utility>
@ -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<std::future<std::optional<std::pair<cv::Mat, double>>>>
results{};
std::vector<std::pair<cv::Mat, double>> 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);
}
}
}