From 0295de096d5ed23d7ec5457ded74d964af241d68 Mon Sep 17 00:00:00 2001 From: Phuntsok Drak-pa Date: Wed, 20 Mar 2019 11:40:17 +0100 Subject: [PATCH] added helper functions and fixed inverted width and height --- include/genimg/drawing.hh | 6 ++++-- src/common.cc | 2 +- src/drawing.cc | 23 +++++++++++++++++++---- src/main.cc | 6 ++++++ 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/include/genimg/drawing.hh b/include/genimg/drawing.hh index 4a1b838..6fead7b 100644 --- a/include/genimg/drawing.hh +++ b/include/genimg/drawing.hh @@ -4,7 +4,9 @@ #include #include -void draw_square(cv::Mat &t_img, cv::Point const &t_top_left, int const size, - cv::Scalar const &t_color); +enum class Shapes { Square, Circle }; + +void draw_shape(cv::Mat &t_img, cv::Point const &t_top_left, int const t_size, + cv::Scalar const &t_color, Shapes const &t_shape); #endif /* GENETIC_IMAGE_INCLUDE_GENIMG_DRAWING_HH_ */ diff --git a/src/common.cc b/src/common.cc index a29fec6..bbab9a8 100644 --- a/src/common.cc +++ b/src/common.cc @@ -12,7 +12,7 @@ std::tuple init_image(std::string const &t_input_file) { spdlog::info("Image loaded!"); spdlog::info("Width:\t\t{}", input_image.size().width); spdlog::info("Height:\t{}", input_image.size().height); - cv::Mat process_image(input_image.size().width, 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); } diff --git a/src/drawing.cc b/src/drawing.cc index c4b9c25..8f85ded 100644 --- a/src/drawing.cc +++ b/src/drawing.cc @@ -1,13 +1,28 @@ #include "drawing.hh" +#include #include #include +#include -void draw_square(cv::Mat &t_img, cv::Point const &t_top_left, int const size, +void drawSquare(cv::Mat &t_img, cv::Point const &t_top_left, int const t_size, cv::Scalar const &t_color) { std::unique_ptr points(new cv::Point[4]); points.get()[0] = t_top_left; - points.get()[1] = cv::Point{t_top_left.x, t_top_left.y + size}; - points.get()[2] = cv::Point{t_top_left.x + size, t_top_left.y + size}; - points.get()[3] = cv::Point{t_top_left.x + size, t_top_left.y}; + points.get()[1] = cv::Point{t_top_left.x, t_top_left.y + t_size}; + points.get()[2] = cv::Point{t_top_left.x + t_size, t_top_left.y + t_size}; + points.get()[3] = cv::Point{t_top_left.x + t_size, t_top_left.y}; fillConvexPoly(t_img, points.get(), 4, t_color); } + +void draw_shape(cv::Mat &t_img, cv::Point const &t_top_left, int const t_size, + cv::Scalar const &t_color, Shapes const &t_shape) { + switch (t_shape) { + case Shapes::Square: { + drawSquare(t_img, t_top_left, t_size, t_color); + break; + } + default: + spdlog::error("Shape does not exist. Aborting..."); + std::exit(1); + } +} diff --git a/src/main.cc b/src/main.cc index fe501a2..3bb07ed 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,5 +1,6 @@ #include "common.hh" #include "parseargs.hh" +#include "drawing.hh" int main(int ac, char **av) { auto const [input_file, output_file, video_output, iterations] = @@ -10,6 +11,11 @@ int main(int ac, char **av) { spdlog::info("Iterations:\t{}", iterations); auto [input_image, process_image] = init_image(input_file); + draw_shape(process_image, cv::Point{0, 0}, 10, cv::Scalar(100, 100, 100), + Shapes::Square); + + cv::imwrite("some.jpg", process_image); + // Launch image generation return 0;