NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
Centroid.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <cmath>
31#include <iostream>
32#include <string>
33#include <type_traits>
34
36#include "NumCpp/Core/Types.hpp"
39#include "NumCpp/NdArray.hpp"
42#include "NumCpp/Utils/sqr.hpp"
43
44namespace nc::imageProcessing
45{
46 //================================================================================
47 // Class Description:
49 template<typename dtype>
51 {
52 private:
54
55 public:
56 using accumulator_t = typename std::conditional<std::is_integral<dtype>::value, int64, double>::type;
57
58 //=============================================================================
59 // Description:
62 Centroid() = default;
63
64 //=============================================================================
65 // Description:
70 explicit Centroid(const Cluster<dtype>& inCluster) :
71 intensity_(inCluster.intensity()),
72 eod_(inCluster.eod())
73 {
74 centerOfMass(inCluster);
75 setEllipseProperties(inCluster);
76 }
77
78 //=============================================================================
79 // Description:
84 [[nodiscard]] double row() const noexcept
85 {
86 return row_;
87 }
88
89 //=============================================================================
90 // Description:
95 [[nodiscard]] double col() const noexcept
96 {
97 return col_;
98 }
99
100 //=============================================================================
101 // Description:
106 [[nodiscard]] accumulator_t intensity() const noexcept
107 {
108 return intensity_;
109 }
110
111 //=============================================================================
112 // Description:
117 [[nodiscard]] double eod() const noexcept
118 {
119 return eod_;
120 }
121
122 //=============================================================================
123 // Description:
128 [[nodiscard]] double a() const noexcept
129 {
130 return a_;
131 }
132
133 //=============================================================================
134
135 // Description:
140 [[nodiscard]] double b() const noexcept
141 {
142 return b_;
143 }
144
145 //=============================================================================
146 // Description:
151 [[nodiscard]] double eccentricity() const noexcept
152 {
153 return eccentricity_;
154 }
155
156 //=============================================================================
157
158 // Description:
163 [[nodiscard]] double orientation() const noexcept
164 {
165 return orientation_;
166 }
167
168 //=============================================================================
169 // Description:
174 [[nodiscard]] std::string str() const
175 {
176 std::string out = "row = " + utils::num2str(row_) + " col = " + utils::num2str(col_) +
177 " intensity = " + utils::num2str(intensity_) + " eod = " + utils::num2str(eod_) +
178 " a = " + utils::num2str(a_) + " b = " + utils::num2str(b_) +
179 " eccentricity = " + utils::num2str(eccentricity_) +
180 " orientation = " + utils::num2str(orientation_) + '\n';
181
182 return out;
183 }
184
185 //============================================================================
189 void print() const
190 {
191 std::cout << *this;
192 }
193
194 //=============================================================================
195 // Description:
202 bool operator==(const Centroid<dtype>& rhs) const noexcept
203 {
204 return (utils::essentiallyEqual(row_, rhs.row_) && utils::essentiallyEqual(col_, rhs.col_) &&
205 utils::essentiallyEqual(intensity_, rhs.intensity_) && utils::essentiallyEqual(eod_, rhs.eod_) &&
206 utils::essentiallyEqual(a_, rhs.a_) && utils::essentiallyEqual(b_, rhs.b_) &&
207 utils::essentiallyEqual(eccentricity_, rhs.eccentricity_) &&
208 utils::essentiallyEqual(orientation_, rhs.orientation_));
209 }
210
211 //=============================================================================
212 // Description:
219 bool operator!=(const Centroid<dtype>& rhs) const noexcept
220 {
221 return !(*this == rhs);
222 }
223
224 //=============================================================================
225 // Description:
235 bool operator<(const Centroid<dtype>& rhs) const noexcept
236 {
237 return intensity_ < rhs.intensity_ ? false : true;
238 }
239
240 //=============================================================================
241 // Description:
248 friend std::ostream& operator<<(std::ostream& inStream, const Centroid<dtype>& inCentriod)
249 {
250 inStream << inCentriod.str();
251 return inStream;
252 }
253
254 private:
255 //==================================Attributes================================///
256 double row_{ 0. };
257 double col_{ 0. };
258 accumulator_t intensity_{ 0 };
259 double eod_{ 0. };
261 double a_{};
263 double b_{};
265 double eccentricity_{};
267 double orientation_{};
268
269 //=============================================================================
270 // Description:
277 void centerOfMass(const Cluster<dtype>& inCluster)
278 {
279 const Shape clusterShape(inCluster.height(), inCluster.width());
280 NdArray<dtype> clusterArray(clusterShape);
281 clusterArray.zeros();
282
283 const uint32 rowMin = inCluster.rowMin();
284 const uint32 colMin = inCluster.colMin();
285
286 for (auto& pixel : inCluster)
287 {
288 clusterArray(pixel.row - rowMin, pixel.col - colMin) = pixel.intensity;
289 }
290
291 const auto rowCol = nc::centerOfMass(clusterArray);
292 row_ = rowCol.front() + rowMin;
293 col_ = rowCol.back() + colMin;
294 }
295
296 //=============================================================================
297 // Description:
302 void setEllipseProperties(const Cluster<dtype>& inCluster) noexcept
303 {
304 constexpr auto two = static_cast<double>(2.);
305
306 auto m20 = static_cast<double>(0.);
307 auto m02 = static_cast<double>(0.);
308 auto m11 = static_cast<double>(0.);
309
310 for (typename Cluster<dtype>::const_iterator iter = inCluster.begin(); iter != inCluster.end(); ++iter)
311 {
312 const auto& pixel = *iter;
313 const double deltaX = pixel.col - col_;
314 const double deltaY = pixel.row - row_;
315
316 m11 += deltaX * deltaY;
317 m20 += utils::sqr(deltaX);
318 m02 += utils::sqr(deltaY);
319 }
320
321 const auto numPixels = static_cast<double>(inCluster.size());
322 m11 /= numPixels;
323 m20 /= numPixels;
324 m02 /= numPixels;
325
326 double piece1 = m20 + m02;
327 piece1 /= two;
328
329 double piece2 = std::sqrt(static_cast<double>(4.) * utils::sqr(m11) + utils::sqr(m20 - m02));
330 piece2 /= two;
331
332 const double lambda1 = piece1 - piece2;
333 const double lambda2 = piece1 + piece2;
334
335 eccentricity_ = std::sqrt(static_cast<double>(1.) - lambda1 / lambda2);
336 orientation_ = static_cast<double>(-0.5) * std::atan2(two * m11, m20 - m02);
337 a_ = two * std::sqrt(lambda2);
338 b_ = two * std::sqrt(lambda1);
339 }
340 };
341} // namespace nc::imageProcessing
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition StaticAsserts.hpp:39
bool operator<(const Centroid< dtype > &rhs) const noexcept
Definition Centroid.hpp:235
double eod() const noexcept
Definition Centroid.hpp:117
friend std::ostream & operator<<(std::ostream &inStream, const Centroid< dtype > &inCentriod)
Definition Centroid.hpp:248
double b() const noexcept
Definition Centroid.hpp:140
void print() const
Definition Centroid.hpp:189
accumulator_t intensity() const noexcept
Definition Centroid.hpp:106
double col() const noexcept
Definition Centroid.hpp:95
bool operator==(const Centroid< dtype > &rhs) const noexcept
Definition Centroid.hpp:202
Centroid(const Cluster< dtype > &inCluster)
Definition Centroid.hpp:70
double eccentricity() const noexcept
Definition Centroid.hpp:151
bool operator!=(const Centroid< dtype > &rhs) const noexcept
Definition Centroid.hpp:219
double row() const noexcept
Definition Centroid.hpp:84
std::string str() const
Definition Centroid.hpp:174
typename std::conditional< std::is_integral< dtype >::value, int64, double >::type accumulator_t
Definition Centroid.hpp:56
double a() const noexcept
Definition Centroid.hpp:128
double orientation() const noexcept
Definition Centroid.hpp:163
Holds the information for a cluster of pixels.
Definition Cluster.hpp:52
typename std::vector< Pixel< dtype > >::const_iterator const_iterator
Definition Cluster.hpp:58
Definition applyThreshold.hpp:34
std::string num2str(dtype inNumber)
Definition num2str.hpp:44
bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
Definition essentiallyEqual.hpp:47
constexpr dtype sqr(dtype inValue) noexcept
Definition sqr.hpp:42
NdArray< double > centerOfMass(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition centerOfMass.hpp:46
std::int64_t int64
Definition Types.hpp:35
std::uint32_t uint32
Definition Types.hpp:40