-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.hpp
More file actions
87 lines (73 loc) · 3 KB
/
Copy pathutils.hpp
File metadata and controls
87 lines (73 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#pragma once
#include <xtensor/xarray.hpp>
#include <xtensor/xio.hpp>
#include <opencv2/opencv.hpp>
#include <print>
namespace translation_estimation::utils
{
template <typename T> cv::Mat load_image_to_mat(std::string const& filepath)
{
cv::Mat img = cv::imread(filepath, cv::IMREAD_ANYDEPTH | cv::IMREAD_ANYCOLOR);
if (img.empty()) throw std::runtime_error("Could not load image: " + filepath);
if (img.channels() != 1)
{
std::println("Image has more than one channel. Convert it to grayscale.");
cv::cvtColor(img, img, cv::COLOR_BGR2GRAY);
}
constexpr auto cv_t = std::is_same_v<T, double> ? CV_64F : CV_32F;
if (img.type() != cv_t) img.convertTo(img, cv_t);
return img;
}
template <typename T> xt::xarray<T> load_image_to_xtensor(std::string const& filepath)
{
cv::Mat img = cv::imread(filepath, cv::IMREAD_ANYDEPTH | cv::IMREAD_ANYCOLOR);
if (img.empty()) throw std::runtime_error("Could not load image: " + filepath);
if (img.channels() != 1)
{
std::println("Image has more than one channel. Convert it to grayscale.");
cv::cvtColor(img, img, cv::COLOR_BGR2GRAY);
}
constexpr auto cv_t = std::is_same_v<T, double> ? CV_64F : CV_32F;
if (img.type() != cv_t) img.convertTo(img, cv_t);
if (!img.isContinuous()) img = img.clone();
const size_t rows = img.rows;
const size_t cols = img.cols;
xt::xarray<T> result = xt::zeros<T>({rows, cols});
std::memcpy(result.data(), img.ptr<T>(), rows * cols * sizeof(T));
return result;
}
} // namespace translation_estimation::utils
template <typename T> struct std::formatter<xt::xarray<T>>: std::formatter<std::string>
{
auto format(xt::xarray<T> const& arr, format_context& ctx) const
{
std::ostringstream oss;
oss << std::fixed << std::setprecision(8);
oss << arr;
return std::formatter<std::string>::format(oss.str(), ctx);
}
};
template <> struct std::formatter<cv::Mat>: std::formatter<std::string>
{
auto format(cv::Mat const& mat, format_context& ctx) const
{
std::ostringstream oss;
oss << std::fixed << std::setprecision(8);
/*oss << cv::format(mat, cv::Formatter::FMT_NUMPY);*/
if (mat.type() == CV_32F)
{
auto arr = xt::adapt((float*)mat.data, mat.total(), xt::no_ownership(),
std::vector<size_t>{static_cast<size_t>(mat.rows), static_cast<size_t>(mat.cols)});
oss << arr;
}
else if (mat.type() == CV_64F)
{
auto arr = xt::adapt((double*)mat.data, mat.total(), xt::no_ownership(),
std::vector<size_t>{static_cast<size_t>(mat.rows), static_cast<size_t>(mat.cols)});
oss << arr;
}
else
throw std::exception("Unsupported matrix type.");
return std::formatter<std::string>::format(oss.str(), ctx);
}
};