1 #ifndef vil_pyramid_image_view_h_
2 #define vil_pyramid_image_view_h_
3 //:
4 // \file
5 // \brief Representation of a pyramid hierarchy of image views.
6 //        The images can be passed to the view (in this case they are any
7 //        size or scale but sorted based on the scale in the descending order,
8 //        or can be generated in here (default case).
9 //        By default: the levels of views are half the size of the previous
10 //        level and image scales are in the descending order. The biggest image
11 //        is at level 0 and with scale 1.0 (1,1.0), and the next levels goes like:
12 //        (1,0.5), (2,0.25) etc..
13 //
14 // \author Gamze D. Tunali
15 // \date   Aug 16, 2010
16 
17 #include <vector>
18 #ifdef _MSC_VER
19 #  include <vcl_msvc_warnings.h>
20 #endif
21 #include "vil_image_view.h"
22 #include "vil_image_view_base.h"
23 
24 template <class T>
25 class vil_pyramid_image_view
26 {
27  public:
28     //: Default constructor, creates an empty list of pyramid
29    vil_pyramid_image_view() = default;
30 
31    //: Creates a pyramid with one image only and its scale is set to 1.0
32    //(biggest)
vil_pyramid_image_view(vil_image_view_base_sptr image)33    vil_pyramid_image_view(vil_image_view_base_sptr image)
34        : nlevels_(1), max_levels_(256) {
35      images_.push_back(image);
36      scales_.push_back(1.0); }
37 
38     //: Creates a pyramid with one image only and its scale is set to 1.0 (biggest)
vil_pyramid_image_view(const vil_image_view<T> & image)39     vil_pyramid_image_view(const vil_image_view<T>& image): nlevels_(1), max_levels_(256)
40     { images_.push_back(new vil_image_view<T>(image)); scales_.push_back(1.0); }
41 
42     //: Creates a pyramid of nlevels and sets the image at the scale 1.0.
43     // It creates smaller images for the smaller scales. Each image is
44     // half the size of the previous image
45     vil_pyramid_image_view(vil_image_view_base_sptr image, unsigned nlevels);
46 
47     //: creates a pyramid of empty image views
48     vil_pyramid_image_view(unsigned levels, unsigned ni, unsigned nj, unsigned n_planes=1);
49 
50     //: Creates a pyramid of given image views with associated scales.
51     // post: sorted in the descending order of the scales
52     vil_pyramid_image_view(std::vector<vil_image_view_base_sptr> const& images,
53                            std::vector<double> const& scales);
54 
55     //: Copy constructor.
56     // The new object will point to the same underlying image as the rhs.
57     vil_pyramid_image_view(const vil_pyramid_image_view<T>& rhs);
58 
59     virtual ~vil_pyramid_image_view() = default;
60 
61     //: adds a view to the list of view sorted in the right place based on the scale in descending order
62     void add_view(vil_image_view_base_sptr &image, double scale);
63 
get_view(unsigned level,double & scale)64     vil_image_view_base_sptr get_view(unsigned level, double& scale) { scale=scales_[level]; return images_[level];  }
65 
scale(unsigned level)66     double scale(unsigned level){return scales_[level];}
67 
68     //: The pixel type of the images
69     typedef T pixel_type;
70 
set_max_level(unsigned l)71     void set_max_level(unsigned l) { max_levels_=l; }
72 
max_levels()73     unsigned max_levels() const { return max_levels_; }
74 
75     //: Number of pyramid levels
nlevels()76     unsigned nlevels() const { return nlevels_; }
77 
78     const vil_pyramid_image_view<T>& operator=(const vil_pyramid_image_view<T>& rhs);
79 
operator()80     vil_image_view<T>& operator()(unsigned l) { return static_cast<vil_image_view<T>&>(*images_[l]); }
81 
82     // iterators
83     typedef vil_image_view_base_sptr iterator;
begin()84     inline iterator begin() { return images_[0]; }
end()85     inline iterator end  () { return images_[images_.size()-1]; }
86 
87  protected:
88     //: the list of image vieas
89     std::vector<vil_image_view_base_sptr> images_;
90 
91     //: the associated scales of images, scales_.size() is always equals to images_.size()
92     std::vector<double> scales_;
93 
94     // the number of images in the view, 0 if it is empty
95     unsigned nlevels_{0};
96 
97     // this is the number of levels that cannot be exceeded, by default it is 256
98     unsigned max_levels_{256};
99 
100     //: returns true if the image size is < 4x4 or the max_level is reached
limit_reached(unsigned i,unsigned j)101     inline bool limit_reached(unsigned i, unsigned j)
102     { return i<2 || j<2 || nlevels_==max_levels_; }
103 
104     //: generates an image half the size of the given image and takes the averages
105     // of pixel values in 4x4 neighborhoods
106     void scale_down(const vil_image_view<T>&, vil_image_view_base_sptr& image_out);
107 };
108 
109 #endif
110