Seg3D  2.4
Seg3D is a free volume segmentation and processing tool developed by the NIH Center for Integrative Biomedical Computing at the University of Utah Scientific Computing and Imaging (SCI) Institute.
pyramid.hxx
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2016 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 
29 // File : pyramid.hxx
30 // Author : Pavel A. Koshevoy
31 // Created : 2006/04/04 12:34
32 // Copyright : (C) 2004-2008 University of Utah
33 // Description : Difference of Gaussians multi-scale image pyramid used for
34 // SIFT key and descriptor generation.
35 
36 #ifndef PYRAMID_HXX_
37 #define PYRAMID_HXX_
38 
39 // local includes:
40 #include <Core/ITKCommon/extrema.hxx>
41 #include <Core/ITKCommon/common.hxx>
42 
43 // ITK includes:
44 #include <itkGradientImageFilter.h>
45 
46 #include <boost/filesystem.hpp>
47 
48 // system includes:
49 #include <list>
50 #include <vector>
51 
52 namespace bfs=boost::filesystem;
53 
54 
55 //----------------------------------------------------------------
56 // gradient_filter_t
57 //
58 typedef itk::GradientImageFilter<image_t> gradient_filter_t;
59 
60 //----------------------------------------------------------------
61 // gradient_image_t
62 //
63 typedef gradient_filter_t::OutputImageType gradient_image_t;
64 
65 //----------------------------------------------------------------
66 // gradient_t
67 //
68 typedef gradient_image_t::PixelType gradient_t;
69 
70 
71 //----------------------------------------------------------------
72 // octave_t
73 //
74 // NOTE: the following comments are by Sebastian Nowozin (or are
75 // based on his comments from autopano-2.4 source code):
76 //
77 // Generate DoG maps. The maps are organized like this:
78 // 0: D(s0)
79 // 1: D(k * s0)
80 // 2: D(k^2 * s0)
81 // ...
82 // s: D(k^s * s0) = D(2 * s0)
83 // s+1: D(k^(s+1) * s0) = D(k * 2 * s0)
84 //
85 // So, we can start peak searching at 1 to s, and have a DoG map into
86 // each direction.
87 //
88 // NOTE: Gaussian G(sigma) has the following property:
89 // G(sigma_1) convoled with G(sigma_2) == G(sqrt(sigma_1^2 + sigma_2^2))
90 //
91 // Therefore, we have:
92 //
93 // G(k^{p+1}) == G(k^p) convolved with G(sigma),
94 //
95 // Our goal is to compute every iterations sigma value so this
96 // equation iteratively produces the next level.
97 //
98 // sigma = sqrt{(k^{p+1})^2 - (k^p)^2}
99 // = sqrt{k^{2p+2} - k^{2p}}
100 // = sqrt{k^2p * (k^2 - 1)}
101 // = k^p * sqrt{k^2 - 1}
102 //
103 class octave_t
104 {
105 public:
106  void setup(const unsigned int & octave,
107  const image_t * L0,
108  const mask_t * mask,
109  const double & s0,
110  const double & k,
111  const unsigned int s,
112  const bool & make_keys);
113 
114  // number of scales in this octave:
115  inline unsigned int scales() const
116  { return gL_.size(); }
117 
118  // for each scale, find the minima and maxima points of the DoF images:
119  void detect_extrema(const unsigned int & pyramid,
120  const unsigned int & octave,
121  const double percent_below_threshold,
122  const bool threshold_by_area,
123  const bfs::path & fn_prefix = "");
124 
125  // NOTE: this operates on the results produced by detect_extrema:
126  void generate_keys();
127 
128  // NOTE: this operates on the results produced by generate_keys:
129  void generate_descriptors(const unsigned int & descriptor_version);
130 
131  // count the number of extrema points detected within this octave:
132  unsigned int count_extrema() const;
133 
134  // count the number of keys detected within this octave:
135  unsigned int count_keys() const;
136 
137  // image mask (eroded to step away from the mosaic boundaries):
138  mask_t::ConstPointer mask_;
139  mask_t::ConstPointer mask_eroded_;
140 
141  // smoothed images:
142  std::vector<image_t::Pointer> L_;
143 
144  // DoG images, D[i] = L[i + 1] - L[i]:
145  std::vector<image_t::Pointer> D_;
146 
147  // gradient vectors (in polar coordinates):
148  std::vector<gradient_image_t::Pointer> gL_;
149 
150  // FIXME: minima/maxima images:
151  std::vector<image_t::Pointer> raw_min_;
152  std::vector<image_t::Pointer> raw_max_;
153 
154  // sigma[i] -> sigma used to generate L[i]:
155  std::vector<double> sigma_;
156 
157  // extrema:
158  std::vector<std::list<extrema_t> > extrema_min_;
159  std::vector<std::list<extrema_t> > extrema_max_;
160 
161  // keys:
162  std::vector<std::list<descriptor_t> > keys_min_;
163  std::vector<std::list<descriptor_t> > keys_max_;
164 
165  // min/max sampling window radius:
166  static const double r0_;
167  static const double r1_;
168 };
169 
170 //----------------------------------------------------------------
171 // pyramid_t
172 //
174 {
175 public:
176  void setup(const image_t * initial_image,
177  const mask_t * mask,
178  const double & initial_sigma,
179  const unsigned int s,
180  const double min_edge = 96.0,
181  const bool & make_keys = true);
182 
183  // number of octaves in this pyramid:
184  inline unsigned int octaves() const
185  { return octave_.size(); }
186 
187  // helper functions:
188  bool remove_lowest_resolution_octave(const unsigned int how_many = 1);
189  bool remove_highest_resolution_octave(const unsigned int how_many = 1);
190 
191  // find the extrema points within each octave:
192  void detect_extrema(const unsigned int & pyramid,
193  const double percent_below_threshold,
194  const bool threshold_by_area = true,
195  const bfs::path & fn_prefix = "");
196 
197  // NOTE: this operates on the results produced by detect_extrema:
198  void generate_keys();
199 
200  // NOTE: this operates on the results produced by generate_keys:
201  void generate_descriptors(const unsigned int & descriptor_version);
202 
203  // count the number of extrema points detected within this pyramid:
204  unsigned int count_extrema() const;
205 
206  // count the number of keys detected within this pyramid:
207  unsigned int count_keys() const;
208 
209  // save a bunch of images with the keys marked on them in color:
210  void debug(const bfs::path & fn_prefix) const;
211 
212  // use this to speed up pyramid setup:
213  bool save(const bfs::path & fn_save) const;
214  bool load(const bfs::path & fn_save);
215 
216  // image pyramid octaves:
217  std::vector<octave_t> octave_;
218 
219  // file name of the image that originated this pyramid:
220  bfs::path fn_data_;
221 };
222 
223 //----------------------------------------------------------------
224 // load_pyramid
225 //
226 extern void
227 load_pyramid(const bfs::path & fn_load,
228  pyramid_t & pyramid,
229  image_t::Pointer & mosaic,
230  mask_t::Pointer & mosaic_mask);
231 
232 
233 #endif // PYRAMID_HXX_
Definition: pyramid.hxx:103
Definition: pyramid.hxx:173