NumCpp  2.7.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Centroid.hpp
Go to the documentation of this file.
1 #pragma once
29 
31 #include "NumCpp/Core/Types.hpp"
34 #include "NumCpp/NdArray.hpp"
35 #include "NumCpp/Utils/num2str.hpp"
36 
37 #include <iostream>
38 #include <string>
39 
40 namespace nc
41 {
42  namespace imageProcessing
43  {
44  //================================================================================
45  // Class Description:
47  template<typename dtype>
48  class Centroid
49  {
50  private:
51  STATIC_ASSERT_ARITHMETIC(dtype);
52 
53  public:
54  //=============================================================================
55  // Description:
58  Centroid() = default;
59 
60  //=============================================================================
61  // Description:
66  explicit Centroid(const Cluster<dtype>& inCluster) :
67  intensity_(inCluster.intensity()),
68  eod_(inCluster.eod())
69  {
70  centerOfMass(inCluster);
71  }
72 
73  //=============================================================================
74  // Description:
79  double row() const noexcept
80  {
81  return row_;
82  }
83 
84  //=============================================================================
85  // Description:
90  double col() const noexcept
91  {
92  return col_;
93  }
94 
95  //=============================================================================
96  // Description:
101  dtype intensity() const noexcept
102  {
103  return intensity_;
104  }
105 
106  //=============================================================================
107  // Description:
112  double eod() const noexcept
113  {
114  return eod_;
115  }
116 
117  //=============================================================================
118  // Description:
123  std::string str() const
124  {
125  std::string out;
126  out += "row = " + utils::num2str(row_) + " col = " + utils::num2str(col_);
127  out += " intensity = " + utils::num2str(intensity_) + " eod = " + utils::num2str(eod_) + '\n';
128 
129  return out;
130  }
131 
132  //============================================================================
136  void print() const
137  {
138  std::cout << *this;
139  }
140 
141  //=============================================================================
142  // Description:
149  bool operator==(const Centroid<dtype>& rhs) const noexcept
150  {
151  return row_ == rhs.row_ && col_ == rhs.col_ && intensity_ == rhs.intensity_ && eod_ == rhs.eod_;
152  }
153 
154  //=============================================================================
155  // Description:
162  bool operator!=(const Centroid<dtype>& rhs) const noexcept
163  {
164  return !(*this == rhs);
165  }
166 
167  //=============================================================================
168  // Description:
178  bool operator<(const Centroid<dtype>& rhs) const noexcept
179  {
180  return intensity_ < rhs.intensity_ ? false : true;
181  }
182 
183  //=============================================================================
184  // Description:
191  friend std::ostream& operator<<(std::ostream& inStream, const Centroid<dtype>& inCentriod)
192  {
193  inStream << inCentriod.str();
194  return inStream;
195  }
196 
197  private:
198  //==================================Attributes================================///
199  double row_{ 0.0 };
200  double col_{ 0.0 };
201  dtype intensity_{ 0 };
202  double eod_{ 0.0 };
203 
204  //=============================================================================
205  // Description:
212  void centerOfMass(const Cluster<dtype>& inCluster)
213  {
214  const Shape clusterShape(inCluster.height(), inCluster.width());
215  NdArray<dtype> clusterArray(clusterShape);
216  clusterArray.zeros();
217 
218  const uint32 rowMin = inCluster.rowMin();
219  const uint32 colMin = inCluster.colMin();
220 
221  for (auto& pixel : inCluster)
222  {
223  clusterArray(pixel.row - rowMin, pixel.col - colMin) = pixel.intensity;
224  }
225 
226  const auto rowCol = nc::centerOfMass(clusterArray);
227  row_ = rowCol.front() + rowMin;
228  col_ = rowCol.back() + colMin;
229  }
230  };
231  } // namespace imageProcessing
232 } // namespace nc
holds the information for a centroid
Definition: Centroid.hpp:49
bool operator<(const Centroid< dtype > &rhs) const noexcept
Definition: Centroid.hpp:178
double eod() const noexcept
Definition: Centroid.hpp:112
void print() const
Definition: Centroid.hpp:136
double col() const noexcept
Definition: Centroid.hpp:90
bool operator==(const Centroid< dtype > &rhs) const noexcept
Definition: Centroid.hpp:149
Centroid(const Cluster< dtype > &inCluster)
Definition: Centroid.hpp:66
friend std::ostream & operator<<(std::ostream &inStream, const Centroid< dtype > &inCentriod)
Definition: Centroid.hpp:191
bool operator!=(const Centroid< dtype > &rhs) const noexcept
Definition: Centroid.hpp:162
dtype intensity() const noexcept
Definition: Centroid.hpp:101
double row() const noexcept
Definition: Centroid.hpp:79
std::string str() const
Definition: Centroid.hpp:123
Holds the information for a cluster of pixels.
Definition: Cluster.hpp:54
std::string num2str(dtype inNumber)
Definition: num2str.hpp:46
Definition: Coordinate.hpp:45
NdArray< double > centerOfMass(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: centerOfMass.hpp:47
std::uint32_t uint32
Definition: Types.hpp:40