Volume Cartographer 2.28.0
Common.hpp
Go to the documentation of this file.
1#pragma once
2
5#include <algorithm>
6#include <tuple>
7#include <vector>
8
9#include <opencv2/core.hpp>
10
11#define BGR_RED cv::Scalar(0, 0, 0xFF)
12#define BGR_GREEN cv::Scalar(0, 0xFF, 0)
13#define BGR_BLUE cv::Scalar(0xFF, 0, 0)
14#define BGR_YELLOW cv::Scalar(0, 0xFF, 0xFF)
15#define BGR_MAGENTA cv::Scalar(0xFF, 0, 0xFF)
16#define BGR_BLACK cv::Scalar(0, 0, 0)
17
18using IndexIntensityPair = std::pair<int, double>;
19using IndexIntensityPairVec = std::vector<IndexIntensityPair>;
20using Voxel = cv::Vec3d;
21using Pixel = cv::Vec2d;
22
34template <typename T1, typename T2>
35std::ostream& operator<<(std::ostream& s, std::pair<T1, T2> p)
36{
37 return s << "(" << std::get<0>(p) << ", " << std::get<1>(p) << ")";
38}
39
51template <typename T>
52std::ostream& operator<<(std::ostream& s, std::vector<T> v)
53{
54 s << "[";
55 if (v.size() == 0) {
56 return s << "]";
57 } else if (v.size() == 1) {
58 return s << v[0] << "]";
59 }
60 // Need - 2 because v.end() points to one past the end of v.
61 std::for_each(v.begin(), v.end() - 1, [&s](const T& t) { s << t << ", "; });
62 return s << v.back() << "]";
63}
64
66{
67
84template <typename T1, typename T2>
85std::vector<std::pair<T1, T2>> Zip(
86 const std::vector<T1>& v1, const std::vector<T2>& v2)
87{
88 assert(v1.size() == v2.size() && "v1 and v2 must be the same size");
89 std::vector<std::pair<T1, T2>> res;
90 res.reserve(v1.size());
91 for (int i = 0; i < int(v1.size()); ++i) {
92 res.push_back(std::make_pair(v1[i], v2[i]));
93 }
94 return res;
95}
96
105template <typename T, int Length>
106std::pair<std::vector<T>, std::vector<T>> Unzip(
107 const std::vector<cv::Vec<T, Length>>& vs)
108{
109 std::vector<T> xs, ys;
110 xs.reserve(vs.size());
111 ys.reserve(vs.size());
112 for (const auto& v : vs) {
113 xs.push_back(v(0));
114 ys.push_back(v(1));
115 }
116 return std::make_pair(xs, ys);
117}
118
128template <typename T>
129std::vector<double> NormalizeVector(
130 const std::vector<T>& v, double newMin = 0, double newMax = 1)
131{
132 // Check if values are already in desired range
133 if (std::all_of(std::begin(v), std::end(v), [newMin, newMax](T e) {
134 return newMin <= e && e <= newMax;
135 })) {
136 return std::vector<double>{std::begin(v), std::end(v)};
137 }
138
139 // Input checking
140 if (v.empty()) {
141 return std::vector<double>{};
142 }
143 if (v.size() == 1) {
144 return std::vector<double>{newMax};
145 }
146
147 auto p = std::minmax_element(begin(v), end(v));
148 auto min = *p.first;
149 auto max = *p.second;
150 std::vector<double> vNorm(v.size());
151
152 // Normalization of [min, max] --> [0, 1]
153 std::transform(
154 begin(v), end(v), begin(vNorm), [min, max, newMin, newMax](T t) {
155 return ((newMax - newMin) / double(max - min)) * t +
156 ((newMin * max - min * newMax) / double(max - min));
157 });
158 return vNorm;
159}
160
168template <typename T, int Len>
169std::vector<cv::Vec<double, Len>> NormalizeVector(
170 const std::vector<cv::Vec<T, Len>> vs)
171{
172 std::vector<cv::Vec<double, Len>> vsNew(vs.size());
173 std::transform(begin(vs), end(vs), std::begin(vsNew), [](auto v) {
174 cv::Vec<double, Len> dv(v);
175 if (cv::norm(dv) < 1e-5) {
176 return dv;
177 } else {
178 return dv / cv::norm(dv);
179 }
180 });
181 return vsNew;
182}
183
184// Some useful utility functions for doing math on std::vectors
195std::vector<double> SquareDiff(
196 const std::vector<Voxel>& v1, const std::vector<Voxel>& v2);
197
207 const std::vector<double>& v1, const std::vector<double>& v2);
209} // namespace volcart::segmentation
std::ostream & operator<<(std::ostream &s, const volcart::Tensor3D< DType > &tensor)
Write a Tensor3D to an output stream.
Definition: Tensor3D.hpp:155
std::vector< double > NormalizeVector(const std::vector< T > &v, double newMin=0, double newMax=1)
Normalize vector elements to within the range [newMin, newMax].
Definition: Common.hpp:129
double SumSquareDiff(const std::vector< double > &v1, const std::vector< double > &v2)
Sums the square differences between two vectors.
std::vector< std::pair< T1, T2 > > Zip(const std::vector< T1 > &v1, const std::vector< T2 > &v2)
Combine two equal-sized vectors into a single vector of paired elements.
Definition: Common.hpp:85
std::vector< double > SquareDiff(const std::vector< Voxel > &v1, const std::vector< Voxel > &v2)
Computes the difference of Squares on two vectors.
std::pair< std::vector< T >, std::vector< T > > Unzip(const std::vector< cv::Vec< T, Length > > &vs)
Separate single vector of paired elements into two vectors of single elements.
Definition: Common.hpp:106
Segmentation algorithms and utilities library