NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
Cluster.hpp
Go to the documentation of this file.
1
28
29#pragma once
30
31#include <algorithm>
32#include <iostream>
33#include <limits>
34#include <string>
35#include <type_traits>
36#include <vector>
37
41#include "NumCpp/Core/Types.hpp"
44
45namespace nc::imageProcessing
46{
47 //================================================================================
48 // Class Description:
50 template<typename dtype>
51 class Cluster
52 {
53 private:
55
56 public:
57 //================================Typedefs===============================
58 using const_iterator = typename std::vector<Pixel<dtype>>::const_iterator;
59 using accumulator_t = typename std::conditional<std::is_integral<dtype>::value, int64, double>::type;
60
61 //=============================================================================
62 // Description:
65 Cluster() = default;
66
67 //=============================================================================
68 // Description:
73 explicit Cluster(uint32 inClusterId) noexcept :
74 clusterId_(inClusterId)
75 {
76 }
77
78 //=============================================================================
79 // Description:
86 bool operator==(const Cluster<dtype>& rhs) const noexcept
87 {
88 if (pixels_.size() != rhs.pixels_.size())
89 {
90 return false;
91 }
92
93 return stl_algorithms::equal(begin(), end(), rhs.begin());
94 }
95
96 //=============================================================================
97 // Description:
104 bool operator!=(const Cluster<dtype>& rhs) const noexcept
105 {
106 return !(*this == rhs);
107 }
108
109 //=============================================================================
110 // Description:
117 const Pixel<dtype>& operator[](uint32 inIndex) const noexcept
118 {
119 return pixels_[inIndex];
120 }
121
122 //=============================================================================
123 // Description:
130 [[nodiscard]] const Pixel<dtype>& at(uint32 inIndex) const
131 {
132 if (inIndex >= pixels_.size())
133 {
134 THROW_INVALID_ARGUMENT_ERROR("index exceeds cluster size.");
135 }
136 return pixels_[inIndex];
137 }
138
139 //=============================================================================
140 // Description:
145 [[nodiscard]] const_iterator begin() const noexcept
146 {
147 return pixels_.cbegin();
148 }
149
150 //=============================================================================
151 // Description:
156 [[nodiscard]] const_iterator end() const noexcept
157 {
158 return pixels_.cend();
159 }
160
161 //=============================================================================
162 // Description:
167 [[nodiscard]] uint32 size() const noexcept
168 {
169 return static_cast<uint32>(pixels_.size());
170 }
171
172 //=============================================================================
173 // Description:
178 [[nodiscard]] uint32 clusterId() const noexcept
179 {
180 return clusterId_;
181 }
182
183 //=============================================================================
184 // Description:
189 [[nodiscard]] uint32 rowMin() const noexcept
190 {
191 return rowMin_;
192 }
193
194 //=============================================================================
195 // Description:
200 [[nodiscard]] uint32 rowMax() const noexcept
201 {
202 return rowMax_;
203 }
204
205 //=============================================================================
206 // Description:
211 [[nodiscard]] uint32 colMin() const noexcept
212 {
213 return colMin_;
214 }
215
216 //=============================================================================
217 // Description:
222 [[nodiscard]] uint32 colMax() const noexcept
223 {
224 return colMax_;
225 }
226
227 //=============================================================================
228 // Description:
233 [[nodiscard]] uint32 height() const noexcept
234 {
235 return rowMax_ - rowMin_ + 1;
236 }
237
238 //=============================================================================
239 // Description:
244 [[nodiscard]] uint32 width() const noexcept
245 {
246 return colMax_ - colMin_ + 1;
247 }
248
249 //=============================================================================
250 // Description:
255 [[nodiscard]] accumulator_t intensity() const noexcept
256 {
257 return intensity_;
258 }
259
260 //=============================================================================
261 // Description:
266 [[nodiscard]] dtype peakPixelIntensity() const noexcept
267 {
268 return peakPixelIntensity_;
269 }
270
271 //=============================================================================
272 // Description:
277 [[nodiscard]] double eod() const noexcept
278 {
279 return eod_;
280 }
281
282 //=============================================================================
283 // Description:
288 void addPixel(const Pixel<dtype>& inPixel)
289 {
290 pixels_.push_back(inPixel);
291 intensity_ += static_cast<accumulator_t>(inPixel.intensity);
292
293 // adjust the cluster bounds
294 rowMin_ = std::min(rowMin_, inPixel.row);
295 rowMax_ = std::max(rowMax_, inPixel.row);
296 colMin_ = std::min(colMin_, inPixel.col);
297 colMax_ = std::max(colMax_, inPixel.col);
298 peakPixelIntensity_ = std::max(peakPixelIntensity_, inPixel.intensity);
299
300 // calculate the energy on detector estimate
301 eod_ = static_cast<double>(peakPixelIntensity_) / static_cast<double>(intensity_);
302 }
303
304 //=============================================================================
305 // Description:
310 [[nodiscard]] std::string str() const
311 {
312 std::string out;
313 uint32 counter = 0;
314 std::for_each(begin(),
315 end(),
316 [&](const Pixel<dtype>& pixel)
317 { out += "Pixel " + utils::num2str(counter++) + ":" + pixel.str(); });
318
319 return out;
320 }
321
322 //============================================================================
326 void print() const
327 {
328 std::cout << *this;
329 }
330
331 //=============================================================================
332 // Description:
339 friend std::ostream& operator<<(std::ostream& inStream, const Cluster<dtype>& inCluster)
340 {
341 inStream << inCluster.str();
342 return inStream;
343 }
344
345 private:
346 //================================Attributes===============================
348 int32 clusterId_{ -1 };
350 std::vector<Pixel<dtype>> pixels_{};
352 uint32 rowMin_{ std::numeric_limits<uint32>::max() }; // largest possible number
354 uint32 rowMax_{ 0 };
356 uint32 colMin_{ std::numeric_limits<uint32>::max() }; // largest possible number
358 uint32 colMax_{ 0 };
360 accumulator_t intensity_{ 0 };
362 dtype peakPixelIntensity_{ 0 };
364 dtype minPixel{};
366 dtype maxPixel{};
368 double eod_{ 1. };
369 };
370} // namespace nc::imageProcessing
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition Error.hpp:37
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition StaticAsserts.hpp:39
typename std::conditional< std::is_integral< dtype >::value, int64, double >::type accumulator_t
Definition Cluster.hpp:59
uint32 colMin() const noexcept
Definition Cluster.hpp:211
double eod() const noexcept
Definition Cluster.hpp:277
uint32 rowMax() const noexcept
Definition Cluster.hpp:200
const_iterator begin() const noexcept
Definition Cluster.hpp:145
uint32 height() const noexcept
Definition Cluster.hpp:233
Cluster(uint32 inClusterId) noexcept
Definition Cluster.hpp:73
bool operator==(const Cluster< dtype > &rhs) const noexcept
Definition Cluster.hpp:86
friend std::ostream & operator<<(std::ostream &inStream, const Cluster< dtype > &inCluster)
Definition Cluster.hpp:339
uint32 colMax() const noexcept
Definition Cluster.hpp:222
void addPixel(const Pixel< dtype > &inPixel)
Definition Cluster.hpp:288
bool operator!=(const Cluster< dtype > &rhs) const noexcept
Definition Cluster.hpp:104
std::string str() const
Definition Cluster.hpp:310
dtype peakPixelIntensity() const noexcept
Definition Cluster.hpp:266
uint32 clusterId() const noexcept
Definition Cluster.hpp:178
accumulator_t intensity() const noexcept
Definition Cluster.hpp:255
uint32 rowMin() const noexcept
Definition Cluster.hpp:189
const Pixel< dtype > & at(uint32 inIndex) const
Definition Cluster.hpp:130
uint32 width() const noexcept
Definition Cluster.hpp:244
typename std::vector< Pixel< dtype > >::const_iterator const_iterator
Definition Cluster.hpp:58
uint32 size() const noexcept
Definition Cluster.hpp:167
const Pixel< dtype > & operator[](uint32 inIndex) const noexcept
Definition Cluster.hpp:117
const_iterator end() const noexcept
Definition Cluster.hpp:156
void print() const
Definition Cluster.hpp:326
Holds the information for a single pixel.
Definition Pixel.hpp:46
dtype intensity
Definition Pixel.hpp:55
uint32 col
Definition Pixel.hpp:54
uint32 row
Definition Pixel.hpp:53
std::string str() const
Definition Pixel.hpp:136
Definition applyThreshold.hpp:34
bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2) noexcept
Definition StlAlgorithms.hpp:141
std::string num2str(dtype inNumber)
Definition num2str.hpp:44
std::int32_t int32
Definition Types.hpp:36
std::int64_t int64
Definition Types.hpp:35
std::uint32_t uint32
Definition Types.hpp:40