core/vil/algo/vil_abs_shuffle_distance.h
Go to the documentation of this file.
00001 #ifndef vil_abs_shuffle_distance_h_
00002 #define vil_abs_shuffle_distance_h_
00003 //:
00004 // \file
00005 // \brief Compute shuffle distance between two images
00006 // \author Tim Cootes
00007 
00008 #include <vil/algo/vil_structuring_element.h>
00009 #include <vil/vil_image_view.h>
00010 
00011 //: Return minimum value of |im[offset[k]]-v0| k=0..n-1
00012 template <class T1, class T2>
00013 inline double vil_abs_shuffle_distance(T1 v0, const T2* im,
00014                                        const vcl_ptrdiff_t* offset, unsigned n)
00015 {
00016   double min_v = im[offset[0]]<v0?(v0-im[offset[0]]):(im[offset[0]]-v0);
00017   for (unsigned i=1;i<n;++i)
00018   {
00019     T2 v1 = im[offset[i]];
00020     double abs_diff = (v0<v1?(v1-v0):(v0-v1));
00021     if (abs_diff<min_v) min_v=abs_diff;
00022   }
00023   return min_v;
00024 }
00025 
00026 //: Return min difference of pixels under structuring element centred at (i0,j0)
00027 //  I.e., returns minimum of |v0-image(i,j,plane)| over (i,j) in element.
00028 //  Checks boundary overlap.
00029 //  \relatesalso vil_image_view
00030 //  \relatesalso vil_structuring_element
00031 template <class T1, class T2>
00032 inline double vil_abs_shuffle_distance(T1 v0, const vil_image_view<T2>& image,
00033                                        unsigned plane,
00034                                        const vil_structuring_element& element,
00035                                        int i0, int j0)
00036 {
00037   double min_v=9e99;
00038   unsigned n = element.p_i().size();
00039   for (unsigned int k=0;k<n;++k)
00040   {
00041     unsigned int i = i0+element.p_i()[k];
00042     unsigned int j = j0+element.p_j()[k];
00043     if (i<image.ni() && j<image.nj())
00044     {
00045       T2 v1 = image(i,j,plane);
00046       double abs_diff = (v0<v1?(v1-v0):(v0-v1));
00047       if (abs_diff < min_v) { min_v = abs_diff; }
00048     }
00049   }
00050   return min_v;
00051 }
00052 
00053 //: Computes shuffle distance between image1 and image2
00054 // For each pixel in image1 it finds the pixel in image2 with
00055 // the closest value in an offset area defined by the element.
00056 // Returns mean over all pixels of this minimum value.
00057 // Images must be of same size.
00058 // If include_borders is false then only include pixels
00059 // for which the structuring element is entirely within the image.
00060 // \relatesalso vil_image_view
00061 // \relatesalso vil_structuring_element
00062 template <class T1, class T2>
00063 double vil_abs_shuffle_distance(const vil_image_view<T1>& image1,
00064                                 const vil_image_view<T2>& image2,
00065                                 const vil_structuring_element& element,
00066                                 bool include_borders=true);
00067 
00068 #endif // vil_abs_shuffle_distance_h_