contrib/rpl/rgrl/rgrl_matcher_pseudo_int_3d.h
Go to the documentation of this file.
00001 #ifndef rgrl_matcher_pseudo_int_3d_h_
00002 #define rgrl_matcher_pseudo_int_3d_h_
00003 //:
00004 // \file
00005 // \author Chuck Stewart & Gehua Yang
00006 // \date   July 2004
00007 // By introducing "int" in the filename, it only means that
00008 // it will resample the mapped points to integer location and also discretize
00009 // the shift vector to integer location.
00010 
00011 #include <rgrl/rgrl_matcher.h>
00012 #include <rgrl/rgrl_feature_set.h>
00013 #include <rgrl/rgrl_view.h>
00014 #include <rgrl/rgrl_transformation.h>
00015 #include <rgrl/rgrl_scale.h>
00016 #include <rgrl/rgrl_feature_sptr.h>
00017 #include "rgrl_evaluator.h"
00018 #include <rgrl/rgrl_mask_sptr.h>
00019 #include <rgrl/rgrl_mask.h>
00020 #include <vil3d/vil3d_image_view.h>
00021 #include <vnl/vnl_double_3.h>
00022 #include <vnl/vnl_int_3.h>
00023 #include "rgrl_evaluator_sptr.h"
00024 
00025 #if 0 // ITK-specific
00026 template < class PixelType, int Dimension > class itkImage;
00027 #endif
00028 
00029 template < class PixelType >
00030 class rgrl_matcher_pseudo_int_3d
00031   : public rgrl_matcher
00032 {
00033  public:
00034 
00035   class rgrl_mapped_pixel_type {
00036    public:
00037     // the mapped location is to be integer
00038     vnl_int_3           location;
00039     double              intensity;
00040     double              weight;
00041     rgrl_mapped_pixel_type(): location(), intensity(0.0), weight(1.0)  {}
00042   };
00043 
00044   typedef vcl_vector< rgrl_mapped_pixel_type > rgrl_mapped_pixel_vector_type;
00045 
00046   //: Initialize the matcher using 3d images.
00047   //
00048   rgrl_matcher_pseudo_int_3d( vil3d_image_view<PixelType> const& from_image,
00049                               vil3d_image_view<PixelType> const& to_image,
00050                               vnl_vector< double > const& from_spacing_ratio,
00051                               vnl_vector< double > const& to_spacing_ratio,
00052                               rgrl_evaluator_sptr evaluator,
00053                               rgrl_mask_sptr mask = 0 );
00054 
00055   //:  Match the features in the "from" image to the intensity in the "to" image.
00056   //
00057   rgrl_match_set_sptr
00058   compute_matches( rgrl_feature_set const&     from_features,
00059                    rgrl_feature_set const&     to_features,
00060                    rgrl_view const&            current_view,
00061                    rgrl_transformation const&  current_xform,
00062                    rgrl_scale const&           current_scale ,
00063                    rgrl_match_set_sptr const& old_matches = 0);
00064 
00065   // Defines type-related functions
00066   rgrl_type_macro( rgrl_matcher_pseudo_int_3d, rgrl_matcher);
00067 
00068  private:
00069 
00070   // only to be used in the map_region_intensities function
00071   struct mapped_info {
00072     vnl_double_3  physical_, pixel_;
00073     PixelType     in_;
00074   };
00075 
00076   // only used when generating discrete shift
00077   struct discrete_shift_node {
00078     vnl_int_3  shift_;
00079     //: delta step compared to previous node
00080     double     step_;
00081 
00082     discrete_shift_node(): shift_(), step_(0.0) {}
00083 
00084     discrete_shift_node(const vnl_int_3& shift, const double& step)
00085       : shift_(shift), step_(step) {}
00086 
00087     discrete_shift_node operator-() const {
00088       discrete_shift_node that;
00089       that.shift_ = -shift_;
00090       that.step_ = -step_;
00091       return that;
00092     }
00093   };
00094 
00095   //:  Map the intensities of the image region.
00096   void
00097   map_region_intensities( rgrl_transformation      const& trans,
00098                           rgrl_feature_sptr               feature_sptr,
00099                           rgrl_mapped_pixel_vector_type & mapped_pixels ) const;
00100 
00101   //:  The actual work of mapping the region intensities.
00102   void
00103   map_region_intensities( vcl_vector< vnl_vector<int> > const& pixel_locations,
00104                           rgrl_transformation           const& trans,
00105                           rgrl_feature_sptr                    feature_sptr,
00106                           rgrl_mapped_pixel_vector_type      & mapped_pixels) const;
00107 
00108   //: move window around to find the optimum response
00109   void
00110   slide_window(rgrl_feature_sptr                    mapped_feature,
00111                rgrl_mapped_pixel_vector_type const& mapped_pixels,
00112                rgrl_scale                    const& current_scale,
00113                vcl_vector< rgrl_feature_sptr >    & matched_to_features,
00114                vcl_vector< double >               & match_weights ) const;
00115 
00116   //: compute response
00117   double compute_response( rgrl_mapped_pixel_vector_type const& mapped_pixels,
00118                            vnl_int_3                     const& shift ) const;
00119 
00120   //: sample pixel locations along a direction vector
00121   //  The first element is always [0 0 0]
00122   //  And it make sure all elements are connected
00123   void sample_pixels_along_direction( vcl_vector<discrete_shift_node>& two_dir_shifts,
00124                                       vnl_double_3 dir,
00125                                       double max_length ) const;
00126  private:
00127 
00128 #if 0 // ITK-specific
00129   typedef itkImage< PixelType, Dimension > ImageType;
00130 #endif // 0
00131 
00132   //  These are currently only for 2d images.  ITK templates across
00133   //  dimension.  VXL / rgrl does not.  Need to work with ITK images
00134   vil3d_image_view<PixelType> from_image_;
00135   vil3d_image_view<PixelType> to_image_;
00136 
00137   // When we try to search for the best match, we have to make sure
00138   // only the points inside the valid region are considered. The mask_
00139   // here seems to be duplicate with the mask_ in
00140   // rgrl_feature_set_location_masked.  But it seems to me that it
00141   // can't be avoid to put another mask_ here with the current design.
00142   // Also, this is also temporary for retina images. - Bess.
00143   rgrl_mask_sptr mask_;
00144   rgrl_evaluator_sptr evaluator_;
00145 
00146   // The ratio of physical coordinates / pixel coordinates.
00147   // It represents the physical distance between pixels.
00148   vnl_double_3 from_spacing_ratio_;
00149   vnl_double_3 to_spacing_ratio_;
00150 };
00151 
00152 #endif // rgrl_matcher_pseudo_int_3d_h_