contrib/mul/vil3d/algo/vil3d_overlap.h
Go to the documentation of this file.
00001 // This is mul/vil3d/algo/vil3d_overlap.h
00002 #ifndef vil3d_overlap_h_
00003 #define vil3d_overlap_h_
00004 //:
00005 // \file
00006 // \brief Compute overlaps of images
00007 // \author Tim Cootes
00008 
00009 
00010 #include <vil3d/vil3d_image_view.h>
00011 #include <vil3d/vil3d_scan_image.h>
00012 
00013 //: Dice overlap = 2*intersection/(intersection+union)
00014 double vil3d_overlap_dice(const vil3d_image_view<bool>& im1,
00015                           const vil3d_image_view<bool>& im2);
00016 
00017 //: Jaccard overlap = intersection/union
00018 double vil3d_overlap_jaccard(const vil3d_image_view<bool>& im1,
00019                              const vil3d_image_view<bool>& im2);
00020 
00021 //: Functor to compute overlaps by thresholding voxel values
00022 template <class T1, class T2>
00023 class vil3d_overlap_functor
00024 {
00025  public:
00026   //: Threshold for image 1
00027   T1 t1;
00028   //: Threshold for image 2
00029   T2 t2;
00030 
00031   //: N voxels true in 1 but not in 2
00032   unsigned n1not2;
00033 
00034   //: N voxels true in 2 but not in 1
00035   unsigned n2not1;
00036 
00037   //: N voxels true in 1 and 2
00038   unsigned n1and2;
00039 
00040   //: Constructor, defining thresholds for voxels to be true
00041   vil3d_overlap_functor(T1 t1a, T2 t2a)
00042     : t1(t1a),t2(t2a),n1not2(0),n2not1(0),n1and2(0) {}
00043 
00044   //: Operator function
00045   void operator()(T1 vox1, T2 vox2)
00046   {
00047     if (vox1>t1)
00048     {
00049       if (vox2>t2) n1and2++;
00050       else         n1not2++;
00051     }
00052     else
00053       if (vox2>t2) n2not1++;
00054   }
00055 
00056   unsigned n_union() const { return n1not2+ n2not1 + n1and2; }
00057   unsigned n_intersection() const { return n1and2; }
00058 };
00059 
00060 //: Dice overlap = 2*intersection/(intersection+union)
00061 //  Voxel in image A is true if its value is strictly above the threshold t
00062 template <class T1, class T2>
00063 double vil3d_overlap_dice(const vil3d_image_view<T1>& im1, T1 t1,
00064                           const vil3d_image_view<T2>& im2, T2 t2)
00065 {
00066   vil3d_overlap_functor<T1,T2> f(t1,t2);
00067   vil3d_scan_image(im1,im2,f);
00068   unsigned d=f.n_intersection()+f.n_union();
00069   if (d==0) return 0.0;
00070   return (2.0*f.n_intersection())/d;
00071 }
00072 
00073 //: Jaccard overlap = intersection/union
00074 //  Voxel in image A is true if its value is strictly above the threshold t
00075 template <class T1, class T2>
00076 double vil3d_overlap_jaccard(const vil3d_image_view<T1>& im1, T1 t1,
00077                              const vil3d_image_view<T2>& im2, T2 t2)
00078 {
00079   vil3d_overlap_functor<T1,T2> f(t1,t2);
00080   vil3d_scan_image(im1,im2,f);
00081   if (f.n_union()==0) return 0.0;
00082   return double(f.n_intersection())/f.n_union();
00083 }
00084 
00085 
00086 #endif // vil3d_overlap_h_