NumCpp  1.0
A C++ implementation of the Python Numpy library
Centroid.hpp
Go to the documentation of this file.
1 #pragma once
30 
31 #include "NumCpp/Core/Types.hpp"
35 #include "NumCpp/NdArray.hpp"
36 #include "NumCpp/Utils/num2str.hpp"
37 
38 #include <iostream>
39 #include <string>
40 
41 namespace nc
42 {
43  namespace imageProcessing
44  {
45  //================================================================================
46  // Class Description:
48  template<typename dtype>
49  class Centroid
50  {
51  private:
52  STATIC_ASSERT_ARITHMETIC(dtype);
53 
54  public:
55  //=============================================================================
56  // Description:
59  Centroid() = default;
60 
61  //=============================================================================
62  // Description:
67  Centroid(const Cluster<dtype>& inCluster) :
68  intensity_(inCluster.intensity()),
69  eod_(inCluster.eod())
70  {
71  centerOfMass(inCluster);
72  }
73 
74  //=============================================================================
75  // Description:
81  double row() const noexcept
82  {
83  return row_;
84  }
85 
86  //=============================================================================
87  // Description:
93  double col() const noexcept
94  {
95  return col_;
96  }
97 
98  //=============================================================================
99  // Description:
105  dtype intensity() const noexcept
106  {
107  return intensity_;
108  }
109 
110  //=============================================================================
111  // Description:
117  double eod() const noexcept
118  {
119  return eod_;
120  }
121 
122  //=============================================================================
123  // Description:
129  std::string str() const
130  {
131  std::string out;
132  out += "row = " + utils::num2str(row_) + " col = " + utils::num2str(col_);
133  out += " intensity = " + utils::num2str(intensity_) + " eod = " + utils::num2str(eod_) + "\n";
134 
135  return out;
136  }
137 
138  //============================================================================
142  void print() const
143  {
144  std::cout << *this;
145  }
146 
147  //=============================================================================
148  // Description:
157  bool operator==(const Centroid<dtype>& rhs) const noexcept
158  {
159  return row_ == rhs.row_ && col_ == rhs.col_ && intensity_ == rhs.intensity_ && eod_ == rhs.eod_;
160  }
161 
162  //=============================================================================
163  // Description:
172  bool operator!=(const Centroid<dtype>& rhs) const noexcept
173  {
174  return !(*this == rhs);
175  }
176 
177  //=============================================================================
178  // Description:
190  bool operator<(const Centroid<dtype>& rhs) const noexcept
191  {
192  return intensity_ < rhs.intensity_ ? false : true;
193  }
194 
195  //=============================================================================
196  // Description:
204  friend std::ostream& operator<<(std::ostream& inStream, const Centroid<dtype>& inCentriod)
205  {
206  inStream << inCentriod.str();
207  return inStream;
208  }
209 
210  private:
211  //==================================Attributes================================///
212  double row_{ 0.0 };
213  double col_{ 0.0 };
214  dtype intensity_{ 0 };
215  double eod_{ 0.0 };
216 
217  //=============================================================================
218  // Description:
226  void centerOfMass(const Cluster<dtype>& inCluster)
227  {
228  const Shape clusterShape(inCluster.height(), inCluster.width());
229  NdArray<dtype> clusterArray(clusterShape);
230  clusterArray.zeros();
231 
232  const uint32 rowMin = inCluster.rowMin();
233  const uint32 colMin = inCluster.colMin();
234 
235  for (auto& pixel : inCluster)
236  {
237  clusterArray(pixel.row - rowMin, pixel.col - colMin) = pixel.intensity;
238  }
239 
240  const auto rowCol = nc::centerOfMass(clusterArray);
241  row_ = rowCol.front() + rowMin;
242  col_ = rowCol.back() + colMin;
243  }
244  };
245  }
246 }
StaticAsserts.hpp
nc::imageProcessing::Centroid::operator!=
bool operator!=(const Centroid< dtype > &rhs) const noexcept
Definition: Centroid.hpp:172
nc::imageProcessing::Centroid::intensity
dtype intensity() const noexcept
Definition: Centroid.hpp:105
nc::utils::num2str
std::string num2str(dtype inNumber)
Definition: num2str.hpp:47
nc::imageProcessing::Centroid::str
std::string str() const
Definition: Centroid.hpp:129
nc::imageProcessing::Centroid::Centroid
Centroid(const Cluster< dtype > &inCluster)
Definition: Centroid.hpp:67
nc::imageProcessing::Centroid::operator==
bool operator==(const Centroid< dtype > &rhs) const noexcept
Definition: Centroid.hpp:157
nc::uint32
std::uint32_t uint32
Definition: Types.hpp:41
nc::imageProcessing::Centroid::eod
double eod() const noexcept
Definition: Centroid.hpp:117
NdArray.hpp
num2str.hpp
nc::imageProcessing::Centroid::operator<
bool operator<(const Centroid< dtype > &rhs) const noexcept
Definition: Centroid.hpp:190
nc::imageProcessing::Centroid::col
double col() const noexcept
Definition: Centroid.hpp:93
nc::imageProcessing::Centroid::row
double row() const noexcept
Definition: Centroid.hpp:81
nc::imageProcessing::Centroid::Centroid
Centroid()=default
nc::imageProcessing::Centroid::operator<<
friend std::ostream & operator<<(std::ostream &inStream, const Centroid< dtype > &inCentriod)
Definition: Centroid.hpp:204
nc
Definition: Coordinate.hpp:45
Cluster.hpp
centerOfMass.hpp
nc::imageProcessing::Centroid::print
void print() const
Definition: Centroid.hpp:142
Types.hpp
nc::imageProcessing::Centroid
holds the information for a centroid
Definition: Centroid.hpp:49
nc::imageProcessing::Cluster
Holds the information for a cluster of pixels.
Definition: Cluster.hpp:54
nc::centerOfMass
NdArray< double > centerOfMass(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: centerOfMass.hpp:48