SegmentOp.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30 */
31 #ifndef DRAIN_SEGMENT_OP_H
32 #define DRAIN_SEGMENT_OP_H
33 
34 #include <math.h>
35 
36 //#include "drain/util/Cloner.h"
37 
38 #include "drain/util/FunctorBank.h"
39 #include "drain/image/SegmentProber.h"
40 
41 #include "ImageOp.h"
42 //#include "FloodFillOp.h"
43 
44 namespace drain
45 {
46 namespace image
47 {
48 
50 
53 class SegmentOp: public ImageOp {
54 
55 public:
56 
57  Range<double> intensity;
58  // double min; // TODO: instead consider criterion method inside(i,j)
59  // double max;
60 
61  bool clearDst;
62 
63  std::string functorStr; // consider internal split util
64 
65 
66 protected:
67 
68  inline
69  SegmentOp(const std::string & name, const std::string & description) :
70  ImageOp(name, description),
71  intensity(0,0),
72  clearDst(true),
73  unicloner(getFunctorBank()),
74  functor(identityFtor)
75  {
76  }
77 
78  SegmentOp(const std::string & name, const std::string & description, const drain::UnaryFunctor & ftor) :
79  ImageOp(name, description + " ftor=" + ftor.getName()),
80  intensity(0,0),
81  clearDst(true),
82  unicloner(getFunctorBank()),
83  functor(ftor) {
84  }
85 
86  mutable
87  UniCloner<UnaryFunctor> unicloner;
88 
90 
94  inline
95  const UnaryFunctor & getFunctor(double scale = 0.0, double bias = 0.0) const {
96 
97  // Consider: this could be generalized? Or unicloner needs scope?
98 
99  drain::Logger mout(getImgLog(), __FILE__, __FUNCTION__);
100 
101  if (functorStr.empty()){
102  // Use my functor.
103  return this->functor;
104  }
105  else {
106  // Override with a cloned functor instance.
107  std::string functorName;
108  std::string functorParams;
109  drain::StringTools::split2(this->functorStr, functorName, functorParams, ":");
110  if (unicloner.bank.has(functorName)){
111  UnaryFunctor & f = unicloner.getCloned(functorName); // drain::getFunctor(functorStr, ':');
112  if (scale > 0.0){
113  f.setScale(scale, bias);
114  }
115  f.setParameters(functorParams,'=',':');
116  mout.debug3(" ftor: " , f );
117  // mout.warn("cftor: " , (const UnaryFunctor &)f );
118  return f;
119  }
120  else {
121  mout.fail("Functor '" , functorName , "' not found. Available functors:" );
122  //std::cerr << unicloner.bank
123  mout.note("Available functors:\n" , unicloner.bank );
124  //static const SprinterLayout layout("\n", "<?>", "\0=;", "''");
125  //Sprinter::sequenceToStream(std::cerr, unicloner.bank, layout);
126  mout.warn("using default ftor: " , this->functor );
127  return this->functor;
128  }
129  }
130  }
131 
132 protected:
133 
134  const drain::IdentityFunctor identityFtor;
135  const drain::UnaryFunctor & functor;
136 
137 
138 
139 
140 };
141 
142 
143 } // namespace image
144 
145 } // namespace drain
146 
147 #endif
148 
149 // Drain
bool has(const K &key) const
Check if a cloner is defined for this key.
Definition: Bank.h:141
void setParameters(std::initializer_list< Variable::init_pair_t > args)
Grants access to (if above hidden)
Definition: BeanLike.h:131
virtual const std::string & getName() const
Return the name of an instance.
Definition: BeanLike.h:69
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition: Log.h:308
Logger & fail(const TT &... args)
Possible error, but execution can continue. Special type of Logger::warn().
Definition: Log.h:449
Logger & note(const TT &... args)
For top-level information.
Definition: Log.h:485
Logger & warn(const TT &... args)
Possible error, but execution can continue.
Definition: Log.h:426
static bool split2(const std::string &s, T1 &first, T2 &second, const C &separators, const std::string &trimChars=" \t\n")
Splits and trims a given std::string to a std Sequence.
Creates an entry of desired type and destroys it upon exit.
Definition: Bank.h:476
Base class for image processing functions.
Definition: ImageOp.h:49
ImageOp(const std::string &name=__FUNCTION__, const std::string &description="")
Definition: ImageOp.h:156
A base class for computing statistics of segments. As segment is an area of connected pixels.
Definition: SegmentOp.h:53
const UnaryFunctor & getFunctor(double scale=0.0, double bias=0.0) const
Retrieve functor to process the result: either the functor defined as functorStr or member functor (i...
Definition: SegmentOp.h:95
Definition: DataSelector.cpp:1277
FunctorBank & getFunctorBank()
Definition: FunctorBank.cpp:51
Definition: Functor.h:126
Definition: Functor.h:116