35 #ifndef STOS_COMMON_HXX_
36 #define STOS_COMMON_HXX_
39 #include <Core/ITKCommon/STOS/stos.hxx>
40 #include <Core/ITKCommon/common.hxx>
41 #include <Core/ITKCommon/the_text.hxx>
44 #include <boost/shared_ptr.hpp>
45 #include <boost/filesystem.hpp>
46 #include <boost/algorithm/string/predicate.hpp>
47 namespace bfs=boost::filesystem;
64 template <
typename TImage>
68 if (stos.empty())
return true;
78 std::list<stos_t<TImage> > sorted;
79 sorted.push_back(remove_head(stos));
84 bool found_head_match =
false;
85 for (
typename std::list<
stos_t<TImage> >::iterator i = stos.begin();
88 const bfs::path & head = sorted.front().fn_[0];
89 const bfs::path & tail = (*i).fn_[1];
92 found_head_match =
true;
93 sorted.push_front(*i);
103 bool found_tail_match =
false;
104 for (
typename std::list<
stos_t<TImage> >::iterator i = stos.begin();
107 const bfs::path & tail = sorted.back().fn_[1];
108 const bfs::path & head = (*i).fn_[0];
111 found_tail_match =
true;
112 sorted.push_back(*i);
121 if (!found_head_match && !found_tail_match)
128 if (!stos.empty())
return false;
130 stos.splice(stos.end(), sorted);
138 template <
typename T>
140 load_mosaic(
const bfs::path & fn_mosaic,
141 const bool & flip_mosaic,
142 const unsigned int & shrink_factor,
143 const bool & assemble_mosaic,
144 const bfs::path & image_path,
145 typename T::Pointer & mosaic,
146 const bool & assemble_mosaic_mask,
147 mask_t::Pointer & mosaic_mask,
148 const double & clahe_slope = 1.0,
149 const unsigned int & clahe_window = 64,
150 typename T::PixelType clahe_new_min = 0,
151 typename T::PixelType clahe_new_max = 255,
152 const bool & dont_allocate =
false)
157 fin.open(fn_mosaic.c_str(), std::ios::in);
165 std::list<bfs::path> fn_tiles;
166 std::vector<base_transform_t::Pointer> transform;
167 std::vector<typename T::ConstPointer> image;
168 std::vector<mask_t::ConstPointer> mask;
170 double pixel_spacing = 1.0;
171 bool use_std_mask =
false;
172 load_mosaic<base_transform_t>(fin,
180 unsigned int num_images = transform.size();
181 assert(pixel_spacing != 0);
183 image.resize(num_images);
184 mask.resize(num_images);
187 bfs::path mosaic_dir = fn_mosaic.parent_path();
190 for (std::list<bfs::path>::const_iterator iter = fn_tiles.begin();
191 iter != fn_tiles.end(); ++iter, i++)
193 const bfs::path & fn_image = *iter;
197 image[i] = std_tile<T>(fn_image, shrink_factor, pixel_spacing);
198 mask[i] = std_mask<T>(image[i], use_std_mask);
204 std::cout <<
" assembling a mosaic " << std::endl;
205 mosaic = make_mosaic<typename T::ConstPointer, base_transform_t::Pointer>
206 (FEATHER_BLEND_E, transform, image, mask, dont_allocate);
209 typename T::PointType origin = mosaic->GetOrigin();
212 mosaic->SetOrigin(origin);
214 if (flip_mosaic && !dont_allocate)
216 mosaic = flip<T>(mosaic);
220 if (assemble_mosaic_mask)
222 std::cout <<
" assembling a mosaic mask " << std::endl;
223 mosaic_mask = make_mask<base_transform_t::Pointer>(transform, mask);
226 mask_t::PointType origin = mosaic_mask->GetOrigin();
229 mosaic_mask->SetOrigin(origin);
233 mosaic_mask = flip<mask_t>(mosaic_mask);
239 if (assemble_mosaic && !dont_allocate && clahe_slope > 1.0)
241 std::cout <<
" enhancing contrast with CLAHE " << std::endl;
242 mosaic = CLAHE<T>(mosaic,
260 template <
typename T>
262 load_mosaic(
const bfs::path & fn_mosaic,
263 const bool & flip_mosaic,
264 const unsigned int & shrink_factor,
265 const double & clahe_slope,
266 const bfs::path & image_path,
267 typename T::Pointer & mosaic,
268 mask_t::Pointer & mosaic_mask,
269 bool mosaic_mask_enabled =
true,
270 bool dont_allocate =
false,
271 typename T::PixelType clahe_new_min = 0,
272 typename T::PixelType clahe_new_max = 255)
274 return load_mosaic<T>(fn_mosaic,
293 load_slice(
const bfs::path & fn_mosaic,
295 const unsigned int & shrink_factor,
296 const double & clahe_slope,
297 const bfs::path & image_path,
298 image_t::Pointer & mosaic,
299 mask_t::Pointer & mosaic_mask,
300 bool mosaic_mask_enabled =
true,
301 bool dont_allocate =
false)
303 return load_mosaic<image_t>(fn_mosaic,
320 load_slice(
const bfs::path & fn_mosaic,
322 const unsigned int & shrink_factor,
323 const double & clahe_slope,
324 const bfs::path & image_path,
325 image_t::Pointer & mosaic,
326 bool dont_allocate =
false)
328 mask_t::Pointer dummy;
329 return load_slice(fn_mosaic,
344 template <
typename TImage>
359 link_t(
const boost::shared_ptr<node_t> & a,
360 const boost::shared_ptr<node_t> & b,
367 inline bool operator < (
const link_t & l)
const
369 return (stos_.fn_[1] < l.stos_.fn_[1]);
372 boost::shared_ptr<node_t> a_;
373 boost::shared_ptr<node_t> b_;
382 node_t(
const bfs::path & fn_data,
383 const bfs::path & image_dir,
384 const bfs::path & fn_mask,
386 const vec2d_t & spacing):
388 image_dir_(image_dir),
396 load_data(
typename TImage::Pointer & data,
397 mask_t::Pointer & mask,
398 unsigned int shrink_factor = 1,
399 double clahe_slope = 1,
400 unsigned int clahe_window = 64)
const
402 if (fn_data_.extension() ==
".mosaic")
404 load_mosaic<TImage>(fn_data_,
417 data = std_tile<TImage>(fn_data_,
419 std::max(spacing_[0], spacing_[1]));
421 if (fn_mask_.empty())
423 mask = cast<TImage, mask_t>(data);
428 mask = std_tile<mask_t>(fn_mask_,
430 std::max(spacing_[0], spacing_[1]));
438 calc_overlap_mask(
unsigned int shrink_factor = 1,
439 double clahe_slope = 1,
440 unsigned int clahe_window = 64)
const
443 typename TImage::Pointer data;
444 mask_t::Pointer mask;
445 load_data(data, mask, shrink_factor, clahe_slope, clahe_window);
451 for (
typename std::list<link_t>::const_iterator
452 i = children_.begin(); i != children_.end(); ++i)
457 mask_t::Pointer imask = link.b_->calc_overlap_mask(shrink_factor,
463 if (!link.b_->children_.empty())
466 base_transform_t::ConstPointer t = link.stos_.t01_.GetPointer();
468 if ( mask != mask_t::Pointer(NULL) && imask != mask_t::Pointer(NULL) )
469 clip_mask_a_by_b(mask, imask, t);
478 static void clip_mask_a_by_b(mask_t::Pointer & mask_a,
479 mask_t::Pointer & mask_b,
480 const base_transform_t::ConstPointer & t_ab)
485 typedef itk::LinearInterpolateImageFunction<mask_t, double> imask_t;
486 imask_t::Pointer imask_b = imask_t::New();
487 imask_b->SetInputImage(mask_b);
493 const base_transform_t * t = t_ab.GetPointer();
495 typedef typename TImage::RegionType rn_t;
496 typedef typename TImage::RegionType::SizeType sz_t;
497 typedef typename TImage::RegionType::IndexType ix_t;
499 rn_t region = mask_a->GetLargestPossibleRegion();
500 ix_t origin = region.GetIndex();
501 sz_t extent = region.GetSize();
503 typename ix_t::IndexValueType x_end = origin[0] + extent[0];
504 typename ix_t::IndexValueType y_end = origin[1] + extent[1];
507 for (ix[1] = origin[1]; ix[1] < y_end; ++ix[1])
509 for (ix[0] = origin[0]; ix[0] < x_end; ++ix[0])
511 if (mask_a->GetPixel(ix) == 0)
516 mask_a->TransformIndexToPhysicalPoint(ix, pt_a);
517 pt_b = t->TransformPoint(pt_a);
519 if (!imask_b->IsInsideBuffer(pt_b))
521 mask_a->SetPixel(ix, 0);
525 double val_mask_b = imask_b->Evaluate(pt_b);
526 if (val_mask_b < 0.5)
528 mask_a->SetPixel(ix, 0);
538 for (
typename std::list<link_t>::const_iterator
539 i = children_.begin(); i != children_.end(); ++i)
542 stos.push_back(link.stos_);
546 for (
typename std::list<link_t>::const_iterator
547 i = children_.begin(); i != children_.end(); ++i)
550 link.b_->collect_stos(stos);
555 dump(std::ostream & os)
557 for (
typename std::list<link_t>::const_iterator
558 i = children_.begin(); i != children_.end(); ++i)
561 os << fn_data_ <<
':' << link.b_->fn_data_
562 <<
", " << link.stos_.fn_load_
567 for (
typename std::list<link_t>::const_iterator
568 i = children_.begin(); i != children_.end(); ++i)
577 bfs::path image_dir_;
584 std::list<link_t> children_;
591 boost::shared_ptr<node_t>
592 lookup(
const bfs::path & fn_data)
const
594 for (
typename std::list<boost::shared_ptr<node_t> >::const_iterator
595 i = nodes_.begin(); i != nodes_.end(); ++i)
597 const boost::shared_ptr<node_t> & node = *i;
599 if ( boost::iequals(node->fn_data_.string(), fn_data.string()) )
605 return boost::shared_ptr<node_t>();
612 boost::shared_ptr<node_t>
613 get_root(
unsigned int root_index = 0)
615 unsigned int num_roots = 0;
616 for (
typename std::list<boost::shared_ptr<node_t> >::iterator
617 i = nodes_.begin(); i != nodes_.end(); ++i)
619 const typename boost::shared_ptr<node_t> & node = *i;
620 if (!node->parent_.a_)
622 if (num_roots == root_index)
631 return boost::shared_ptr<node_t>();
647 i = stos_list.begin(); i != stos_list.end(); ++i)
651 boost::shared_ptr<node_t> a = lookup(stos.fn_[0]);
652 boost::shared_ptr<node_t> b = lookup(stos.fn_[1]);
656 a = boost::shared_ptr<node_t>(
new node_t(stos.fn_[0],
666 b = boost::shared_ptr<node_t>(
new node_t(stos.fn_[1],
674 boost::shared_ptr<node_t> prev_parent = b->parent_.a_;
675 b->parent_ = link_t(a, b, stos);
676 a->children_.push_back(link_t(a, b, stos));
687 std::size_t num_roots = 0;
688 for (
typename std::list<boost::shared_ptr<node_t> >::iterator
689 i = nodes_.begin(); i != nodes_.end(); ++i)
691 const boost::shared_ptr<node_t> & node = *i;
692 if (!node->parent_.a_)
705 get_cascade(
const bfs::path & src,
706 const bfs::path & dst,
707 std::vector<base_transform_t::Pointer> & cascade)
716 boost::shared_ptr<node_t> node = lookup(dst);
722 std::list<base_transform_t::Pointer> transforms;
723 while (node->parent_.a_)
725 base_transform_t::Pointer t =
726 const_cast<base_transform_t *
>(node->parent_.stos_.t01_.GetPointer());
727 transforms.push_front(t);
729 if ( boost::iequals(node->parent_.a_->fn_data_.string(), src.string()) )
731 cascade.assign(transforms.begin(), transforms.end());
735 node = node->parent_.a_;
746 for (
typename std::list<boost::shared_ptr<node_t> >::iterator
747 i = nodes_.begin(); i != nodes_.end(); ++i)
749 const typename boost::shared_ptr<node_t> & node = *i;
750 if (!node->parent_.a_)
752 node->collect_stos(stos);
758 dump(std::ostream & os)
760 std::size_t root_count = 0;
761 for (
typename std::list<boost::shared_ptr<node_t> >::iterator
762 i = nodes_.begin(); i != nodes_.end(); ++i)
764 const typename boost::shared_ptr<node_t> & node = *i;
765 if (!node->parent_.a_)
768 os << root_count <<
". --------------------------------------------\n";
776 std::list<boost::shared_ptr<node_t> > nodes_;
780 #endif // STOS_COMMON_HXX_
Definition: stos_common.hxx:345
Definition: stos_common.hxx:380
Definition: stos_common.hxx:354