contrib/mul/vil3d/algo/vil3d_abs_shuffle_distance.h
Go to the documentation of this file.
00001 #ifndef vil3d_abs_shuffle_distance_h_
00002 #define vil3d_abs_shuffle_distance_h_
00003 //:
00004 // \file
00005 // \brief Compute shuffle distance between two 3D images
00006 // \author Vlad Petrovic & Tim Cootes
00007 
00008 #include <vil3d/algo/vil3d_structuring_element.h>
00009 #include <vil3d/vil3d_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 vil3d_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,k0)
00027 //  I.e., returns minimum of |v0-image(i,j,k,plane)| over (i,j,k) in element.
00028 //  Checks boundary overlap.
00029 template <class T1, class T2>
00030 inline double vil3d_abs_shuffle_distance(T1 v0, const vil3d_image_view<T2>& image,
00031                              unsigned plane,
00032                              const vil3d_structuring_element& element,
00033                              int i0, int j0, int k0)
00034 {
00035   double min_v=9e99;
00036   unsigned n = element.p_i().size();
00037   for (unsigned int m=0;m<n;++m)
00038   {
00039     unsigned int i = i0+element.p_i()[m];
00040     unsigned int j = j0+element.p_j()[m];
00041     unsigned int k = k0+element.p_k()[m];
00042     if (i<image.ni() && j<image.nj() && k<image.nk())
00043     {
00044       T2 v1 = image(i,j,k,plane);
00045       double abs_diff = (v0<v1?(v1-v0):(v0-v1));
00046       if (abs_diff < min_v) { min_v = abs_diff; }
00047     }
00048   }
00049   return min_v;
00050 }
00051 
00052 //: Computes shuffle distance between image1 and image2
00053 // For each pixel in image1 it finds the pixel in image2 with
00054 // the closest value in an offset area defined by the element.
00055 // Returns mean over all pixels of this minimum value.
00056 // Images must be of same size.
00057 // If include_borders is false then only include pixels
00058 // for which the structuring element is entirely within the image.
00059 // \relatesalso vil_image_view
00060 // \relatesalso vil_structuring_element
00061 template <class T1, class T2>
00062 double vil3d_abs_shuffle_distance(const vil3d_image_view<T1>& image1,
00063                                 const vil3d_image_view<T2>& image2,
00064                                 const vil3d_structuring_element& element,
00065                                 bool include_borders=true);
00066 
00067 //: Computes shuffle distance between image1 and image2
00068 // For each pixel in image1 it finds the pixel in image2 with
00069 // the closest value in an offset area defined by the element.
00070 // If include_borders is false then only include pixels
00071 // for which the structuring element is entirely within the image.
00072 // \relatesalso vil_image_view
00073 // \relatesalso vil_structuring_element
00074 template <class T1, class T2>
00075 void vil3d_abs_shuffle_distance(const vil3d_image_view<T1>& image1,
00076                                 const vil3d_image_view<T2>& image2,
00077                                 const vil3d_structuring_element& element,
00078                                 vil3d_image_view<T1>& image3);
00079 
00080 #endif // vil3d_abs_shuffle_distance_h_