67 uint8 inBorderWidth = 0) :
69 intensities_(inIntensityArrayPtr)
71 if (xcds_->shape() != intensities_->shape())
73 THROW_INVALID_ARGUMENT_ERROR(
"input xcd and intensity arrays must be the same shape.");
76 shape_ = xcds_->shape();
79 for (
uint32 row = 0; row < shape_.rows; ++row)
81 for (
uint32 col = 0; col < shape_.cols; ++col)
83 if (xcds_->operator()(row, col))
85 const Pixel<dtype> thePixel(row, col, intensities_->operator()(row, col));
86 xcdsVec_.push_back(thePixel);
93 for (
uint8 i = 0; i < inBorderWidth; ++i)
107 return static_cast<uint32>(clusters_.size());
120 return clusters_[inIndex];
133 if (inIndex >= clusters_.size())
137 return clusters_[inIndex];
148 return clusters_.cbegin();
159 return clusters_.cend();
166 std::vector<Pixel<dtype>> xcdsVec_{};
170 std::vector<Cluster<dtype>> clusters_{};
181 Pixel<dtype> makePixel(
int32 inRow,
int32 inCol)
noexcept
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);
189 return Pixel<dtype>(row, col, intensity);
200 void findNeighbors(
const Pixel<dtype>& inPixel, std::set<Pixel<dtype>>& outNeighbors)
205 const auto row =
static_cast<int32>(inPixel.row);
206 const auto col =
static_cast<int32>(inPixel.col);
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));
227 void findNeighborNotXcds(
const Pixel<dtype>& inPixel, std::vector<Pixel<dtype>>& outNeighbors)
229 std::set<Pixel<dtype>> neighbors;
230 findNeighbors(inPixel, neighbors);
233 for (
auto& pixel : neighbors)
235 if (!xcds_->operator()(pixel.row, pixel.col))
237 outNeighbors.push_back(pixel);
251 void findNeighborXcds(
const Pixel<dtype>& inPixel, std::vector<uint32>& outNeighbors)
253 std::set<Pixel<dtype>> neighbors;
254 findNeighbors(inPixel, neighbors);
255 std::vector<Pixel<dtype>> neighborXcds;
258 for (
auto& pixel : neighbors)
260 if (xcds_->operator()(pixel.row, pixel.col))
262 neighborXcds.push_back(pixel);
267 for (
auto& pixel : neighborXcds)
269 auto theExceedanceIter = std::find(xcdsVec_.begin(), xcdsVec_.end(), pixel);
270 outNeighbors.push_back(
static_cast<uint32>(theExceedanceIter - xcdsVec_.begin()));
278 void runClusterMaker()
282 for (
auto& currentPixel : xcdsVec_)
285 if (currentPixel.clusterId == -1)
287 Cluster<dtype> newCluster(clusterId);
288 currentPixel.clusterId = clusterId;
289 newCluster.addPixel(currentPixel);
292 std::vector<uint32> neighborIds;
293 findNeighborXcds(currentPixel, neighborIds);
294 if (neighborIds.empty())
296 clusters_.push_back(newCluster);
302 for (
uint32 neighborsIdx = 0; neighborsIdx < neighborIds.size(); ++neighborsIdx)
304 Pixel<dtype>& currentNeighborPixel = xcdsVec_[neighborIds[neighborsIdx]];
307 std::vector<uint32> newNeighborIds;
308 findNeighborXcds(currentNeighborPixel, newNeighborIds);
311 for (
auto newNeighborId : newNeighborIds)
314 if (std::find(neighborIds.begin(), neighborIds.end(), newNeighborId) == neighborIds.end())
316 neighborIds.push_back(newNeighborId);
321 if (currentNeighborPixel.clusterId == -1)
323 currentNeighborPixel.clusterId = clusterId;
324 newCluster.addPixel(currentNeighborPixel);
328 clusters_.push_back(std::move(newCluster));
338 void expandClusters()
341 for (
auto& theCluster : clusters_)
344 for (
auto& thePixel : theCluster)
346 std::vector<Pixel<dtype>> neighborsNotXcds;
347 findNeighborNotXcds(thePixel, neighborsNotXcds);
350 for (
auto& newPixel : neighborsNotXcds)
352 if (std::find(theCluster.begin(), theCluster.end(), newPixel) == theCluster.end())
354 theCluster.addPixel(newPixel);