contrib/mul/msdi/msdi_reflected_marked_images.cxx
Go to the documentation of this file.
00001 
00002 //:
00003 // \file
00004 // \author Tim Cootes
00005 // \brief Adaptor which generates reflected versions of images/points
00006 
00007 #include <vcl_cassert.h>
00008 #include <vcl_string.h>
00009 
00010 #include "msdi_reflected_marked_images.h"
00011 #include <vimt/vimt_image_2d_of.h>
00012 #include <vil/vil_flip.h>
00013 #include <msm/msm_reflect_shape.h>
00014 
00015 //: Construct with external vectors of images and points
00016 msdi_reflected_marked_images::msdi_reflected_marked_images(
00017                         msdi_marked_images& raw_data,
00018                         const vcl_vector<unsigned>& sym_pts,
00019                         bool only_reflect)
00020   : marked_images_(raw_data),
00021     sym_pts_(sym_pts),
00022     only_reflect_(only_reflect)
00023 {
00024   pyr_builder_.set_min_size(24,24);
00025   reset();
00026 }
00027 
00028 //=======================================================================
00029 // Destructor
00030 //=======================================================================
00031 
00032 msdi_reflected_marked_images::~msdi_reflected_marked_images()
00033 {
00034 }
00035 
00036 unsigned msdi_reflected_marked_images::size() const
00037 {
00038   if (only_reflect_) return marked_images_.size();
00039 
00040   return marked_images_.size()*2;
00041 }
00042 
00043 //: Return current image
00044 const vimt_image_2d& msdi_reflected_marked_images::image()
00045 {
00046   if (!image_ok_) get_image();
00047   return image_;
00048 }
00049 
00050 
00051   //: Return current image pyramid
00052 const vimt_image_pyramid& msdi_reflected_marked_images::image_pyr()
00053 {
00054   if (!image_ok_) get_image();
00055   if (!image_pyr_ok_)
00056   {
00057     pyr_builder_.build(image_pyr_,image_);
00058     image_pyr_ok_=true;
00059   }
00060 
00061   return image_pyr_;
00062 }
00063 
00064 const msm_points& msdi_reflected_marked_images::points()
00065 {
00066   if (!points_ok_) get_points();
00067   return points_;
00068 }
00069 
00070 //=======================================================================
00071 //: Set to first example
00072 //=======================================================================
00073 void msdi_reflected_marked_images::reset()
00074 {
00075   marked_images_.reset();
00076   first_pass_=true;
00077   points_ok_=false;
00078   image_ok_=false;
00079   image_pyr_ok_=false;
00080 }
00081 
00082 //=======================================================================
00083 //: Move to next example
00084 //=======================================================================
00085 bool msdi_reflected_marked_images::next()
00086 {
00087   if (!marked_images_.next())
00088   {
00089     if (first_pass_ && !only_reflect_) 
00090     {
00091       first_pass_=false;
00092       marked_images_.reset();
00093     }
00094     else return false;
00095   }
00096   points_ok_=false;
00097   image_ok_=false;
00098   image_pyr_ok_=false;
00099 
00100   return true;
00101 }
00102 
00103 
00104 //: Load in current image and generate suitable pyramid
00105 void msdi_reflected_marked_images::get_image()
00106 {
00107   assert(marked_images_.image().is_a()=="vimt_image_2d_of<vxl_byte>");
00108   const vimt_image_2d_of<vxl_byte>& b_im
00109     = static_cast<const vimt_image_2d_of<vxl_byte>&>(marked_images_.image());
00110 
00111   if (!first_pass_)
00112   {
00113     image_ = b_im;
00114     image_ok_=true;
00115     return;
00116   }
00117 
00118   // First pass through, so reflect the image
00119   image_.image()=vil_flip_lr(b_im.image());
00120   image_.set_world2im(b_im.world2im());
00121   image_ok_=true;
00122 }
00123 
00124 void msdi_reflected_marked_images::get_points()
00125 {
00126   if (!first_pass_)
00127   {
00128     points_ = marked_images_.points();
00129     points_ok_=true;
00130     return;
00131   }
00132 
00133   // First pass through, so reflect the points about image midline.
00134   double ax = 0.5*(image().image_base().ni()-1);
00135   msm_reflect_shape_along_x(marked_images_.points(),
00136                             sym_pts_,points_,ax);
00137   points_ok_=true;
00138 }
00139 
00140 
00141 //: Return current image file name
00142 vcl_string msdi_reflected_marked_images::image_name() const
00143 {
00144   if (!first_pass_) return marked_images_.image_name();
00145 
00146   return "ref_"+marked_images_.image_name();
00147 }
00148 
00149 //: Return current points file name
00150 vcl_string msdi_reflected_marked_images::points_name() const
00151 {
00152   if (!first_pass_) return marked_images_.points_name();
00153 
00154   return "ref_"+marked_images_.points_name();
00155 }
00156 
00157 
00158