21 #ifndef IMAGE_MANIPULATOR_H_
22 #define IMAGE_MANIPULATOR_H_
28 #include <stb_image.h>
29 #include <stb_image_write.h>
30 #include <opencv2/imgcodecs.hpp>
31 #include <opencv2/imgproc.hpp>
32 #include <opencv2/opencv.hpp>
41 template <
typename DType_>
53 template <
typename OutType_>
56 static void show_image(
const std::string& filepath,
size_t delay = 0) {
58 std::string msg{
"Unable to open file '" + filepath +
"'." };
60 throw std::runtime_error{ msg };
63 cv::Mat image = cv::imread(filepath, cv::IMREAD_UNCHANGED);
64 cv::imshow(filepath, image);
68 static void show_image(
const cv::Mat image,
size_t delay = 0) {
69 cv::imshow(
"IMAGE", image);
73 static std::vector<uint8_t>
resize(
const std::vector<uint8_t>& in,
size_t orig_w,
size_t orig_h,
size_t new_w,
74 size_t new_h,
size_t num_channels = 3);
76 static void store_JPEG(
const std::string& filepath,
const std::vector<std::uint8_t>& in,
size_t w,
size_t h,
77 size_t quality,
size_t num_channels);
80 static std::vector<float>
to_float32(
const std::vector<std::uint8_t>& in);
83 static std::vector<std::uint8_t>
to_uint8(
const std::vector<float>& in);
100 static void store_PNG(
const std::string& filepath,
const std::vector<uint8_t>& in,
size_t w,
size_t h,
101 size_t num_channels) {
102 auto res{ stbi_write_png(filepath.c_str(), w, h, num_channels, (uint8_t*)in.data(), w * num_channels) };
105 std::string msg{
"Writing the '" + filepath +
"' image failed!" };
107 throw std::runtime_error(msg);
111 static void store_PNG(
const std::string& filepath,
const std::vector<float>& in,
size_t w,
size_t h,
112 size_t num_channels) {
113 auto res{ stbi_write_png(filepath.c_str(), w, h, num_channels, (
float*)in.data(), w * num_channels) };
116 std::string msg{
"Writing the '" + filepath +
"' image failed!" };
118 throw std::runtime_error(msg);
122 static void store_JPEG(
const std::string& filepath,
const std::vector<float>& in,
size_t w,
size_t h,
123 size_t quality = 100,
size_t num_channels = 3,
bool are_ints =
false);
Basic abstraction for images.
Definition: image-processor.h:42
std::vector< DType_ > _data
Definition: image-processor.h:47
size_t num_channels
Definition: image-processor.h:46
size_t w
Definition: image-processor.h:44
size_t h
Definition: image-processor.h:45
Provides utilities for image manipulation and processing.
Definition: image-processor.h:51
static void store_PNG(const std::string &filepath, const std::vector< float > &in, size_t w, size_t h, size_t num_channels)
Definition: image-processor.h:111
static std::vector< uint8_t > resize(const std::vector< uint8_t > &in, size_t orig_w, size_t orig_h, size_t new_w, size_t new_h, size_t num_channels=3)
Definition: image-processor.cpp:31
static void show_image(const cv::Mat image, size_t delay=0)
Definition: image-processor.h:68
static void store_PNG(const std::string &filepath, const std::vector< uint8_t > &in, size_t w, size_t h, size_t num_channels)
Loads the image from the provided filepath.
Definition: image-processor.h:100
static void store_JPEG(const std::string &filepath, const std::vector< float > &in, size_t w, size_t h, size_t quality=100, size_t num_channels=3, bool are_ints=false)
static std::vector< std::uint8_t > to_uint8(const std::vector< float > &in)
Returns a copy of float buffer from the uint8 one.
Definition: image-processor.cpp:61
static OutType_ load_image(const std::string &filepath)
static void show_image(const std::string &filepath, size_t delay=0)
Definition: image-processor.h:56
static std::vector< float > to_float32(const std::vector< std::uint8_t > &in)
Returns a copy of uint8 buffer from the float one.
Definition: image-processor.cpp:52
static void store_JPEG(const std::string &filepath, const std::vector< std::uint8_t > &in, size_t w, size_t h, size_t quality, size_t num_channels)
Definition: image-processor.cpp:116
bool file_exists(const std::string &filepath)
Definition: os-utils.hpp:148
Definition: common-types.h:33
#define SHLOG_E(x)
Definition: static-logger.hpp:157