00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 #ifndef MLN_LABELING_FILL_HOLES_HH
00028 # define MLN_LABELING_FILL_HOLES_HH
00029 
00033 
00034 # include <mln/labeling/background.hh>
00035 # include <mln/labeling/compute.hh>
00036 
00037 # include <mln/data/transform.hh>
00038 
00039 # include <mln/core/image/dmorph/image_if.hh>
00040 # include <mln/accu/math/count.hh>
00041 
00042 
00043 namespace mln
00044 {
00045 
00046   namespace labeling
00047   {
00048 
00062     template <typename I, typename N, typename L>
00063     mln_concrete(I)
00064     fill_holes(const Image<I>& input, const Neighborhood<N>& nbh,
00065                L& nlabels);
00066 
00067 
00068 # ifndef MLN_INCLUDE_ONLY
00069 
00070     template <typename I, typename N, typename L>
00071     inline
00072     mln_concrete(I)
00073     fill_holes(const Image<I>& input, const Neighborhood<N>& nbh,
00074                L& nlabels)
00075     {
00076       trace::entering("labeling::fill_holes");
00077 
00078       mlc_equal(mln_trait_image_kind(I),
00079                 mln::trait::image::kind::binary)::check();
00080       mln_precondition(exact(input).is_valid());
00081       mln_precondition(exact(nbh).is_valid());
00082 
00083       mln_ch_value(I, L) lbls = labeling::background(input, nbh, nlabels);
00084 
00085       accu::math::count<mln_value(I)> a_;
00086       util::array<unsigned> arr = labeling::compute(a_, input, lbls, nlabels);
00087 
00088       unsigned bg_count = 0;
00089       unsigned bg_lbl = 0;
00090 
00091       
00092       for (unsigned i = 1; i < arr.nelements(); ++i)
00093       {
00094         if (arr[i] > bg_count)
00095         {
00096           bg_count = arr[i];
00097           bg_lbl = i;
00098         }
00099       }
00100 
00101       util::array<bool> bg_relbl(arr.nelements(), true);
00102       bg_relbl(bg_lbl) = false;
00103       mln_ch_value(I, bool) output = data::transform(lbls, bg_relbl);
00104 
00105       trace::exiting("labeling::fill_holes");
00106       return output;
00107     }
00108 
00109 # endif // ! MLN_INCLUDE_ONLY
00110 
00111   } 
00112 
00113 } 
00114 
00115 
00116 #endif // ! MLN_LABELING_FILL_HOLES_HH