contrib/mul/vil3d/algo/vil3d_overlap.cxx
Go to the documentation of this file.
00001 //:
00002 // \file
00003 // \brief Compute overlaps of images
00004 // \author Tim Cootes
00005 
00006 #include "vil3d_overlap.h"
00007 
00008 class vil3d_bool_overlap_functor
00009 {
00010 public:
00011   //: N voxels true in A but not in B
00012   unsigned nAnotB;
00013 
00014   //: N voxels true in B but not in A
00015   unsigned nBnotA;
00016 
00017   //: N voxels true in A and B
00018   unsigned nAandB;
00019 
00020   //: Constructor
00021   vil3d_bool_overlap_functor()
00022     : nAnotB(0),nBnotA(0),nAandB(0) {}
00023 
00024   //: Operator function
00025   void operator()(bool voxA, bool voxB)
00026   {
00027     if (voxA)
00028     {
00029       if (voxB) nAandB++;
00030       else      nAnotB++;
00031     }
00032     else
00033       if (voxB) nBnotA++;
00034   }
00035 
00036   unsigned n_union() { return nAnotB+ nBnotA + nAandB; }
00037   unsigned n_intersection() { return nAandB; }
00038 };
00039 
00040 
00041 //: Dice overlap = 2*intersection/(intersection+union)
00042 double vil3d_overlap_dice(const vil3d_image_view<bool>& im1,
00043                         const vil3d_image_view<bool>& im2)
00044 {
00045   vil3d_bool_overlap_functor f;
00046   vil3d_scan_image(im1,im2,f);
00047   unsigned d=f.n_intersection()+f.n_union();
00048   if (d==0) return 0.0;
00049   return double(2*f.n_intersection())/d;
00050 }
00051 
00052 //: Jaccard overlap = intersection/union
00053 double vil3d_overlap_jaccard(const vil3d_image_view<bool>& im1,
00054                         const vil3d_image_view<bool>& im2)
00055 {
00056   vil3d_bool_overlap_functor f;
00057   vil3d_scan_image(im1,im2,f);
00058   if (f.n_union()==0) return 0.0;
00059   return double(f.n_intersection())/f.n_union();
00060 }
00061