NumCpp  2.6.2
A Templatized Header Only C++ Implementation of the Python NumPy Library
Centroid.hpp
Go to the documentation of this file.
1 
28 #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:
80  double row() const noexcept
81  {
82  return row_;
83  }
84 
85  //=============================================================================
86  // Description:
92  double col() const noexcept
93  {
94  return col_;
95  }
96 
97  //=============================================================================
98  // Description:
104  dtype intensity() const noexcept
105  {
106  return intensity_;
107  }
108 
109  //=============================================================================
110  // Description:
116  double eod() const noexcept
117  {
118  return eod_;
119  }
120 
121  //=============================================================================
122  // Description:
128  std::string str() const
129  {
130  std::string out;
131  out += "row = " + utils::num2str(row_) + " col = " + utils::num2str(col_);
132  out += " intensity = " + utils::num2str(intensity_) + " eod = " + utils::num2str(eod_) + '\n';
133 
134  return out;
135  }
136 
137  //============================================================================
141  void print() const
142  {
143  std::cout << *this;
144  }
145 
146  //=============================================================================
147  // Description:
156  bool operator==(const Centroid<dtype>& rhs) const noexcept
157  {
158  return row_ == rhs.row_ && col_ == rhs.col_ && intensity_ == rhs.intensity_ && eod_ == rhs.eod_;
159  }
160 
161  //=============================================================================
162  // Description:
171  bool operator!=(const Centroid<dtype>& rhs) const noexcept
172  {
173  return !(*this == rhs);
174  }
175 
176  //=============================================================================
177  // Description:
189  bool operator<(const Centroid<dtype>& rhs) const noexcept
190  {
191  return intensity_ < rhs.intensity_ ? false : true;
192  }
193 
194  //=============================================================================
195  // Description:
203  friend std::ostream& operator<<(std::ostream& inStream, const Centroid<dtype>& inCentriod)
204  {
205  inStream << inCentriod.str();
206  return inStream;
207  }
208 
209  private:
210  //==================================Attributes================================///
211  double row_{ 0.0 };
212  double col_{ 0.0 };
213  dtype intensity_{ 0 };
214  double eod_{ 0.0 };
215 
216  //=============================================================================
217  // Description:
225  void centerOfMass(const Cluster<dtype>& inCluster)
226  {
227  const Shape clusterShape(inCluster.height(), inCluster.width());
228  NdArray<dtype> clusterArray(clusterShape);
229  clusterArray.zeros();
230 
231  const uint32 rowMin = inCluster.rowMin();
232  const uint32 colMin = inCluster.colMin();
233 
234  for (auto& pixel : inCluster)
235  {
236  clusterArray(pixel.row - rowMin, pixel.col - colMin) = pixel.intensity;
237  }
238 
239  const auto rowCol = nc::centerOfMass(clusterArray);
240  row_ = rowCol.front() + rowMin;
241  col_ = rowCol.back() + colMin;
242  }
243  };
244  } // namespace imageProcessing
245 } // namespace nc
holds the information for a centroid
Definition: Centroid.hpp:49
bool operator<(const Centroid< dtype > &rhs) const noexcept
Definition: Centroid.hpp:189
double eod() const noexcept
Definition: Centroid.hpp:116
void print() const
Definition: Centroid.hpp:141
double col() const noexcept
Definition: Centroid.hpp:92
bool operator==(const Centroid< dtype > &rhs) const noexcept
Definition: Centroid.hpp:156
Centroid(const Cluster< dtype > &inCluster)
Definition: Centroid.hpp:66
friend std::ostream & operator<<(std::ostream &inStream, const Centroid< dtype > &inCentriod)
Definition: Centroid.hpp:203
bool operator!=(const Centroid< dtype > &rhs) const noexcept
Definition: Centroid.hpp:171
dtype intensity() const noexcept
Definition: Centroid.hpp:104
double row() const noexcept
Definition: Centroid.hpp:80
std::string str() const
Definition: Centroid.hpp:128
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