cc.hh

00001 // Copyright (C) 2002, 2003, 2004  EPITA Research and Development Laboratory
00002 //
00003 // This file is part of the Olena Library.  This library is free
00004 // software; you can redistribute it and/or modify it under the terms
00005 // of the GNU General Public License version 2 as published by the
00006 // Free Software Foundation.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this library; see the file COPYING.  If not, write to
00015 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
00016 // MA 02111-1307, USA.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software library without restriction.  Specifically, if other files
00020 // instantiate templates or use macros or inline functions from this
00021 // file, or you compile this file and link it with other files to
00022 // produce an executable, this file does not by itself cause the
00023 // resulting executable to be covered by the GNU General Public
00024 // License.  This exception does not however invalidate any other
00025 // reasons why the executable file might be covered by the GNU General
00026 // Public License.
00027 
00028 #ifndef OLENA_LEVEL_CC_HH
00029 # define OLENA_LEVEL_CC_HH
00030 
00031 /*
00032   Connected components.
00033 */
00034 
00035 # include <oln/arith/ops.hh>
00036 # include <oln/core/image.hh>
00037 # include <oln/level/fill.hh>
00038 
00039 # include <set>
00040 
00041 namespace oln {
00042 
00045   namespace level {
00046 
00047     // optional behavior for this algorithm.
00048     struct update_label;
00049 
00094     template <class DestType, class I, class E>
00095     typename mute<I, DestType>::ret
00096     frontp_connected_component(const abstract::binary_image<I>& input,
00097                                const abstract::neighborhood<E>& se,
00098                                unsigned& nb_label)
00099     {
00100       typename mute<I, DestType>::ret output(input.size());
00101       level::fill(output, 0);
00102 
00103       typedef std::set<oln_point_type(I),
00104                        oln::internal::default_less<oln_point_type(I) > >
00105               points_set;
00106 
00107       I is_processed(input.size());
00108       level::fill(is_processed, false);
00109       DestType cur_label = 1;
00110       oln_iter_type(I) p(input);
00111       for_all(p) if ((input[p] == true)&& (is_processed[p] == false))
00112         {
00113           //propagation front
00114           points_set component;
00115           component.insert(p.cur());
00116           points_set points_to_process;
00117           points_to_process.insert(p.cur());
00118           while (!points_to_process.empty())
00119             {
00120               //set label and net neighbors
00121               points_set next;
00122               for (typename points_set::const_iterator i =
00123                      points_to_process.begin();
00124                    i != points_to_process.end();
00125                    ++i)
00126                 {
00127                   component.insert(*i);
00128                   oln_neighb_type(E) p_prime(se, *i);
00129                   for_all (p_prime) if(input.hold(p_prime) &&
00130                                        (input[p_prime] == true))
00131                     next.insert(p_prime.cur());
00132                 }
00133               points_to_process.clear();
00134               set_difference(next.begin(), next.end(),
00135                              component.begin(), component.end(),
00136                              inserter(points_to_process,
00137                                       points_to_process.begin()),
00138                              oln::internal::default_less<oln_point_type(I) >());
00139             }
00140           for (typename points_set::const_iterator i = component.begin();
00141                i != component.end();
00142                ++i)
00143             {
00144               output[*i] = cur_label;
00145               is_processed[*i] = true;
00146             }
00147           cur_label++;
00148         }
00149       nb_label = cur_label;
00150       return output;
00151     }
00152 
00153     template <class DestType, class I, class E>
00154     typename mute<I, DestType>::ret
00155     frontp_connected_component(const abstract::image<I>& input,
00156                                const abstract::neighborhood<E>& se)
00157     {
00158       unsigned dummy;
00159       return frontp_connected_component(input, se, dummy);
00160     }
00161 
00162     template <class I>
00163     typename mute<I, ntg::bin>::ret
00164     extract_i_cc(const abstract::image<I>& input,
00165                  oln_value_type(I) i)
00166     {
00167 
00168       typename mute<I, ntg::bin>::ret output(input.size());
00169       level::fill(output, false);
00170       oln_iter_type(I) p(input);
00171       for_all(p)
00172         if (input[p] == i)
00173           output[p] = true;
00174       return output;
00175     }
00176 
00177     template <class I>
00178     oln_value_type(I) get_n_cc(const abstract::image<I>& input)
00179     {
00180       return fold(arith::default_f_max<oln_value_type(I)>(), input);
00181     }
00182 
00183   } // end of namespace level
00184 
00185 } // end of namespace oln
00186 
00187 #endif // ! OLENA_LEVEL_CC_HH

Generated on Thu Apr 15 20:13:07 2004 for Olena by doxygen 1.3.6-20040222