NumCpp  2.5.1
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:
75  explicit Cluster(uint32 inClusterId) noexcept :
76  clusterId_(inClusterId)
77  {}
78 
79  //=============================================================================
80  // Description:
89  bool operator==(const Cluster<dtype>& rhs) const noexcept
90  {
91  if (pixels_.size() != rhs.pixels_.size())
92  {
93  return false;
94  }
95 
96  return stl_algorithms::equal(begin(), end(), rhs.begin());
97  }
98 
99  //=============================================================================
100  // Description:
109  bool operator!=(const Cluster<dtype>& rhs) const noexcept
110  {
111  return !(*this == rhs);
112  }
113 
114  //=============================================================================
115  // Description:
124  const Pixel<dtype>& operator[](uint32 inIndex) const noexcept
125  {
126  return pixels_[inIndex];
127  }
128 
129  //=============================================================================
130  // Description:
139  const Pixel<dtype>& at(uint32 inIndex) const
140  {
141  if (inIndex >= pixels_.size())
142  {
143  THROW_INVALID_ARGUMENT_ERROR("index exceeds cluster size.");
144  }
145  return pixels_[inIndex];
146  }
147 
148  //=============================================================================
149  // Description:
155  const_iterator begin() const noexcept
156  {
157  return pixels_.cbegin();
158  }
159 
160  //=============================================================================
161  // Description:
167  const_iterator end() const noexcept
168  {
169  return pixels_.cend();
170  }
171 
172  //=============================================================================
173  // Description:
179  uint32 size() const noexcept
180  {
181  return static_cast<uint32>(pixels_.size());
182  }
183 
184  //=============================================================================
185  // Description:
191  uint32 clusterId() const noexcept
192  {
193  return clusterId_;
194  }
195 
196  //=============================================================================
197  // Description:
203  uint32 rowMin() const noexcept
204  {
205  return rowMin_;
206  }
207 
208  //=============================================================================
209  // Description:
215  uint32 rowMax() const noexcept
216  {
217  return rowMax_;
218  }
219 
220  //=============================================================================
221  // Description:
227  uint32 colMin() const noexcept
228  {
229  return colMin_;
230  }
231 
232  //=============================================================================
233  // Description:
239  uint32 colMax() const noexcept
240  {
241  return colMax_;
242  }
243 
244  //=============================================================================
245  // Description:
251  uint32 height() const noexcept
252  {
253  return rowMax_ - rowMin_ + 1;
254  }
255 
256  //=============================================================================
257  // Description:
263  uint32 width() const noexcept
264  {
265  return colMax_ - colMin_ + 1;
266  }
267 
268  //=============================================================================
269  // Description:
275  dtype intensity() const noexcept
276  {
277  return intensity_;
278  }
279 
280  //=============================================================================
281  // Description:
287  dtype peakPixelIntensity() const noexcept
288  {
289  return peakPixelIntensity_;
290  }
291 
292  //=============================================================================
293  // Description:
299  double eod() const noexcept
300  {
301  return eod_;
302  }
303 
304  //=============================================================================
305  // Description:
311  void addPixel(const Pixel<dtype>& inPixel)
312  {
313  pixels_.push_back(inPixel);
314  intensity_ += inPixel.intensity;
315 
316  // adjust the cluster bounds
317  rowMin_ = std::min(rowMin_, inPixel.row);
318  rowMax_ = std::max(rowMax_, inPixel.row);
319  colMin_ = std::min(colMin_, inPixel.col);
320  colMax_ = std::max(colMax_, inPixel.col);
321  peakPixelIntensity_ = std::max(peakPixelIntensity_, inPixel.intensity);
322 
323  // calculate the energy on detector estimate
324  eod_ = static_cast<double>(peakPixelIntensity_) / static_cast<double>(intensity_);
325  }
326 
327  //=============================================================================
328  // Description:
334  std::string str() const
335  {
336  std::string out;
337  uint32 counter = 0;
339  [&](const Pixel<dtype>& pixel)
340  {
341  out += "Pixel " + utils::num2str(counter++) + ":" + pixel.str();
342  });
343 
344  return out;
345  }
346 
347  //============================================================================
351  void print() const
352  {
353  std::cout << *this;
354  }
355 
356  //=============================================================================
357  // Description:
365  friend std::ostream& operator<<(std::ostream& inStream, const Cluster<dtype>& inCluster)
366  {
367  inStream << inCluster.str();
368  return inStream;
369  }
370 
371  private:
372  //================================Attributes===============================
373  int32 clusterId_{ -1 };
374  std::vector<Pixel<dtype> > pixels_{};
375 
376  uint32 rowMin_{ std::numeric_limits<uint32>::max() }; // largest possible number
377  uint32 rowMax_{ 0 };
378  uint32 colMin_{ std::numeric_limits<uint32>::max() }; // largest possible number
379  uint32 colMax_{ 0 };
380 
381  dtype intensity_{ 0 };
382  dtype peakPixelIntensity_{ 0 };
383 
384  double eod_{ 1.0 };
385  };
386  } // namespace imageProcessing
387 } // 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:275
friend std::ostream & operator<<(std::ostream &inStream, const Cluster< dtype > &inCluster)
Definition: Cluster.hpp:365
uint32 colMin() const noexcept
Definition: Cluster.hpp:227
const Pixel< dtype > & operator[](uint32 inIndex) const noexcept
Definition: Cluster.hpp:124
typename std::vector< Pixel< dtype > >::const_iterator const_iterator
Definition: Cluster.hpp:60
double eod() const noexcept
Definition: Cluster.hpp:299
uint32 rowMax() const noexcept
Definition: Cluster.hpp:215
const Pixel< dtype > & at(uint32 inIndex) const
Definition: Cluster.hpp:139
const_iterator begin() const noexcept
Definition: Cluster.hpp:155
uint32 height() const noexcept
Definition: Cluster.hpp:251
Cluster(uint32 inClusterId) noexcept
Definition: Cluster.hpp:75
bool operator==(const Cluster< dtype > &rhs) const noexcept
Definition: Cluster.hpp:89
uint32 colMax() const noexcept
Definition: Cluster.hpp:239
void addPixel(const Pixel< dtype > &inPixel)
Definition: Cluster.hpp:311
bool operator!=(const Cluster< dtype > &rhs) const noexcept
Definition: Cluster.hpp:109
std::string str() const
Definition: Cluster.hpp:334
dtype peakPixelIntensity() const noexcept
Definition: Cluster.hpp:287
uint32 clusterId() const noexcept
Definition: Cluster.hpp:191
uint32 rowMin() const noexcept
Definition: Cluster.hpp:203
uint32 width() const noexcept
Definition: Cluster.hpp:263
uint32 size() const noexcept
Definition: Cluster.hpp:179
const_iterator end() const noexcept
Definition: Cluster.hpp:167
void print() const
Definition: Cluster.hpp:351
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:142
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:45
NdArray< dtype > min(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: min.hpp:45
std::int32_t int32
Definition: Types.hpp:36
std::uint32_t uint32
Definition: Types.hpp:40