core/vil/vil_pyramid_image_view.txx
Go to the documentation of this file.
00001 #ifndef vil_pyramid_image_view_txx_
00002 #define vil_pyramid_image_view_txx_
00003 
00004 #include "vil_pyramid_image_view.h"
00005 #include <vil/vil_image_view.h>
00006 #include <vil/vil_resample_bilin.h>
00007 
00008 template <class T>
00009 vil_pyramid_image_view<T>::vil_pyramid_image_view(unsigned levels, unsigned ni,
00010                                                   unsigned nj, unsigned n_planes)
00011   : nlevels_(levels), max_levels_(256)
00012 {
00013   images_.resize(levels);
00014   scales_.resize(levels);
00015   unsigned i=ni, j=nj;
00016   double scale=1.0;
00017   for (unsigned l=0; l<levels && !limit_reached(i,j); l++) {
00018     images_[l] = new vil_image_view<T>(i, j, n_planes);
00019     scales_[l] = scale;
00020     i/=2;j/=2;
00021     scale/=2.0;
00022   }
00023 }
00024 
00025 template <class T>
00026 vil_pyramid_image_view<T>::vil_pyramid_image_view(vil_image_view_base_sptr image, unsigned levels)
00027 : nlevels_(levels), max_levels_(256)
00028 {
00029   vil_image_view<T>* img=dynamic_cast<vil_image_view<T>*>(image.ptr());
00030   if (!img)
00031     return;
00032 
00033   // it works with grey scale images
00034   if (img->nplanes() != 1)
00035     return;
00036 
00037   images_.resize(levels);
00038   scales_.resize(levels);
00039   unsigned i=image->ni()/2, j=image->nj()/2;
00040   double scale=1.0;
00041   images_[0] = image;
00042   scales_[0] = 1.0;
00043 
00044   for (unsigned l=1; l<levels && !limit_reached(i,j); l++) {
00045     // scale down the image
00046     scale/=2.0;
00047     img = static_cast<vil_image_view<T>*>(images_[l-1].ptr());
00048     vil_image_view_base_sptr new_img=0;
00049     scale_down(*img, new_img);
00050     images_[l] = new_img;
00051     scales_[l] = scale;
00052     i/=2;j/=2;
00053   }
00054 }
00055 
00056 template <class T>
00057 vil_pyramid_image_view<T>::vil_pyramid_image_view(vcl_vector<vil_image_view_base_sptr> const& images,
00058                                                   vcl_vector<double> const& scales)
00059 {
00060   nlevels_=(unsigned int)(images.size());
00061   images_.resize(nlevels_);
00062   scales_.resize(nlevels_);
00063   for (unsigned l=0; l<nlevels_; l++) {
00064     images_[l] = images[l];
00065     scales_[l] = scales[l];
00066   }
00067 }
00068 
00069 template <class T>
00070 vil_pyramid_image_view<T>::vil_pyramid_image_view(const vil_pyramid_image_view<T>& rhs)
00071 {
00072   this->images_.resize(rhs.nlevels());
00073   this->nlevels_ = rhs.nlevels();
00074   this->images_ = rhs.images_;
00075   this->scales_ = rhs.scales_;
00076 }
00077 
00078 template <class T>
00079 void vil_pyramid_image_view<T>::add_view(vil_image_view_base_sptr &image, double scale)
00080 {
00081   // find the place of the image based on the ordered scales
00082   unsigned i=0;
00083   vcl_vector<vil_image_view_base_sptr>::iterator image_iter=images_.begin();
00084   vcl_vector<double>::iterator scale_iter= scales_.begin();
00085   while (i<nlevels_ && scale<scales_[i]) {
00086     i++;
00087     image_iter++;
00088     scale_iter++;
00089   }
00090 
00091   // insert into the vector
00092   images_.insert(image_iter,image);
00093   scales_.insert(scale_iter,scale);
00094   nlevels_++;
00095 }
00096 
00097 template <class T>
00098 const vil_pyramid_image_view<T>&
00099 vil_pyramid_image_view<T>::operator=(const vil_pyramid_image_view<T>& rhs)
00100 {
00101   this->images_.resize(rhs.nlevels());
00102   this->max_levels_=rhs.max_levels();
00103   this->nlevels_ = rhs.nlevels();
00104   this->images_ = rhs.images_;
00105   this->scales_ = rhs.scales_;
00106   return *this;
00107 }
00108 
00109 template <class T>
00110 void vil_pyramid_image_view<T>::scale_down(const vil_image_view<T>& image_in,
00111                                            vil_image_view_base_sptr& image_out)
00112 {
00113   unsigned ni=image_in.ni();
00114   unsigned nj=image_in.nj();
00115   unsigned ni2=ni/2;
00116   unsigned nj2=nj/2;
00117   vil_image_view<T>* half_size = new vil_image_view<T>(ni2, nj2);
00118   vil_resample_bilin(image_in, *half_size, ni2, nj2);
00119   image_out = half_size;
00120 }
00121 
00122 
00123 #define VIL_PYRAMID_IMAGE_VIEW_INSTANTIATE(T) \
00124 template class vil_pyramid_image_view<T >
00125 
00126 #endif