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
00028 #ifndef OLENA_LEVEL_CONNECTED_HH
00029 # define OLENA_LEVEL_CONNECTED_HH
00030
00031 # include <ntg/basics.hh>
00032 # include <oln/level/lut.hh>
00033 # include <oln/level/fill.hh>
00034 # include <oln/morpho/splitse.hh>
00035 # include <oln/convert/conversion_ng_se.hh>
00036
00037 namespace oln {
00038
00039 namespace level {
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 template <class DestType, class I, class N>
00059 typename mute<I, DestType>::ret
00060 connected_component(const abstract::binary_image_with_dim<2, I>& input,
00061 const abstract::neighborhood<N>& Ng)
00062 {
00063 mlc::eq<I::dim, N::dim>::ensure();
00064 typename mute<I, DestType>::ret output(input.size());
00065 level::hlut< DestType, DestType > T;
00066 DestType k = 1;
00067
00068 fill(output, 0);
00069 oln_iter_type(I) p(input);
00070 for_all(p)
00071 {
00072 if (input[p] == true)
00073 {
00074 typedef typename abstract::neighborhood<N>::win_type E;
00075 E se_plus = morpho::get_plus_se_only(convert::ng_to_se(Ng));
00076 oln_neighb_type(E) p_prime(se_plus, p);
00077 bool all_zero = true;
00078 for_all(p_prime)
00079 if (input.hold(p_prime) && input[p_prime])
00080 {
00081 all_zero = false;
00082 break;
00083 }
00084 if (all_zero)
00085 {
00086 output[p] = k++;
00087 }
00088 else
00089 {
00090
00091 bool same_label = true;
00092 DestType cl = 0;
00093
00094 for_all (p_prime)
00095 if (output.hold(p_prime))
00096 {
00097 cl = output[p_prime];
00098 break;
00099 }
00100
00101 for_all_remaining (p_prime)
00102 if (output.hold(p_prime) && cl != output[p_prime])
00103 {
00104 same_label = false;
00105 break;
00106 }
00107
00108 if (same_label)
00109 output[p] = cl;
00110 else
00111 {
00112 DestType min = ntg_max_val(DestType);
00113 for_all(p_prime)
00114 if (output.hold(p_prime))
00115 if (output[p_prime] && T(output[p_prime]) < min)
00116 min = T(output[p_prime]);
00117 output[p] = min;
00118
00119 for_all(p_prime)
00120 if (input.hold(p_prime) && output[p_prime])
00121 {
00122 DestType m;
00123 DestType beta = T(output[p_prime]);
00124 while (T(beta) != min)
00125 {
00126 m = T(beta);
00127 T.set(beta, min);
00128 beta = m;
00129 }
00130 }
00131 }
00132 }
00133 }
00134 }
00135
00136 for (DestType i = 1; i <= k; ++i)
00137 {
00138 DestType m = i;
00139 while (T(m) != m)
00140 m = T(m);
00141 T.set(i,m);
00142 }
00143 for_all (p)
00144 output[p] = T(output[p]);
00145 return output;
00146 }
00147
00148 }
00149
00150 }
00151
00152 #endif // ! OLENA_LEVEL_CONNECTED_HH