NumCpp  2.1.0
A C++ implementation of the Python Numpy library
Cluster.hpp
Go to the documentation of this file.
1 
30 #pragma once
31 
35 #include "NumCpp/Core/Types.hpp"
37 #include "NumCpp/Utils/num2str.hpp"
38 
39 #include <algorithm>
40 #include <iostream>
41 #include <limits>
42 #include <string>
43 #include <utility>
44 #include <vector>
45 
46 namespace nc
47 {
48  namespace imageProcessing
49  {
50  //================================================================================
51  // Class Description:
53  template<typename dtype>
54  class Cluster
55  {
56  private:
57  STATIC_ASSERT_ARITHMETIC(dtype);
58 
59  public:
60  //================================Typedefs===============================
61  using const_iterator = typename std::vector<Pixel<dtype> >::const_iterator;
62 
63  //=============================================================================
64  // Description:
67  Cluster() = default;
68 
69  //=============================================================================
70  // Description:
76  explicit Cluster(uint32 inClusterId) noexcept :
77  clusterId_(inClusterId)
78  {}
79 
80  //=============================================================================
81  // Description:
90  bool operator==(const Cluster<dtype>& rhs) const noexcept
91  {
92  if (pixels_.size() != rhs.pixels_.size())
93  {
94  return false;
95  }
96 
97  return stl_algorithms::equal(begin(), end(), rhs.begin());
98  }
99 
100  //=============================================================================
101  // Description:
110  bool operator!=(const Cluster<dtype>& rhs) const noexcept
111  {
112  return !(*this == rhs);
113  }
114 
115  //=============================================================================
116  // Description:
125  const Pixel<dtype>& operator[](uint32 inIndex) const noexcept
126  {
127  return pixels_[inIndex];
128  }
129 
130  //=============================================================================
131  // Description:
140  const Pixel<dtype>& at(uint32 inIndex) const
141  {
142  if (inIndex >= pixels_.size())
143  {
144  THROW_INVALID_ARGUMENT_ERROR("index exceeds cluster size.");
145  }
146  return pixels_[inIndex];
147  }
148 
149  //=============================================================================
150  // Description:
156  const_iterator begin() const noexcept
157  {
158  return pixels_.cbegin();
159  }
160 
161  //=============================================================================
162  // Description:
168  const_iterator end() const noexcept
169  {
170  return pixels_.cend();
171  }
172 
173  //=============================================================================
174  // Description:
180  uint32 size() const noexcept
181  {
182  return static_cast<uint32>(pixels_.size());
183  }
184 
185  //=============================================================================
186  // Description:
192  uint32 clusterId() const noexcept
193  {
194  return clusterId_;
195  }
196 
197  //=============================================================================
198  // Description:
204  uint32 rowMin() const noexcept
205  {
206  return rowMin_;
207  }
208 
209  //=============================================================================
210  // Description:
216  uint32 rowMax() const noexcept
217  {
218  return rowMax_;
219  }
220 
221  //=============================================================================
222  // Description:
228  uint32 colMin() const noexcept
229  {
230  return colMin_;
231  }
232 
233  //=============================================================================
234  // Description:
240  uint32 colMax() const noexcept
241  {
242  return colMax_;
243  }
244 
245  //=============================================================================
246  // Description:
252  uint32 height() const noexcept
253  {
254  return rowMax_ - rowMin_ + 1;
255  }
256 
257  //=============================================================================
258  // Description:
264  uint32 width() const noexcept
265  {
266  return colMax_ - colMin_ + 1;
267  }
268 
269  //=============================================================================
270  // Description:
276  dtype intensity() const noexcept
277  {
278  return intensity_;
279  }
280 
281  //=============================================================================
282  // Description:
288  dtype peakPixelIntensity() const noexcept
289  {
290  return peakPixelIntensity_;
291  }
292 
293  //=============================================================================
294  // Description:
300  double eod() const noexcept
301  {
302  return eod_;
303  }
304 
305  //=============================================================================
306  // Description:
312  void addPixel(const Pixel<dtype>& inPixel)
313  {
314  pixels_.push_back(inPixel);
315  intensity_ += inPixel.intensity;
316 
317  // adjust the cluster bounds
318  rowMin_ = std::min(rowMin_, inPixel.row);
319  rowMax_ = std::max(rowMax_, inPixel.row);
320  colMin_ = std::min(colMin_, inPixel.col);
321  colMax_ = std::max(colMax_, inPixel.col);
322  peakPixelIntensity_ = std::max(peakPixelIntensity_, inPixel.intensity);
323 
324  // calculate the energy on detector estimate
325  eod_ = static_cast<double>(peakPixelIntensity_) / static_cast<double>(intensity_);
326  }
327 
328  //=============================================================================
329  // Description:
335  std::string str() const
336  {
337  std::string out;
338  uint32 counter = 0;
340  [&](const Pixel<dtype>& pixel)
341  {
342  out += "Pixel " + utils::num2str(counter++) + ":" + pixel.str();
343  });
344 
345  return out;
346  }
347 
348  //============================================================================
352  void print() const
353  {
354  std::cout << *this;
355  }
356 
357  //=============================================================================
358  // Description:
366  friend std::ostream& operator<<(std::ostream& inStream, const Cluster<dtype>& inCluster)
367  {
368  inStream << inCluster.str();
369  return inStream;
370  }
371 
372  private:
373  //================================Attributes===============================
374  int32 clusterId_{ -1 };
375  std::vector<Pixel<dtype> > pixels_{};
376 
377  uint32 rowMin_{ std::numeric_limits<uint32>::max() }; // largest possible number
378  uint32 rowMax_{ 0 };
379  uint32 colMin_{ std::numeric_limits<uint32>::max() }; // largest possible number
380  uint32 colMax_{ 0 };
381 
382  dtype intensity_{ 0 };
383  dtype peakPixelIntensity_{ 0 };
384 
385  double eod_{ 1.0 };
386  };
387  } // namespace imageProcessing
388 } // namespace nc
nc::imageProcessing::Cluster::eod
double eod() const noexcept
Definition: Cluster.hpp:300
StaticAsserts.hpp
nc::int32
std::int32_t int32
Definition: Types.hpp:37
Pixel.hpp
nc::imageProcessing::Cluster::height
uint32 height() const noexcept
Definition: Cluster.hpp:252
Error.hpp
nc::imageProcessing::Cluster::size
uint32 size() const noexcept
Definition: Cluster.hpp:180
nc::imageProcessing::Cluster::peakPixelIntensity
dtype peakPixelIntensity() const noexcept
Definition: Cluster.hpp:288
nc::imageProcessing::Pixel::str
std::string str() const
Definition: Pixel.hpp:143
nc::imageProcessing::Cluster::rowMax
uint32 rowMax() const noexcept
Definition: Cluster.hpp:216
nc::utils::num2str
std::string num2str(dtype inNumber)
Definition: num2str.hpp:47
nc::imageProcessing::Cluster::at
const Pixel< dtype > & at(uint32 inIndex) const
Definition: Cluster.hpp:140
nc::imageProcessing::Cluster::print
void print() const
Definition: Cluster.hpp:352
nc::stl_algorithms::for_each
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:214
nc::uint32
std::uint32_t uint32
Definition: Types.hpp:41
nc::imageProcessing::Cluster::operator==
bool operator==(const Cluster< dtype > &rhs) const noexcept
Definition: Cluster.hpp:90
num2str.hpp
nc::imageProcessing::Cluster::colMin
uint32 colMin() const noexcept
Definition: Cluster.hpp:228
nc::imageProcessing::Cluster::operator<<
friend std::ostream & operator<<(std::ostream &inStream, const Cluster< dtype > &inCluster)
Definition: Cluster.hpp:366
nc
Definition: Coordinate.hpp:45
nc::imageProcessing::Pixel::intensity
dtype intensity
Definition: Pixel.hpp:57
nc::imageProcessing::Cluster::intensity
dtype intensity() const noexcept
Definition: Cluster.hpp:276
THROW_INVALID_ARGUMENT_ERROR
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
nc::max
NdArray< dtype > max(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: max.hpp:46
nc::imageProcessing::Cluster::operator[]
const Pixel< dtype > & operator[](uint32 inIndex) const noexcept
Definition: Cluster.hpp:125
StlAlgorithms.hpp
nc::imageProcessing::Cluster::width
uint32 width() const noexcept
Definition: Cluster.hpp:264
nc::imageProcessing::Cluster::Cluster
Cluster()=default
nc::imageProcessing::Cluster::str
std::string str() const
Definition: Cluster.hpp:335
nc::stl_algorithms::equal
bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2) noexcept
Definition: StlAlgorithms.hpp:136
nc::imageProcessing::Cluster::begin
const_iterator begin() const noexcept
Definition: Cluster.hpp:156
nc::imageProcessing::Pixel::col
uint32 col
Definition: Pixel.hpp:56
Types.hpp
nc::imageProcessing::Cluster::const_iterator
typename std::vector< Pixel< dtype > >::const_iterator const_iterator
Definition: Cluster.hpp:61
nc::imageProcessing::Cluster::clusterId
uint32 clusterId() const noexcept
Definition: Cluster.hpp:192
nc::imageProcessing::Cluster::operator!=
bool operator!=(const Cluster< dtype > &rhs) const noexcept
Definition: Cluster.hpp:110
nc::imageProcessing::Cluster::Cluster
Cluster(uint32 inClusterId) noexcept
Definition: Cluster.hpp:76
nc::imageProcessing::Cluster::rowMin
uint32 rowMin() const noexcept
Definition: Cluster.hpp:204
nc::imageProcessing::Cluster
Holds the information for a cluster of pixels.
Definition: Cluster.hpp:54
nc::imageProcessing::Pixel::row
uint32 row
Definition: Pixel.hpp:55
nc::min
NdArray< dtype > min(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: min.hpp:46
nc::imageProcessing::Cluster::end
const_iterator end() const noexcept
Definition: Cluster.hpp:168
nc::imageProcessing::Pixel
Holds the information for a single pixel.
Definition: Pixel.hpp:47
nc::imageProcessing::Cluster::colMax
uint32 colMax() const noexcept
Definition: Cluster.hpp:240
nc::imageProcessing::Cluster::addPixel
void addPixel(const Pixel< dtype > &inPixel)
Definition: Cluster.hpp:312