37 #ifndef VIGRA_MULTI_LOCALMINMAX_HXX
38 #define VIGRA_MULTI_LOCALMINMAX_HXX
42 #include "multi_array.hxx"
43 #include "localminmax.hxx"
44 #include "multi_gridgraph.hxx"
45 #include "multi_labeling.hxx"
46 #include "metaprogramming.hxx"
50 namespace boost_graph {
54 template <
class Graph,
class T1Map,
class T2Map,
class Compare>
56 localMinMaxGraph(Graph
const &g,
59 typename property_traits<T2Map>::value_type marker,
60 typename property_traits<T1Map const>::value_type threshold,
61 Compare
const &compare,
62 bool allowAtBorder =
true)
64 typedef typename graph_traits<Graph>::vertex_iterator graph_scanner;
65 typedef typename graph_traits<Graph>::adjacency_iterator neighbor_iterator;
67 typedef typename property_traits<T1Map const>::value_type T1;
69 graph_scanner node, srcend;
70 neighbor_iterator arc, nbend;
72 unsigned int count = 0;
74 for (; node != srcend; ++node)
76 const T1 current =
get(src, *node);
78 if (!compare(current, threshold))
81 if(!allowAtBorder && node.atBorder())
85 for (;arc != nbend; ++arc)
86 if (!compare(current,
get(src, *arc)))
91 put(dest, *node, marker);
100 namespace lemon_graph {
102 template <
class Graph,
class T1Map,
class T2Map,
class Compare>
104 localMinMaxGraph(Graph
const &g,
107 typename T2Map::value_type marker,
108 typename T1Map::value_type threshold,
109 Compare
const &compare,
110 bool allowAtBorder =
true)
112 typedef typename Graph::NodeIt graph_scanner;
113 typedef typename Graph::OutArcIt neighbor_iterator;
115 unsigned int count = 0;
116 for (graph_scanner node(g); node != INVALID; ++node)
118 typename T1Map::value_type current = src[*node];
120 if (!compare(current, threshold))
123 if(!allowAtBorder && node.atBorder())
126 neighbor_iterator arc(g, node);
127 for (; arc != INVALID; ++arc)
128 if (!compare(current, src[g.target(*arc)]))
133 dest[*node] = marker;
140 template <
class Graph,
class T1Map,
class T2Map,
class Compare,
class Equal>
142 extendedLocalMinMaxGraph(Graph
const &g,
145 typename T2Map::value_type marker,
146 typename T1Map::value_type threshold,
147 Compare
const &compare,
149 bool allowAtBorder =
true)
151 typename Graph::template NodeMap<unsigned int> regions(g);
153 int max_region_label = labelGraph(g, src, regions, equal);
156 std::vector<unsigned char> isExtremum(max_region_label+1, (
unsigned char)1);
158 typedef typename Graph::NodeIt graph_scanner;
159 typedef typename Graph::OutArcIt neighbor_iterator;
161 unsigned int count = max_region_label;
162 for (graph_scanner node(g); node != INVALID; ++node)
164 unsigned int label = regions[*node];
166 if(!isExtremum[label])
169 typename T1Map::value_type current = src[*node];
171 if (!compare(current, threshold) ||
172 (!allowAtBorder && node.atBorder()))
174 isExtremum[label] = 0;
179 for (neighbor_iterator arc(g, node); arc != INVALID; ++arc)
181 if (label != regions[g.target(*arc)] && compare(src[g.target(*arc)], current))
183 isExtremum[label] = 0;
189 for (graph_scanner node(g); node != INVALID; ++node)
191 if(isExtremum[regions[*node]])
192 dest[*node] = marker;
199 template <
unsigned int N,
class T1,
class C1,
202 class EqualityFunctor>
204 localMinMax(MultiArrayView<N, T1, C1>
const & src,
205 MultiArrayView<N, T2, C2> dest,
207 Compare
const & compare,
208 EqualityFunctor
const & equal,
209 LocalMinmaxOptions
const & options = LocalMinmaxOptions())
211 vigra_precondition(src.shape() == dest.shape(),
212 "localMinMax(): shape mismatch between input and output.");
216 if(options.neigh == 0 || options.neigh == 2*N)
218 else if(options.neigh == 1 || options.neigh == MetaPow<3, N>::value - 1)
221 vigra_precondition(
false,
222 "localMinMax(): option object specifies invalid neighborhood type.");
224 T2 marker = (T2)options.marker;
226 GridGraph<N, undirected_tag> graph(src.shape(), neighborhood);
227 if(options.allow_plateaus)
228 return lemon_graph::extendedLocalMinMaxGraph(graph, src, dest, marker, threshold,
229 compare, equal, options.allow_at_border);
231 return lemon_graph::localMinMaxGraph(graph, src, dest, marker, threshold,
232 compare, options.allow_at_border);
242 template <
unsigned int N,
class T1,
class C1,
class T2,
class C2>
245 MultiArrayView<N, T2, C2> dest,
246 LocalMinmaxOptions
const & options = LocalMinmaxOptions())
248 T1 threshold = options.use_threshold
249 ? std::min(NumericTraits<T1>::max(), (T1)options.thresh)
250 : NumericTraits<T1>::max();
251 return localMinMax(src, dest, threshold, std::less<T1>(), std::equal_to<T1>(), options);
255 template <
unsigned int N,
class T1,
class S1,
257 class EqualityFunctor>
260 MultiArrayView<N, T2, S2> dest,
261 EqualityFunctor
const & equal,
262 LocalMinmaxOptions options = LocalMinmaxOptions())
264 options.allowPlateaus();
265 T1 threshold = options.use_threshold
266 ? std::min(NumericTraits<T1>::max(), (T1)options.thresh)
267 : NumericTraits<T1>::max();
268 return localMinMax(src, dest, threshold, std::less<T1>(), equal, options);
277 template <
unsigned int N,
class T1,
class C1,
class T2,
class C2>
280 MultiArrayView<N, T2, C2> dest,
281 LocalMinmaxOptions
const & options = LocalMinmaxOptions())
283 T1 threshold = options.use_threshold
284 ? std::max(NumericTraits<T1>::min(), (T1)options.thresh)
285 : NumericTraits<T1>::min();
286 return localMinMax(src, dest, threshold, std::greater<T1>(), std::equal_to<T1>(), options);
290 template <
unsigned int N,
class T1,
class S1,
292 class EqualityFunctor>
295 MultiArrayView<N, T2, S2> dest,
296 EqualityFunctor
const & equal,
297 LocalMinmaxOptions options = LocalMinmaxOptions())
299 options.allowPlateaus();
300 T1 threshold = options.use_threshold
301 ? std::max(NumericTraits<T1>::min(), (T1)options.thresh)
302 : NumericTraits<T1>::min();
303 return localMinMax(src, dest, threshold, std::greater<T1>(), equal, options);
308 #endif // VIGRA_MULTI_LOCALMINMAX_HXX
void put(vigra::MultiArrayView< N, T, Stride > &pmap, typename vigra::MultiArrayView< N, T, Stride >::difference_type const &k, U const &val)
Put value val at key k in the property map pmap (API: boost).
Definition: multi_gridgraph.hxx:2847
std::pair< typename vigra::GridGraph< N, DirectedTag >::adjacency_iterator, typename vigra::GridGraph< N, DirectedTag >::adjacency_iterator > adjacent_vertices(typename vigra::GridGraph< N, DirectedTag >::vertex_descriptor const &v, vigra::GridGraph< N, DirectedTag > const &g)
Get a (begin, end) iterator pair for the neighbor vertices of vertex v in graph g (API: boost)...
Definition: multi_gridgraph.hxx:2706
void extendedLocalMinima(...)
Find local minimal regions (plateaus) in an array.
void localMinima(...)
Find local minima in an image or multi-dimensional array.
void localMaxima(...)
Find local maxima in an image or multi-dimensional array.
use direct and indirect neighbors
Definition: multi_shape.hxx:254
void extendedLocalMaxima(...)
Find local maximal regions in an array.
std::pair< typename vigra::GridGraph< N, DirectedTag >::vertex_iterator, typename vigra::GridGraph< N, DirectedTag >::vertex_iterator > vertices(vigra::GridGraph< N, DirectedTag > const &g)
Get a (begin, end) iterator pair for the vertices of graph g (API: boost).
Definition: multi_gridgraph.hxx:2683
use only direct neighbors
Definition: multi_shape.hxx:253
NeighborhoodType
Choose the neighborhood system in a dimension-independent way.
Definition: multi_shape.hxx:252