even less duplicate code

This commit is contained in:
Phuntsok Drak-pa 2019-04-02 10:54:01 +02:00
parent 72b91ab1b1
commit 3bd03afd1d
3 changed files with 37 additions and 25 deletions

View File

@ -12,6 +12,9 @@ namespace methods_private {
[[nodiscard]] auto randomColor(); [[nodiscard]] auto randomColor();
[[nodiscard]] auto getColorSet(cv::Mat const& t_reference); [[nodiscard]] auto getColorSet(cv::Mat const& t_reference);
[[nodiscard]] auto getSquareValues(cv::Mat const& t_img); [[nodiscard]] auto getSquareValues(cv::Mat const& t_img);
[[nodiscard]] auto getControlledSquareValues(cv::Mat const& t_img,
int const t_init_iter,
int const t_iter);
[[nodiscard]] auto createCandidate( [[nodiscard]] auto createCandidate(
cv::Mat const& t_base, cv::Mat const& t_base,
cv::Mat const& t_ref, cv::Mat const& t_ref,

View File

@ -12,7 +12,7 @@
exit(-1); exit(-1);
} }
spdlog::debug("Image loaded!"); spdlog::debug("Image loaded!");
spdlog::debug("Width:\t\t{}", input_image.size().width); spdlog::debug("Width:\t{}", input_image.size().width);
spdlog::debug("Height:\t{}", input_image.size().height); spdlog::debug("Height:\t{}", input_image.size().height);
cv::Mat process_image(input_image.size().height, input_image.size().width, cv::Mat process_image(input_image.size().height, input_image.size().width,
CV_8UC3, cv::Scalar(0, 0, 0)); CV_8UC3, cv::Scalar(0, 0, 0));

View File

@ -52,15 +52,38 @@ namespace methods_private {
return std::tuple<int, int, int>(rand_x, rand_y, size); return std::tuple<int, int, int>(rand_x, rand_y, size);
} }
[[nodiscard]] auto getControlledSquareValues(cv::Mat const& t_img,
int const t_init_iter,
int const t_iter)
{
int rand_x = rand() % t_img.size().width;
int rand_y = rand() % t_img.size().height;
float const coef
= static_cast<float>(t_iter) / static_cast<float>(t_init_iter);
int const min_size = static_cast<int>(
(static_cast<float>(std::min(t_img.size().width, t_img.size().height))
/ 2.0f)
* coef);
int const max_size = min_size * 2 + 1;
int size = rand() % (max_size - min_size) + min_size;
return std::tuple<int, int, int>(rand_x, rand_y, size);
}
[[nodiscard]] auto createCandidate(cv::Mat const& t_base, [[nodiscard]] auto createCandidate(cv::Mat const& t_base,
cv::Mat const& t_ref, cv::Mat const& t_ref,
ColorSet const& t_colors, ColorSet const& t_colors,
double const diff, double const diff,
bool const t_controlled_size) bool const t_controlled_size = false,
int const t_init_iter = 0,
int const t_iter = 0)
{ {
auto temp_image = t_base.clone(); auto temp_image = t_base.clone();
// auto const [rand_x, rand_y, size]
// = methods_private::getSquareValues(temp_image);
auto const [rand_x, rand_y, size] auto const [rand_x, rand_y, size]
= methods_private::getSquareValues(temp_image); = t_controlled_size ? methods_private::getSquareValues(temp_image)
: methods_private::getControlledSquareValues(
temp_image, t_init_iter, t_iter);
methods_private::newSquare2(temp_image, cv::Point{rand_x, rand_y}, size, methods_private::newSquare2(temp_image, cv::Point{rand_x, rand_y}, size,
t_colors[rand() % t_colors.size()]); t_colors[rand() % t_colors.size()]);
auto new_diff = euclidian_distance(t_ref, temp_image); auto new_diff = euclidian_distance(t_ref, temp_image);
@ -148,8 +171,8 @@ void method2(cv::Mat const& t_reference, cv::Mat& t_output, int t_iterations)
auto const colors = methods_private::getColorSet(t_reference); auto const colors = methods_private::getColorSet(t_reference);
spdlog::debug("{} colors detected.", colors.size()); spdlog::debug("{} colors detected.", colors.size());
while (t_iterations > 0) { while (t_iterations > 0) {
if (auto result = methods_private::createCandidate(t_output, t_reference, if (auto result
colors, diff, false); = methods_private::createCandidate(t_output, t_reference, colors, diff);
result.has_value()) { result.has_value()) {
diff = result->second; diff = result->second;
result->first.copyTo(t_output); result->first.copyTo(t_output);
@ -170,27 +193,13 @@ void method3(cv::Mat const& t_reference, cv::Mat& t_output, int t_iterations)
while (t_iterations > 0) { while (t_iterations > 0) {
auto temp_image = t_output.clone(); auto temp_image = t_output.clone();
int const rand_x = rand() % temp_image.size().width; if (auto result = methods_private::createCandidate(
int const rand_y = rand() % temp_image.size().height; t_output, t_reference, colors, diff, true, init_iter, t_iterations);
float const coef result.has_value()) {
= static_cast<float>(t_iterations) / static_cast<float>(init_iter); diff = result->second;
int const min_size = static_cast<int>( result->first.copyTo(t_output);
(static_cast<float>(
std::min(t_reference.size().width, t_reference.size().height))
/ 2.0f)
* coef);
int const max_size = min_size * 2 + 1;
int const size = rand() % (max_size - min_size) + min_size;
methods_private::newSquare2(temp_image, cv::Point{rand_x, rand_y}, size,
colors[rand() % colors.size()]);
if (auto new_diff = euclidian_distance(t_reference, temp_image);
new_diff < diff) {
diff = new_diff;
temp_image.copyTo(t_output);
spdlog::debug("iteration:{} diff:{} size: {} coef:{} min:{} max:{}",
t_iterations, diff, size, coef, min_size, max_size);
--t_iterations; --t_iterations;
spdlog::debug("iteration:{} diff:{}", t_iterations, diff);
} }
} }
} }