core/vil/vil_pyramid_image_view.h
Go to the documentation of this file.
00001 #ifndef vil_pyramid_image_view_h_
00002 #define vil_pyramid_image_view_h_
00003 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00004 #pragma interface
00005 #endif
00006 //:
00007 // \file
00008 // \brief Representation of a pyramid hierarchy of image views.
00009 //        The images can be passed to the view (in this case they are any
00010 //        size or scale but sorted based on the scale in the descending order,
00011 //        or can be generated in here (default case).
00012 //        By default: the levels of views are half the size of the previous
00013 //        level and image scales are in the descending order. The biggest image
00014 //        is at level 0 and with scale 1.0 (1,1.0), and the next levels goes like:
00015 //        (1,0.5), (2,0.25) etc..
00016 //
00017 // \author Gamze D. Tunali
00018 // \date   Aug 16, 2010
00019 
00020 #include <vcl_vector.h>
00021 #include <vil/vil_image_view.h>
00022 #include <vil/vil_image_view_base.h>
00023 
00024 template <class T>
00025 class vil_pyramid_image_view
00026 {
00027  public:
00028     //: Default constructor, creates an empty list of pyramid
00029     vil_pyramid_image_view(): nlevels_(0), max_levels_(256) {}
00030 
00031     //: Creates a pyramid with one image only and its scale is set to 1.0 (biggest)
00032     vil_pyramid_image_view(vil_image_view_base_sptr image): nlevels_(1), max_levels_(256)
00033     { images_.push_back(image); scales_.push_back(1.0); }
00034 
00035     //: Creates a pyramid with one image only and its scale is set to 1.0 (biggest)
00036     vil_pyramid_image_view(const vil_image_view<T>& image): nlevels_(1), max_levels_(256)
00037     { images_.push_back(new vil_image_view<T>(image)); scales_.push_back(1.0); }
00038 
00039     //: Creates a pyramid of nlevels and sets the image at the scale 1.0.
00040     // It creates smaller images for the smaller scales. Each image is
00041     // half the size of the previous image
00042     vil_pyramid_image_view(vil_image_view_base_sptr image, unsigned nlevels);
00043 
00044     //: creates a pyramid of empty image views
00045     vil_pyramid_image_view(unsigned levels, unsigned ni, unsigned nj, unsigned n_planes=1);
00046 
00047     //: Creates a pyramid of given image views with associated scales.
00048     // post: sorted in the descending order of the scales
00049     vil_pyramid_image_view(vcl_vector<vil_image_view_base_sptr> const& images,
00050                            vcl_vector<double> const& scales);
00051 
00052     //: Copy constructor.
00053     // The new object will point to the same underlying image as the rhs.
00054     vil_pyramid_image_view(const vil_pyramid_image_view<T>& rhs);
00055 
00056     virtual ~vil_pyramid_image_view() {}
00057 
00058     //: adds a view to the list of view sorted in the right place based on the scale in descending order
00059     void add_view(vil_image_view_base_sptr &image, double scale);
00060 
00061     vil_image_view_base_sptr get_view(unsigned level, double& scale) { scale=scales_[level]; return images_[level];  }
00062 
00063     double scale(unsigned level){return scales_[level];}
00064 
00065     //: The pixel type of the images
00066     typedef T pixel_type;
00067 
00068     void set_max_level(unsigned l) { max_levels_=l; }
00069 
00070     unsigned max_levels() const { return max_levels_; }
00071 
00072     //: Number of pyramid levels
00073     unsigned nlevels() const { return nlevels_; }
00074 
00075     const vil_pyramid_image_view<T>& operator=(const vil_pyramid_image_view<T>& rhs);
00076 
00077     vil_image_view<T>& operator()(unsigned l) { return static_cast<vil_image_view<T>&>(*images_[l]); }
00078 
00079     // iterators
00080     typedef vil_image_view_base_sptr iterator;
00081     inline iterator begin() { return images_[0]; }
00082     inline iterator end  () { return images_[images_.size()-1]; }
00083 
00084  protected:
00085     //: the list of image vieas
00086     vcl_vector<vil_image_view_base_sptr> images_;
00087 
00088     //: the associated scales of images, scales_.size() is always equals to images_.size()
00089     vcl_vector<double> scales_;
00090 
00091     // the number of images in the view, 0 if it is empty
00092     unsigned nlevels_;
00093 
00094     // this is the number of levels that cannot be exceeded, by default it is 256
00095     unsigned max_levels_;
00096 
00097     //: returns true if the image size is < 4x4 or the max_level is reached
00098     inline bool limit_reached(unsigned i, unsigned j)
00099     { return i<2 || j<2 || nlevels_==max_levels_; }
00100 
00101     //: generates an image half the size of the given image and takes the averages
00102     // of pixel values in 4x4 neighborhoods
00103     void scale_down(const vil_image_view<T>&, vil_image_view_base_sptr& image_out);
00104 };
00105 
00106 #endif