code cleanup, started report, new verbose option

This commit is contained in:
Phuntsok Drak-pa
2019-03-24 19:43:25 +01:00
parent 13e59c2dc4
commit eb1046603d
15 changed files with 2670 additions and 62 deletions

View File

@@ -3,18 +3,18 @@
#include <cmath>
#include <cstdlib>
std::tuple<cv::Mat, cv::Mat> init_image(std::string const &t_input_file) {
std::pair<cv::Mat, cv::Mat> init_image(std::string const &t_input_file) {
cv::Mat input_image = cv::imread(t_input_file, cv::IMREAD_COLOR);
if (!input_image.data) {
spdlog::critical("Could not open or find image!\n");
exit(-1);
}
spdlog::info("Image loaded!");
spdlog::info("Width:\t\t{}", input_image.size().width);
spdlog::info("Height:\t{}", input_image.size().height);
spdlog::debug("Image loaded!");
spdlog::debug("Width:\t\t{}", input_image.size().width);
spdlog::debug("Height:\t{}", input_image.size().height);
cv::Mat process_image(input_image.size().height, input_image.size().width,
CV_8UC3, cv::Scalar(0, 0, 0));
return std::make_tuple(std::move(input_image), process_image);
return std::make_pair(std::move(input_image), process_image);
}
double euclidian_distance(cv::Mat const &t_img1, cv::Mat const &t_img2) {
@@ -28,8 +28,3 @@ double euclidian_distance(cv::Mat const &t_img1, cv::Mat const &t_img2) {
euclidian = std::sqrt(euclidian);
return euclidian;
}
cv::Scalar random_color(std::mt19937 &t_gen) {
static std::uniform_int_distribution<> dis(0, 255);
return cv::Scalar(dis(t_gen), dis(t_gen), dis(t_gen));
}

View File

@@ -1,14 +1,16 @@
#include "common.hh"
#include "method1.hh"
#include "methods.hh"
#include "parseargs.hh"
#include <iostream>
int main(int ac, char **av) {
auto const [input_file, output_file, video_output, iterations, method] =
parse_args(ac, av);
spdlog::info("Input file:\t{}", input_file.native());
spdlog::info("Output file:\t{}", output_file.native());
spdlog::info("Video output:\t{}", video_output);
spdlog::info("Iterations:\t{}", iterations);
auto const [input_file, output_file, video_output, iterations, method,
verbose] = parse_args(ac, av);
spdlog::set_level(verbose ? spdlog::level::debug : spdlog::level::info);
spdlog::debug("Input file:\t{}", input_file.native());
spdlog::debug("Output file:\t{}", output_file.native());
spdlog::debug("Video output:\t{}", video_output);
spdlog::debug("Iterations:\t{}", iterations);
auto [input_image, process_image] = init_image(input_file.native());
std::random_device rd;
std::mt19937 gen(rd());

View File

@@ -1,4 +1,4 @@
#include "method1.hh"
#include "methods.hh"
#include "common.hh"
#include "drawing.hh"
#include <algorithm>
@@ -13,34 +13,21 @@ using randint = std::uniform_int_distribution<>;
using Color = std::array<uchar, 3>;
using ColorSet = std::vector<Color>;
namespace methods_private {
cv::Scalar randomColor(std::mt19937 &t_gen) {
static std::uniform_int_distribution<> dis(0, 255);
return cv::Scalar(dis(t_gen), dis(t_gen), dis(t_gen));
}
void newSquare1(cv::Mat &t_process_img, std::mt19937 &t_gen,
randint &t_rand_pos) {
const int square_size = t_rand_pos(t_gen);
auto square_top_left = cv::Point{t_rand_pos(t_gen), t_rand_pos(t_gen)};
draw_shape(t_process_img, square_top_left, square_size, random_color(t_gen),
draw_shape(t_process_img, square_top_left, square_size, randomColor(t_gen),
Shapes::Square);
}
void method1(cv::Mat &t_reference, cv::Mat &t_output, int t_iterations,
std::mt19937 &t_gen) {
auto diff = euclidian_distance(t_reference, t_output);
auto const max_size =
std::max(t_reference.size().width, t_reference.size().height);
randint dist(0, max_size);
spdlog::info("Beginning method1, initial difference: {}", diff);
while (t_iterations > 0) {
auto temp_image = t_output.clone();
newSquare1(temp_image, t_gen, dist);
if (auto new_diff = euclidian_distance(t_reference, temp_image);
new_diff < diff) {
diff = new_diff;
temp_image.copyTo(t_output);
--t_iterations;
spdlog::info("Iteration {}: diff {}", t_iterations, diff);
}
}
}
void threadedGetColor(cv::Mat &t_reference, ColorSet &t_colors, int t_h) {
if (t_h > t_reference.size().height)
return;
@@ -62,8 +49,9 @@ ColorSet getColorSet(cv::Mat &t_reference) {
for (int h = 0; h < t_reference.size().height; h += thread_nbr) {
std::vector<std::thread> thread_list{};
for (int i = 0; i < thread_nbr; ++i) {
thread_list.push_back(std::thread(threadedGetColor, std::ref(t_reference),
std::ref(res), h + i));
thread_list.push_back(std::thread(methods_private::threadedGetColor,
std::ref(t_reference), std::ref(res),
h + i));
}
for (auto &th : thread_list)
th.join();
@@ -85,26 +73,48 @@ void newSquare2(cv::Mat &t_process_img, std::mt19937 &t_gen,
Shapes::Square);
}
} // namespace methods_private
void method1(cv::Mat &t_reference, cv::Mat &t_output, int t_iterations,
std::mt19937 &t_gen) {
auto diff = euclidian_distance(t_reference, t_output);
auto const max_size =
std::max(t_reference.size().width, t_reference.size().height);
randint dist(0, max_size);
spdlog::debug("Beginning method1, initial difference: {}", diff);
while (t_iterations > 0) {
auto temp_image = t_output.clone();
methods_private::newSquare1(temp_image, t_gen, dist);
if (auto new_diff = euclidian_distance(t_reference, temp_image);
new_diff < diff) {
diff = new_diff;
temp_image.copyTo(t_output);
--t_iterations;
spdlog::debug("Iteration {}: diff {}", t_iterations, diff);
}
}
}
void method2(cv::Mat &t_reference, cv::Mat &t_output, int t_iterations,
std::mt19937 &t_gen) {
auto diff = euclidian_distance(t_reference, t_output);
auto const max_size =
std::max(t_reference.size().width, t_reference.size().height);
randint dist(0, max_size);
spdlog::info("Beginning method2, initial difference: {}", diff);
auto const colors = getColorSet(t_reference);
spdlog::info("Running {} threads.", thread_nbr);
spdlog::info("{} colors detected.", colors.size());
spdlog::debug("Beginning method2, initial difference: {}", diff);
auto const colors = methods_private::getColorSet(t_reference);
spdlog::debug("Running {} threads.", thread_nbr);
spdlog::debug("{} colors detected.", colors.size());
randint rand_color(0, colors.size());
while (t_iterations > 0) {
auto temp_image = t_output.clone();
newSquare2(temp_image, t_gen, colors, dist, rand_color);
methods_private::newSquare2(temp_image, t_gen, colors, dist, rand_color);
if (auto new_diff = euclidian_distance(t_reference, temp_image);
new_diff < diff) {
diff = new_diff;
temp_image.copyTo(t_output);
--t_iterations;
spdlog::info("Iteration {}: diff {}", t_iterations, diff);
spdlog::debug("Iteration {}: diff {}", t_iterations, diff);
}
}
}

View File

@@ -21,7 +21,7 @@ void processFilenames(po::variables_map const &vm, path const &t_input,
}
}
std::tuple<path, path, bool, int, int> parse_args(int t_ac, char **t_av) {
std::tuple<path, path, bool, int, int, bool> parse_args(int t_ac, char **t_av) {
po::options_description desc("Allowed options");
desc.add_options()
("help,h", "Display this help message")
@@ -30,7 +30,8 @@ std::tuple<path, path, bool, int, int> parse_args(int t_ac, char **t_av) {
"Image or video output path (default: input path + \"_output\")")
("method,m", po::value<int>(), "Method number to be used (default: 1)")
("iterations,n", po::value<int>(), "Number of iterations (default: 5000)")
("video,v", "Enable video output");
("video,V", "Enable video output")
("verbose,v", "Enables verbosity");
po::variables_map vm;
po::store(po::parse_command_line(t_ac, t_av, desc), vm);
po::notify(vm);
@@ -49,5 +50,6 @@ std::tuple<path, path, bool, int, int> parse_args(int t_ac, char **t_av) {
output_path,
vm.count("video") ? true : false,
vm.count("iterations") ? vm["iterations"].as<int>() : DEFAULT_ITERATIONS,
vm.count("method") ? vm["method"].as<int>() : 1);
vm.count("method") ? vm["method"].as<int>() : 1,
vm.count("verbose") ? true : false);
}