40 #include <Core/ITKCommon/common.hxx>
41 #include <Core/ITKCommon/the_dynamic_array.hxx>
42 #include <Core/ITKCommon/the_utils.hxx>
44 #include <Core/Utils/Exception.h>
58 typedef std::list<image_t::IndexType> cluster_t;
66 template <
unsigned int d>
75 inline bool operator < (const centerofmass_t<d> & cm)
const
76 {
return mass_ < cm.mass_; }
79 {
return mass_ > cm.mass_; }
82 inline const double & operator [] (
const unsigned int & index)
const
83 {
return cm_[index]; }
85 inline double & operator [] (
const unsigned int & index)
86 {
return cm_[index]; }
89 itk::Point<double, d> cm_;
103 identify_clusters_cm(
const double * data,
104 const unsigned int & size,
106 const bool periodic =
true);
115 identify_clusters_cm(
const image_t * image,
125 template <
class image_t>
127 assemble_clusters(
const image_t * image,
129 typename image_t::PixelType background,
130 const unsigned int connectivity,
134 typedef typename image_t::RegionType region_t;
135 typedef typename image_t::PixelType pixel_t;
136 typedef typename image_t::IndexType index_t;
137 typedef std::list<index_t> cluster_t;
140 if (connectivity > 8)
142 CORE_THROW_EXCEPTION(
"cluster connectivity > 8");
146 static int stencil[][2] = {
160 mask_t::RegionType::SizeType mask_size =
161 image->GetLargestPossibleRegion().GetSize();
162 unsigned int spacing_scale = 1;
166 mask_size = mask->GetLargestPossibleRegion().GetSize();
168 static_cast<unsigned int>(image->GetSpacing()[0] / mask->GetSpacing()[0]);
171 region_t rn = image->GetLargestPossibleRegion();
172 typename region_t::SizeType sz = rn.GetSize();
174 typedef itk::Image<unsigned int> cluster_map_t;
175 cluster_map_t::Pointer cluster_map = make_image<cluster_map_t>(sz, ~0u);
177 typedef itk::ImageRegionConstIteratorWithIndex<image_t> itex_t;
178 itex_t itex(image, rn);
180 for (itex.GoToBegin(); !itex.IsAtEnd(); ++itex)
182 const pixel_t v = itex.Get();
185 if (v <= background)
continue;
187 const index_t index = itex.GetIndex();
190 if (!pixel_in_mask<image_t>(mask, mask_size, index, spacing_scale))
196 std::list<unsigned int> neighbors;
198 for (
unsigned int k = 0; k < connectivity; k++)
201 uv[0] += stencil[k][0];
202 uv[1] += stencil[k][1];
203 if (!pixel_in_mask<image_t>(mask, mask_size, uv, spacing_scale))
210 if (uv[0] < 0 || image_size_value_t(uv[0]) > sz[0] ||
211 uv[1] < 0 || image_size_value_t(uv[1]) > sz[1])
213 CORE_THROW_EXCEPTION(
"cluster ids of neighbors out of bounds");
216 unsigned int cluster_id = cluster_map->GetPixel(uv);
217 if (cluster_id != ~0u)
219 push_back_unique(neighbors, cluster_id);
223 if (neighbors.empty())
226 clusters.append(cluster_t());
228 unsigned int id = clusters.end_index(
true);
229 cluster_map->SetPixel(index,
id);
230 clusters[id].push_back(index);
235 unsigned int id = neighbors.front();
236 cluster_map->SetPixel(index,
id);
237 clusters[id].push_back(index);
239 if (neighbors.size() > 1)
242 for (std::list<unsigned int>::iterator bi = ++(neighbors.begin());
243 bi != neighbors.end(); ++bi)
245 unsigned int old_id = *bi;
247 while (!clusters[old_id].empty())
249 index_t uv = remove_head(clusters[old_id]);
251 cluster_map->SetPixel(uv,
id);
252 clusters[id].push_back(uv);
261 #endif // CLUSTER_HXX_
Definition: cluster.hxx:67
Definition: the_dynamic_array.hxx:45