SMIL  1.0.4
DMorphoMaxTreeCriteria.hpp
1 /* __HEAD__
2  * Copyright (c) 2017-2024, Centre de Morphologie Mathematique
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of Matthieu FAESSEL, or ARMINES nor the
14  * names of its contributors may be used to endorse or promote products
15  * derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * Description :
30  * This file does... some very complex morphological operation...
31  *
32  * History :
33  * - XX/XX/2017 - by Beatriz Marcotegui
34  * Just created it...A
35  *
36  * __HEAD__ - Stop here !
37  */
38 
39 #ifndef MORPHO_MAX_TREE_CRITERIA_H_
40 #define MORPHO_MAX_TREE_CRITERIA_H_
41 
42 #include <complex>
43 #include <memory>
44 #include <queue>
45 
46 #include "DMorphoHierarQ.hpp" //BMI
47 
48 namespace smil
49 {
50 
53  template <class Attr_T>
55  {
56  public:
58  {
59  }
60 
61  virtual ~GenericCriterion()
62  {
63  }
64 
65  public:
66  virtual void initialize() = 0;
67  virtual void reset() = 0;
68  virtual void merge(GenericCriterion *other_criteron) = 0;
69  virtual void update(const size_t x, const size_t y, const size_t z) = 0;
70  virtual bool operator<(const Attr_T &other_attribute) = 0;
71  Attr_T getAttributeValue()
72  {
73  compute();
74  return attribute_value_;
75  }
76 
77  protected:
78  virtual void compute() = 0;
79 
80  protected:
81  Attr_T attribute_value_;
82  };
83 
86  class AreaCriterion : public GenericCriterion<size_t>
87  {
88  public:
90  {
91  initialize();
92  }
93 
94  virtual ~AreaCriterion()
95  {
96  }
97 
98  public:
99  virtual void initialize()
100  {
101  attribute_value_ = 1;
102  }
103 
104  virtual void reset()
105  {
106  attribute_value_ = 0;
107  }
108 
109  virtual void merge(GenericCriterion *other_criteron)
110  {
111  // AreaCriterion &oc = dynamic_cast<AreaCriterion &>(*other_criteron);
112 
113  attribute_value_ +=
114  dynamic_cast<AreaCriterion &>(*other_criteron).attribute_value_;
115  }
116 
117  virtual void update(SMIL_UNUSED const size_t x, SMIL_UNUSED const size_t y,
118  SMIL_UNUSED const size_t z)
119  {
120  attribute_value_ += 1;
121  }
122  virtual bool operator<(const size_t &other_attribute)
123  {
124  return (attribute_value_ < other_attribute);
125  }
126 
127  protected:
128  virtual void compute()
129  {
130  }
131  };
132 
135  class HeightCriterion : public GenericCriterion<size_t>
136  {
137  public:
139  {
140  initialize();
141  }
142 
143  virtual ~HeightCriterion()
144  {
145  }
146 
147  public:
148  virtual void initialize()
149  {
150  attribute_value_ = 0;
151  // lowest instead of min in Andres code
152  y_max_ = std::numeric_limits<size_t>::min();
153  y_min_ = std::numeric_limits<size_t>::max();
154  }
155 
156  virtual void reset()
157  {
158  initialize();
159  }
160 
161  virtual void merge(GenericCriterion *other_criteron)
162  {
163  // HeightCriterion &oc = dynamic_cast<HeightCriterion &>(*other_criteron);
164 
165  y_max_ = std::max(
166  y_max_, dynamic_cast<HeightCriterion &>(*other_criteron).y_max_);
167  y_min_ = std::min(
168  y_min_, dynamic_cast<HeightCriterion &>(*other_criteron).y_min_);
169  }
170 
171  virtual void update(SMIL_UNUSED const size_t x, const size_t y,
172  SMIL_UNUSED const size_t z)
173  {
174  y_max_ = std::max(y_max_, y);
175  y_min_ = std::min(y_min_, y);
176  }
177  virtual bool operator<(const size_t &other_attribute)
178  {
179  return (attribute_value_ < other_attribute);
180  }
181 
182  protected:
183  virtual void compute()
184  {
185  attribute_value_ = y_max_ - y_min_ + 1;
186  }
187 
188  private:
189  // BMI: int in Andres code
190  size_t y_max_;
191  size_t y_min_;
192  };
193 
196  class WidthCriterion : public GenericCriterion<size_t>
197  {
198  public:
200  {
201  initialize();
202  }
203 
204  virtual ~WidthCriterion()
205  {
206  }
207 
208  public:
209  virtual void initialize()
210  {
211  attribute_value_ = 0;
212  // lowest instead of min in Andres code
213  x_max_ = std::numeric_limits<size_t>::min();
214  x_min_ = std::numeric_limits<size_t>::max();
215  }
216 
217  virtual void reset()
218  {
219  initialize();
220  }
221 
222  virtual void merge(GenericCriterion *other_criteron)
223  {
224  // WidthCriterion &oc = dynamic_cast<WidthCriterion &>(*other_criteron);
225 
226  x_max_ = std::max(x_max_,
227  dynamic_cast<WidthCriterion &>(*other_criteron).x_max_);
228  x_min_ = std::min(x_min_,
229  dynamic_cast<WidthCriterion &>(*other_criteron).x_min_);
230  }
231 
232  virtual void update(const size_t x, SMIL_UNUSED const size_t y,
233  SMIL_UNUSED const size_t z)
234  {
235  x_max_ = std::max(x_max_, x);
236  x_min_ = std::min(x_min_, x);
237  }
238  virtual bool operator<(const size_t &other_attribute)
239  {
240  return (attribute_value_ < other_attribute);
241  }
242 
243  protected:
244  virtual void compute()
245  {
246  attribute_value_ = x_max_ - x_min_ + 1;
247  }
248 
249  private:
250  // BMI: int in Andres code
251  size_t x_max_;
252  size_t x_min_;
253  };
254 
255  struct HA {
256  size_t H;
257  size_t A;
258  };
259  struct HWA {
260  size_t H;
261  size_t W;
262  size_t A;
263  };
264 
267  class HACriterion : public GenericCriterion<HA>
268  {
269  public:
270  HACriterion()
271  {
272  initialize();
273  }
274 
275  virtual ~HACriterion()
276  {
277  }
278 
279  public:
280  virtual void initialize()
281  {
282  attribute_value_.H = 1;
283  attribute_value_.A = 0;
284 
285  // lowest instead of min in Andres code
286  y_max_ = std::numeric_limits<size_t>::min();
287  y_min_ = std::numeric_limits<size_t>::max();
288  }
289 
290  virtual void reset()
291  {
292  initialize();
293  }
294 
295  virtual void merge(GenericCriterion *other_criteron)
296  {
297  // HACriterion &oc = dynamic_cast<HACriterion &>(*other_criteron);
298 
299  attribute_value_.A +=
300  dynamic_cast<HACriterion &>(*other_criteron).getAttributeValue().A;
301 
302  y_max_ =
303  std::max(y_max_, dynamic_cast<HACriterion &>(*other_criteron).y_max_);
304  y_min_ =
305  std::min(y_min_, dynamic_cast<HACriterion &>(*other_criteron).y_min_);
306  }
307 
308  virtual void update(SMIL_UNUSED const size_t x, const size_t y,
309  SMIL_UNUSED const size_t z)
310  {
311  attribute_value_.A += 1;
312  y_max_ = std::max(y_max_, y);
313  y_min_ = std::min(y_min_, y);
314  }
315  virtual bool operator<(const HA &other_attribute)
316  {
317  return (attribute_value_.H < other_attribute.H);
318  }
319 
320  protected:
321  virtual void compute()
322  {
323  attribute_value_.H = y_max_ - y_min_ + 1;
324  }
325 
326  private:
327  // BMI: int in Andres code
328  size_t y_max_;
329  size_t y_min_;
330  };
331 
334  class HWACriterion : public GenericCriterion<HWA>
335  {
336  public:
337  HWACriterion()
338  {
339  initialize();
340  }
341 
342  virtual ~HWACriterion()
343  {
344  }
345 
346  public:
347  virtual void initialize()
348  {
349  attribute_value_.H = 1;
350  attribute_value_.W = 1;
351  attribute_value_.A = 0;
352 
353  // lowest instead of min in Andres code
354  x_max_ = std::numeric_limits<size_t>::min();
355  x_min_ = std::numeric_limits<size_t>::max();
356 
357  y_max_ = std::numeric_limits<size_t>::min();
358  y_min_ = std::numeric_limits<size_t>::max();
359  }
360 
361  virtual void reset()
362  {
363  initialize();
364  }
365 
366  virtual void merge(GenericCriterion *other_criteron)
367  {
368  // HWACriterion &oc = dynamic_cast<HWACriterion &>(*other_criteron);
369 
370  attribute_value_.A +=
371  dynamic_cast<HWACriterion &>(*other_criteron).getAttributeValue().A;
372 
373  x_max_ = std::max(x_max_,
374  dynamic_cast<HWACriterion &>(*other_criteron).x_max_);
375  x_min_ = std::min(x_min_,
376  dynamic_cast<HWACriterion &>(*other_criteron).x_min_);
377 
378  y_max_ = std::max(y_max_,
379  dynamic_cast<HWACriterion &>(*other_criteron).y_max_);
380  y_min_ = std::min(y_min_,
381  dynamic_cast<HWACriterion &>(*other_criteron).y_min_);
382  }
383 
384  virtual void update(const size_t x, const size_t y,
385  SMIL_UNUSED const size_t z)
386  {
387  attribute_value_.A += 1;
388  x_max_ = std::max(x_max_, x);
389  x_min_ = std::min(x_min_, x);
390 
391  // AttributeOpen with this criterion would be a Height Opening
392  y_max_ = std::max(y_max_, y);
393  y_min_ = std::min(y_min_, y);
394  }
395  virtual bool operator<(const HWA &other_attribute)
396  {
397  return (attribute_value_.H < other_attribute.H);
398  }
399 
400  protected:
401  virtual void compute()
402  {
403  attribute_value_.W = x_max_ - x_min_ + 1;
404  attribute_value_.H = y_max_ - y_min_ + 1;
405  }
406 
407  private:
408  // BMI: int in Andres code
409  size_t x_max_;
410  size_t x_min_;
411 
412  size_t y_max_;
413  size_t y_min_;
414  };
415 
416 } // namespace smil
417 
418 #endif // MORPHO_MAX_TREE_CRITERIA_H_
Area criterion. Useful for Area Opening/Closing algorithms based on max-tree.
Definition: DMorphoMaxTreeCriteria.hpp:87
Generic criterion for the max-tree. A user-defined criterion should be derived from this class.
Definition: DMorphoMaxTreeCriteria.hpp:55
HeightArea criterion. Useful for Height Opening/Closing algorithms based on max-tree.
Definition: DMorphoMaxTreeCriteria.hpp:268
HeightArea criterion. Useful for Height Opening/Closing algorithms based on max-tree.
Definition: DMorphoMaxTreeCriteria.hpp:335
Height criterion. Useful for Height Opening/Closing algorithms based on max-tree.
Definition: DMorphoMaxTreeCriteria.hpp:136
Width criterion. Useful for Width Opening/Closing algorithms based on max-tree.
Definition: DMorphoMaxTreeCriteria.hpp:197
Definition: DMorphoMaxTreeCriteria.hpp:255
Definition: DMorphoMaxTreeCriteria.hpp:259