Go to the documentation of this file.00001
00002 #ifndef vil3d_overlap_h_
00003 #define vil3d_overlap_h_
00004
00005
00006
00007
00008
00009
00010 #include <vil3d/vil3d_image_view.h>
00011 #include <vil3d/vil3d_scan_image.h>
00012
00013
00014 double vil3d_overlap_dice(const vil3d_image_view<bool>& im1,
00015 const vil3d_image_view<bool>& im2);
00016
00017
00018 double vil3d_overlap_jaccard(const vil3d_image_view<bool>& im1,
00019 const vil3d_image_view<bool>& im2);
00020
00021
00022 template <class T1, class T2>
00023 class vil3d_overlap_functor
00024 {
00025 public:
00026
00027 T1 t1;
00028
00029 T2 t2;
00030
00031
00032 unsigned n1not2;
00033
00034
00035 unsigned n2not1;
00036
00037
00038 unsigned n1and2;
00039
00040
00041 vil3d_overlap_functor(T1 t1a, T2 t2a)
00042 : t1(t1a),t2(t2a),n1not2(0),n2not1(0),n1and2(0) {}
00043
00044
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
00061
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
00074
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_