NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
ClusterMaker.hpp
Go to the documentation of this file.
1
28
29#pragma once
30
31#include <algorithm>
32#include <set>
33#include <utility>
34#include <vector>
35
38#include "NumCpp/Core/Types.hpp"
40#include "NumCpp/NdArray.hpp"
41
42namespace nc::imageProcessing
43{
44 //=============================================================================
45 // Class Description:
47 template<typename dtype>
49 {
50 private:
52
53 public:
54 //================================Typedefs=====================================
55 using const_iterator = typename std::vector<Cluster<dtype>>::const_iterator;
56
57 //=============================================================================
58 // Description:
65 ClusterMaker(const NdArray<bool>* const inXcdArrayPtr,
66 const NdArray<dtype>* const inIntensityArrayPtr,
67 uint8 inBorderWidth = 0) :
68 xcds_(inXcdArrayPtr),
69 intensities_(inIntensityArrayPtr)
70 {
71 if (xcds_->shape() != intensities_->shape())
72 {
73 THROW_INVALID_ARGUMENT_ERROR("input xcd and intensity arrays must be the same shape.");
74 }
75
76 shape_ = xcds_->shape();
77
78 // convert the NdArray of booleans to a vector of exceedances
79 for (uint32 row = 0; row < shape_.rows; ++row)
80 {
81 for (uint32 col = 0; col < shape_.cols; ++col)
82 {
83 if (xcds_->operator()(row, col))
84 {
85 const Pixel<dtype> thePixel(row, col, intensities_->operator()(row, col));
86 xcdsVec_.push_back(thePixel);
87 }
88 }
89 }
90
91 runClusterMaker();
92
93 for (uint8 i = 0; i < inBorderWidth; ++i)
94 {
95 expandClusters();
96 }
97 }
98
99 //=============================================================================
100 // Description:
105 uint32 size() noexcept
106 {
107 return static_cast<uint32>(clusters_.size());
108 }
109
110 //=============================================================================
111 // Description:
118 const Cluster<dtype>& operator[](uint32 inIndex) const noexcept
119 {
120 return clusters_[inIndex];
121 }
122
123 //=============================================================================
124 // Description:
131 [[nodiscard]] const Cluster<dtype>& at(uint32 inIndex) const
132 {
133 if (inIndex >= clusters_.size())
134 {
135 THROW_INVALID_ARGUMENT_ERROR("index exceeds cluster size.");
136 }
137 return clusters_[inIndex];
138 }
139
140 //=============================================================================
141 // Description:
146 [[nodiscard]] const_iterator begin() const noexcept
147 {
148 return clusters_.cbegin();
149 }
150
151 //=============================================================================
152 // Description:
157 [[nodiscard]] const_iterator end() const noexcept
158 {
159 return clusters_.cend();
160 }
161
162 private:
163 //==================================Attributes=================================
164 const NdArray<bool>* const xcds_{};
165 const NdArray<dtype>* const intensities_{};
166 std::vector<Pixel<dtype>> xcdsVec_{};
167
168 Shape shape_{};
169
170 std::vector<Cluster<dtype>> clusters_{};
171
172 //=============================================================================
173 // Description:
181 Pixel<dtype> makePixel(int32 inRow, int32 inCol) noexcept
182 {
183 // Make sure that on the edges after i've added or subtracted 1 from the row and col that
184 // i haven't gone over the edge
185 const uint32 row = std::min(static_cast<uint32>(std::max<int32>(inRow, 0)), shape_.rows - 1);
186 const uint32 col = std::min(static_cast<uint32>(std::max<int32>(inCol, 0)), shape_.cols - 1);
187 const dtype intensity = intensities_->operator()(row, col);
188
189 return Pixel<dtype>(row, col, intensity);
190 }
191
192 //=============================================================================
193 // Description:
200 void findNeighbors(const Pixel<dtype>& inPixel, std::set<Pixel<dtype>>& outNeighbors)
201 {
202 // using a set will auto take care of adding duplicate pixels on the edges
203
204 // the 8 surrounding neighbors
205 const auto row = static_cast<int32>(inPixel.row);
206 const auto col = static_cast<int32>(inPixel.col);
207
208 outNeighbors.insert(outNeighbors.end(), makePixel(row - 1, col - 1));
209 outNeighbors.insert(outNeighbors.end(), makePixel(row - 1, col));
210 outNeighbors.insert(outNeighbors.end(), makePixel(row - 1, col + 1));
211 outNeighbors.insert(outNeighbors.end(), makePixel(row, col - 1));
212 outNeighbors.insert(outNeighbors.end(), makePixel(row, col + 1));
213 outNeighbors.insert(outNeighbors.end(), makePixel(row + 1, col - 1));
214 outNeighbors.insert(outNeighbors.end(), makePixel(row + 1, col));
215 outNeighbors.insert(outNeighbors.end(), makePixel(row + 1, col + 1));
216 }
217
218 //=============================================================================
219 // Description:
227 void findNeighborNotXcds(const Pixel<dtype>& inPixel, std::vector<Pixel<dtype>>& outNeighbors)
228 {
229 std::set<Pixel<dtype>> neighbors;
230 findNeighbors(inPixel, neighbors);
231
232 // check if the neighboring pixels are exceedances and insert into the xcd vector
233 for (auto& pixel : neighbors)
234 {
235 if (!xcds_->operator()(pixel.row, pixel.col))
236 {
237 outNeighbors.push_back(pixel);
238 }
239 }
240 }
241
242 //=============================================================================
243 // Description:
251 void findNeighborXcds(const Pixel<dtype>& inPixel, std::vector<uint32>& outNeighbors)
252 {
253 std::set<Pixel<dtype>> neighbors;
254 findNeighbors(inPixel, neighbors);
255 std::vector<Pixel<dtype>> neighborXcds;
256
257 // check if the neighboring pixels are exceedances and insert into the xcd vector
258 for (auto& pixel : neighbors)
259 {
260 if (xcds_->operator()(pixel.row, pixel.col))
261 {
262 neighborXcds.push_back(pixel);
263 }
264 }
265
266 // loop through the neighbors and find the cooresponding index into exceedances_
267 for (auto& pixel : neighborXcds)
268 {
269 auto theExceedanceIter = std::find(xcdsVec_.begin(), xcdsVec_.end(), pixel);
270 outNeighbors.push_back(static_cast<uint32>(theExceedanceIter - xcdsVec_.begin()));
271 }
272 }
273
274 //=============================================================================
275 // Description:
278 void runClusterMaker()
279 {
280 uint32 clusterId = 0;
281
282 for (auto& currentPixel : xcdsVec_)
283 {
284 // not already visited
285 if (currentPixel.clusterId == -1)
286 {
287 Cluster<dtype> newCluster(clusterId); // a new cluster
288 currentPixel.clusterId = clusterId;
289 newCluster.addPixel(currentPixel); // assign pixel to cluster
290
291 // get the neighbors
292 std::vector<uint32> neighborIds;
293 findNeighborXcds(currentPixel, neighborIds);
294 if (neighborIds.empty())
295 {
296 clusters_.push_back(newCluster);
297 ++clusterId;
298 continue;
299 }
300
301 // loop through the neighbors
302 for (uint32 neighborsIdx = 0; neighborsIdx < neighborIds.size(); ++neighborsIdx)
303 {
304 Pixel<dtype>& currentNeighborPixel = xcdsVec_[neighborIds[neighborsIdx]];
305
306 // go to neighbors
307 std::vector<uint32> newNeighborIds;
308 findNeighborXcds(currentNeighborPixel, newNeighborIds);
309
310 // loop through the new neighbors and add them to neighbors
311 for (auto newNeighborId : newNeighborIds)
312 {
313 // not already in neighbors
314 if (std::find(neighborIds.begin(), neighborIds.end(), newNeighborId) == neighborIds.end())
315 {
316 neighborIds.push_back(newNeighborId);
317 }
318 }
319
320 // not already assigned to a cluster
321 if (currentNeighborPixel.clusterId == -1)
322 {
323 currentNeighborPixel.clusterId = clusterId;
324 newCluster.addPixel(currentNeighborPixel);
325 }
326 }
327
328 clusters_.push_back(std::move(newCluster));
329 ++clusterId;
330 }
331 }
332 }
333
334 //=============================================================================
335 // Description:
338 void expandClusters()
339 {
340 // loop through the clusters
341 for (auto& theCluster : clusters_)
342 {
343 // loop through the pixels of the cluster
344 for (auto& thePixel : theCluster)
345 {
346 std::vector<Pixel<dtype>> neighborsNotXcds;
347 findNeighborNotXcds(thePixel, neighborsNotXcds);
348
349 // loop through the neighbors and if they haven't already been added to the cluster, add them
350 for (auto& newPixel : neighborsNotXcds)
351 {
352 if (std::find(theCluster.begin(), theCluster.end(), newPixel) == theCluster.end())
353 {
354 theCluster.addPixel(newPixel);
355 }
356 }
357 }
358 }
359 }
360 };
361} // namespace nc::imageProcessing
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition Error.hpp:37
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition StaticAsserts.hpp:39
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition NdArrayCore.hpp:135
Holds the information for a cluster of pixels.
Definition Cluster.hpp:52
ClusterMaker(const NdArray< bool > *const inXcdArrayPtr, const NdArray< dtype > *const inIntensityArrayPtr, uint8 inBorderWidth=0)
Definition ClusterMaker.hpp:65
const_iterator begin() const noexcept
Definition ClusterMaker.hpp:146
const_iterator end() const noexcept
Definition ClusterMaker.hpp:157
typename std::vector< Cluster< dtype > >::const_iterator const_iterator
Definition ClusterMaker.hpp:55
const Cluster< dtype > & at(uint32 inIndex) const
Definition ClusterMaker.hpp:131
const Cluster< dtype > & operator[](uint32 inIndex) const noexcept
Definition ClusterMaker.hpp:118
uint32 size() noexcept
Definition ClusterMaker.hpp:105
Holds the information for a single pixel.
Definition Pixel.hpp:46
Definition applyThreshold.hpp:34
std::uint8_t uint8
Definition Types.hpp:42
std::int32_t int32
Definition Types.hpp:36
std::uint32_t uint32
Definition Types.hpp:40