NumCpp  2.7.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Cluster.hpp
Go to the documentation of this file.
1 
29 #pragma once
30 
34 #include "NumCpp/Core/Types.hpp"
36 #include "NumCpp/Utils/num2str.hpp"
37 
38 #include <algorithm>
39 #include <iostream>
40 #include <limits>
41 #include <string>
42 #include <utility>
43 #include <vector>
44 
45 namespace nc
46 {
47  namespace imageProcessing
48  {
49  //================================================================================
50  // Class Description:
52  template<typename dtype>
53  class Cluster
54  {
55  private:
56  STATIC_ASSERT_ARITHMETIC(dtype);
57 
58  public:
59  //================================Typedefs===============================
60  using const_iterator = typename std::vector<Pixel<dtype> >::const_iterator;
61 
62  //=============================================================================
63  // Description:
66  Cluster() = default;
67 
68  //=============================================================================
69  // Description:
74  explicit Cluster(uint32 inClusterId) noexcept :
75  clusterId_(inClusterId)
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  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  const_iterator begin() const noexcept
146  {
147  return pixels_.cbegin();
148  }
149 
150  //=============================================================================
151  // Description:
156  const_iterator end() const noexcept
157  {
158  return pixels_.cend();
159  }
160 
161  //=============================================================================
162  // Description:
167  uint32 size() const noexcept
168  {
169  return static_cast<uint32>(pixels_.size());
170  }
171 
172  //=============================================================================
173  // Description:
178  uint32 clusterId() const noexcept
179  {
180  return clusterId_;
181  }
182 
183  //=============================================================================
184  // Description:
189  uint32 rowMin() const noexcept
190  {
191  return rowMin_;
192  }
193 
194  //=============================================================================
195  // Description:
200  uint32 rowMax() const noexcept
201  {
202  return rowMax_;
203  }
204 
205  //=============================================================================
206  // Description:
211  uint32 colMin() const noexcept
212  {
213  return colMin_;
214  }
215 
216  //=============================================================================
217  // Description:
222  uint32 colMax() const noexcept
223  {
224  return colMax_;
225  }
226 
227  //=============================================================================
228  // Description:
233  uint32 height() const noexcept
234  {
235  return rowMax_ - rowMin_ + 1;
236  }
237 
238  //=============================================================================
239  // Description:
244  uint32 width() const noexcept
245  {
246  return colMax_ - colMin_ + 1;
247  }
248 
249  //=============================================================================
250  // Description:
255  dtype intensity() const noexcept
256  {
257  return intensity_;
258  }
259 
260  //=============================================================================
261  // Description:
266  dtype peakPixelIntensity() const noexcept
267  {
268  return peakPixelIntensity_;
269  }
270 
271  //=============================================================================
272  // Description:
277  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_ += 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  std::string str() const
311  {
312  std::string out;
313  uint32 counter = 0;
314  std::for_each(begin(), end(),
315  [&](const Pixel<dtype>& pixel)
316  {
317  out += "Pixel " + utils::num2str(counter++) + ":" + pixel.str();
318  });
319 
320  return out;
321  }
322 
323  //============================================================================
327  void print() const
328  {
329  std::cout << *this;
330  }
331 
332  //=============================================================================
333  // Description:
340  friend std::ostream& operator<<(std::ostream& inStream, const Cluster<dtype>& inCluster)
341  {
342  inStream << inCluster.str();
343  return inStream;
344  }
345 
346  private:
347  //================================Attributes===============================
348  int32 clusterId_{ -1 };
349  std::vector<Pixel<dtype> > pixels_{};
350 
351  uint32 rowMin_{ std::numeric_limits<uint32>::max() }; // largest possible number
352  uint32 rowMax_{ 0 };
353  uint32 colMin_{ std::numeric_limits<uint32>::max() }; // largest possible number
354  uint32 colMax_{ 0 };
355 
356  dtype intensity_{ 0 };
357  dtype peakPixelIntensity_{ 0 };
358 
359  double eod_{ 1.0 };
360  };
361  } // namespace imageProcessing
362 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
Holds the information for a cluster of pixels.
Definition: Cluster.hpp:54
dtype intensity() const noexcept
Definition: Cluster.hpp:255
friend std::ostream & operator<<(std::ostream &inStream, const Cluster< dtype > &inCluster)
Definition: Cluster.hpp:340
uint32 colMin() const noexcept
Definition: Cluster.hpp:211
const Pixel< dtype > & operator[](uint32 inIndex) const noexcept
Definition: Cluster.hpp:117
typename std::vector< Pixel< dtype > >::const_iterator const_iterator
Definition: Cluster.hpp:60
double eod() const noexcept
Definition: Cluster.hpp:277
uint32 rowMax() const noexcept
Definition: Cluster.hpp:200
const Pixel< dtype > & at(uint32 inIndex) const
Definition: Cluster.hpp:130
const_iterator begin() const noexcept
Definition: Cluster.hpp:145
uint32 height() const noexcept
Definition: Cluster.hpp:233
Cluster(uint32 inClusterId) noexcept
Definition: Cluster.hpp:74
bool operator==(const Cluster< dtype > &rhs) const noexcept
Definition: Cluster.hpp:86
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
uint32 rowMin() const noexcept
Definition: Cluster.hpp:189
uint32 width() const noexcept
Definition: Cluster.hpp:244
uint32 size() const noexcept
Definition: Cluster.hpp:167
const_iterator end() const noexcept
Definition: Cluster.hpp:156
void print() const
Definition: Cluster.hpp:327
Holds the information for a single pixel.
Definition: Pixel.hpp:47
dtype intensity
Definition: Pixel.hpp:56
uint32 col
Definition: Pixel.hpp:55
uint32 row
Definition: Pixel.hpp:54
std::string str() const
Definition: Pixel.hpp:135
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:213
bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2) noexcept
Definition: StlAlgorithms.hpp:135
std::string num2str(dtype inNumber)
Definition: num2str.hpp:46
Definition: Coordinate.hpp:45
NdArray< dtype > max(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: max.hpp:44
NdArray< dtype > min(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: min.hpp:44
std::int32_t int32
Definition: Types.hpp:36
std::uint32_t uint32
Definition: Types.hpp:40