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