Beginning refactoring, adding triangles to the list of possible shapes

This commit is contained in:
Phuntsok Drak-pa
2019-04-27 15:45:39 +02:00
parent 326eb7c9e8
commit 012508e523
7 changed files with 281 additions and 75 deletions

View File

@@ -1,7 +1,7 @@
#pragma once
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include "shapes.hh"
#include <array>
#include <spdlog/spdlog.h>
#include <string>
#include <vector>
@@ -19,11 +19,13 @@ class ImageManipulator {
/// \brief Load image from input, and prepare for output
ImageManipulator(std::string const t_input_path,
std::string const t_output_path,
int const iterations);
int const iterations,
Shape::ShapeType const t_shape);
/// \brief Basically makes views from image
ImageManipulator(cv::Mat const& t_origin_image,
int const iterations,
int const t_iterations,
Shape::ShapeType const t_shape,
int const t_x,
int const t_y,
int const t_width,
@@ -67,41 +69,59 @@ class ImageManipulator {
/// \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;
/// \brief Generates random square coordinates
[[nodiscard]] auto get_square_values() const noexcept;
[[deprecated]] [[nodiscard]] auto get_square_values() const noexcept;
/// \brief Generates controlled random square coordinates
[[nodiscard]] auto get_controlled_square_values() const noexcept;
[[deprecated]] [[nodiscard]] auto get_controlled_square_values() const
noexcept;
/// \brief Generates a candidate for image generation improvement
[[nodiscard]] auto create_candidate(bool const t_controlled_size) const;
[[nodiscard]] auto create_candidate(bool const t_controlled_size);
/// \brief Generates organized views of the reference image for method 5
[[nodiscard]] auto generate_tiles(int const t_cols, int const t_rows) const;
/// \brief Gets all colors from the reference image
void get_color_set();
/// \brief Threaded helper for \ref get_color_set
void threaded_get_color(int const t_h);
/// \brief Draw a square on an image
void draw_square(cv::Mat& t_img,
cv::Point const& t_top_left,
int const t_size,
cv::Scalar const& t_color) const;
[[deprecated]] void draw_square(cv::Mat& t_img,
cv::Point const& t_top_left,
int const t_size,
cv::Scalar const& t_color) const;
void draw_shape(cv::Mat& t_img, cv::Scalar&& t_color);
/// \brief Update this objects generated image
void update_gen_image(cv::Mat const& t_img, double const t_diff);
/// \brief Merges tiles generated by method5
void merge_tiles(std::vector<std::vector<ImageManipulator>> const& t_tiles);
/// \brief First method as described in the
/// [report](https://labs.phundrak.fr/phundrak/genetic-images/blob/master/report/report.pdf)
void method1();
/// \brief Second method as described in the
/// [report](https://labs.phundrak.fr/phundrak/genetic-images/blob/master/report/report.pdf)
void method2();
/// \brief Third method as described in the
/// [report](https://labs.phundrak.fr/phundrak/genetic-images/blob/master/report/report.pdf)
void method3();
/// \brief Fourth method as described in the
/// [report](https://labs.phundrak.fr/phundrak/genetic-images/blob/master/report/report.pdf)
void method4(bool const t_controlled_size);
/// \brief Fifth method as described in the
/// [report](https://labs.phundrak.fr/phundrak/genetic-images/blob/master/report/report.pdf)
void method5(bool const t_controlled_size,
@@ -116,6 +136,7 @@ class ImageManipulator {
cv::Mat generated_image_{
reference_.size().height, reference_.size().width, CV_8UC3,
cv::Scalar(0, 0, 0)}; /*!< Working, generated image */
Shape shape_{Shape::ShapeType::Square};
mutable std::mutex
colors_mutex_{}; /*!< Thread mutex for color set generation */
std::string const output_path_{}; /*!< Write path for the generated image */

View File

@@ -1,15 +1,20 @@
#pragma once
#include <filesystem>
#include <tuple>
#include "shapes.hh"
struct ParsedArgs {
std::filesystem::path input_path;
std::filesystem::path output_path;
Shape::ShapeType shape;
int iterations;
int method;
int cols;
int rows;
int submethod;
bool controlled_size;
bool verbose;
};
/// \brief Parses the arguments passed to the program
[[nodiscard]] auto parse_args(int, char**) -> std::tuple<std::filesystem::path,
std::filesystem::path,
int,
int,
int,
int,
int,
bool,
bool>;
[[nodiscard]] auto parse_args(int, char**) -> ParsedArgs;

63
include/genimg/shapes.hh Normal file
View File

@@ -0,0 +1,63 @@
#pragma once
#include <array>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
class Shape {
public:
static constexpr int MAX_POINTS{4};
enum class ShapeType { Square, Triangle };
/// \brief Default constructor
Shape() = delete;
Shape(Shape::ShapeType const t_type);
/// \brief Copy constructor
Shape(const Shape& other) = default;
/// \brief Move constructor
Shape(Shape&& other) noexcept;
/// \brief Destructor
virtual ~Shape() noexcept = default;
/// \brief Copy assignment operator
Shape& operator=(const Shape& other) = delete;
/// \brief Move assignment operator
Shape& operator=(Shape&& other) noexcept = delete;
/// \brief Generates a shape's points
void operator()(cv::Point&& t_max_pos,
int const t_max_size,
int const t_min_size = 0) noexcept;
[[nodiscard]] auto get_points() const noexcept
-> std::array<cv::Point, Shape::MAX_POINTS> const&
{
return points_;
}
/// \brief Returns the type of shape described by the object
[[nodiscard]] auto get_type() const noexcept -> ShapeType const&
{
return type_;
}
[[nodiscard]] auto get_nb_points() const noexcept { return nb_points_; }
protected:
private:
void create_square_points(cv::Point const& t_top_left,
int const t_size) noexcept;
void create_triangle_points(cv::Point const& t_top_left,
int const t_size) noexcept;
ShapeType const type_{ShapeType::Square};
std::array<cv::Point, Shape::MAX_POINTS> points_{
cv::Point{0, 0}, cv::Point{0, 0}, cv::Point{0, 0}, cv::Point{0, 0}};
int nb_points_{Shape::MAX_POINTS};
};