ImageOpBank.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_IMAGE_OP_BANK_H_
32 #define DRAIN_IMAGE_OP_BANK_H_
33 
34 //
35 #include <iostream>
36 #include <map>
37 
38 
39 #include <drain/RegExp.h>
40 
41 #include "drain/util/Bank.h"
42 #include "drain/util/FunctorPack.h"
43 
44 #include "ImageOp.h"
45 #include "FunctorOp.h"
46 
47 #include "drain/prog/Command.h"
48 #include "drain/prog/CommandBank.h"
49 
50 #include "drain/util/Cloner.h"
51 #include "drain/util/Fuzzy.h"
52 
53 
54 
55 
56 #include "GammaOp.h"
57 #include "RunLengthOp.h"
58 
59 #include "BlenderOp.h"
60 
61 #include "CatenatorOp.h"
62 #include "CopyOp.h"
63 #include "CropOp.h"
64 #include "DistanceTransformFillOp.h"
65 #include "DistanceTransformOp.h"
66 #include "FastAverageOp.h"
67 //#include "FastOpticalFlowOp.h"
68 #include "FlowAverageWindowOp.h" // for SlidingWindowOp
69 #include "FloodFillOp.h"
70 #include "FunctorOp.h"
71 #include "GammaOp.h"
72 #include "GaussianAverageOp.h"
73 #include "DifferentialOp.h"
74 #include "HighBoostOp.h"
75 #include "HighPassOp.h"
76 #include "ImpulseAvgOp.h"
77 #include "MarginalStatisticOp.h"
78 //#include "MotionExtrapolatorOp.h"
79 //#include "MotionIllustratorOp.h"
80 #include "MultiThresholdOp.h"
81 #include "PaletteOp.h"
82 #include "PixelVectorOp.h"
83 #include "QualityMixerOp.h"
84 #include "QualityOverrideOp.h"
85 #include "QuantizatorOp.h"
86 #include "ResizeOp.h"
87 #include "RunLengthOp.h"
88 #include "SegmentAreaOp.h"
89 #include "SegmentStatisticsOp.h"
90 #include "SlidingWindowHistogramOp.h"
91 #include "SlidingWindowMedianOp.h"
92 #include "SlidingWindowOp.h"
93 #include "TransposeOp.h"
94 
95 //#include "drain/image/SegmentProber.h"
96 #include "SuperProberOp.h"
97 
98 
99 namespace drain
100 {
101 
102 namespace image
103 {
104 
105 template <class T>
106 class ImageOpCloner : public Cloner<ImageOp,T> {
107 public:
108 
109  static
110  const std::string & getName(){
111  static T op;
112  return op.getName();
113  }
114 
115 };
116 
117 template <class F>
118 class UnaryFunctorOpCloner : public ImageOpCloner<UnaryFunctorOp<F> > {
119  public:
120 };
121 
122 
123 template <class F>
124 class BinaryFunctorOpCloner : public ImageOpCloner<BinaryFunctorOp<F> > {
125  public:
126 };
127 
128 
129 class NegateOp : public ImageOp {
130 
131 public:
132 
133  NegateOp() : ImageOp(__FUNCTION__, "Invert values (unsigned char or unsigned short int)"){
134  }
135 
136  virtual inline
137  void traverseChannels(const ImageTray<const Channel> & src, ImageTray<Channel> & dst) const {
138  traverseChannelsEqually(src, dst);
139  }
140 
141 
143  virtual inline
144  void traverseChannel(const Channel & src, Channel & dst) const {
145 
146  double srcMax = src.getConf().getTypeMax<double>();
147  double dstMax = dst.getConf().getTypeMax<double>();
148  double rescale = dstMax/srcMax;
149 
150  Channel::const_iterator s = src.begin();
151  Channel::iterator d = dst.begin();
152 
153  while (d != dst.end()){
154  *d = dstMax - rescale*static_cast<double>(*s);
155  ++s;
156  ++d;
157  }
158 
159  };
160 
161 
162 
163 };
164 
165 
166 class ImageOpBank : public Bank<ImageOp> {
167 
168 public:
169 
170  //CommandBank & cmdBank;
171 
172  //ImageOpBank(CommandBank & cmdBank) : cmdBank(cmdBank) {
173  ImageOpBank() {
174  };
175 
176  virtual
177  ~ImageOpBank(){}
178 
180 
183  template <class OP>
184  OP & install(const std::string & name = OP().getName()){
185 
186  drain::Logger mout(getImgLog(), __FILE__, __FUNCTION__);
187 
188  std::string key(name);
190  mout.warn() << name << ">" << OP().getName() << '>' << key << '\n';
191  return Bank<ImageOp>::add<OP>(key);
192  }
193 
195  ImageOp & getComplete(const std::string & query, char separator=',', char assign='=', const drain::SmartMap<std::string> & aliasMap = drain::SmartMap<std::string>());
196 
198  // Palette palette;
199 
200 };
201 
202 
203 
204 std::ostream & operator<<(std::ostream & ostr, const ImageOpBank & bank);
205 
206 
207 
208 // consider separate
209 extern
211 
212 template <class T>
213 void installImageOps(T & installer) {
214 
215 
216  try {
217  //installer.install<CmdHistogram>();
218  installer.template install<NegateOp>();
219  installer.template install<UnaryFunctorOp<ScalingFunctor> >("Rescale");
220  //installer.template install<UnaryFunctorOp<NegateFunctor> >();
221  installer.template install<UnaryFunctorOp<RemappingFunctor> >("Remap");
222  installer.template install<UnaryFunctorOp<ThresholdFunctor> >();
223  installer.template install<UnaryFunctorOp<BinaryThresholdFunctor> >("ThresholdBinary");
224 
225 
227  installer.template install<BinaryFunctorOp<AdditionFunctor> >("Add");
228  installer.template install<BinaryFunctorOp<DivisionFunctor> >("Div");
229  installer.template install<BinaryFunctorOp<MaximumFunctor> >("Max");
230  installer.template install<BinaryFunctorOp<MinimumFunctor> >("Min");
231  installer.template install<BinaryFunctorOp<MixerFunctor> >("Mix");
232  installer.template install<BinaryFunctorOp<MultiplicationFunctor> >("Mul");
233  installer.template install<BinaryFunctorOp<SubtractionFunctor> >("Sub");
234 
235 
237  installer.template install<UnaryFunctorOp<FuzzyBell<double>,true> >();
238  installer.template install<UnaryFunctorOp<FuzzyBell2<double>,true> >();
239  installer.template install<UnaryFunctorOp<FuzzyStep<double>,true> >();
240  installer.template install<UnaryFunctorOp<FuzzyStepsoid<double>,true> >();
241  installer.template install<UnaryFunctorOp<FuzzyTriangle<double>,true> >();
242  installer.template install<UnaryFunctorOp<FuzzyTwinPeaks<double>,true> >();
243 
244 
245  installer.template install<ChannelCatenatorOp>("Catenate");
246 
247  installer.template install<CopyOp>();
248  installer.template install<CropOp>();
249 
250  installer.template install<DistanceTransformLinearOp>("DistanceTransform"); // consider rename op
251  installer.template install<DistanceTransformExponentialOp>("DistanceTransformExp"); // consider rename op
252  installer.template install<DistanceTransformFillLinearOp>("DistanceTransformFill"); // consider rename op
253  installer.template install<DistanceTransformFillExponentialOp>("DistanceTransformFillExp"); // consider rename op
254 
255  installer.template install<FastAverageOp>("Average");
256 
257  installer.template install<FlowAverageOp>();
258 
259  installer.template install<BlenderOp>();
260 
261  installer.template install<ImpulseResponseOp<ImpulseAvg> >("impulseAvg"); // consider getName -> avg.getName()
262 
263 
264  //static ImageOpCloner<FastOpticalFlowOp> fastopticalflow;
265  //bank.template install< >(fastopticalflow);
266 
267  installer.template install<FloodFillOp>();
268 
269  // static ImageOpCloner<FunctorOp> functor;
270  installer.template install<UnaryFunctorOp<GammaFunctor,true> >();
271 
272  //installer.template install<QuantizatorOp>();
273  installer.template install<UnaryFunctorOp<QuantizatorFunctor> >();
274 
275  installer.template install<GaussianAverageOp>();
276 
278  installer.template install<GradientOp>();
279  installer.template install<GradientHorzOp>();
280  installer.template install<GradientVertOp>();
281  installer.template install<LaplaceOp>();
282  installer.template install<LaplaceHorzOp>();
283  installer.template install<LaplaceVertOp>();
284 
285 
286  installer.template install<HighBoostOp>();
287 
288  installer.template install<HighPassOp>();
289 
290  installer.template install<MarginalStatisticOp>("marginStat"); // consider rename op
291 
292  //static ImageOpCloner<MotionExtrapolatorOp> motionextrapolator;
293  //static ImageOpCloner<MotionIllustratorOp> motionillustrator;
294  //static ImageOpCloner<PaletteOp> palette;
295 
296  //static ImageOpCloner<PixelVectorOp> pixelvector;
297  //bank.template install< >(pixelvector, "pixelvector");
298  installer.template install<GrayOp>();
299  installer.template install<DistanceOp>();
300  installer.template install<ProductOp>();
301  installer.template install<MagnitudeOp>();
302 
303  installer.template install<MultiThresholdOp>();
304 
305  //static ImageOpCloner<Distance2Op> distance2;
306  //bank.template install< >(distance2);
307 
308  // installer.template install<QuadraticSmootherOp>();
309  // static ImageOpCloner<QuadraticSmootherOp> quadraticSmoother;
310  // bank.template install< >(quadraticSmoother);
311  installer.template install<QualityThresholdOp>();
312 
313  installer.template install<QualityMixerOp>();
314 
315  installer.template install<QualityOverrideOp>();
316 
317 
318  installer.template install<ResizeOp>();
319 
320  //static ImageOpCloner<RecursiveRepairerOp> recursiveRepairer;
321  //bank.template install< >(recursiveRepairer);
322 
323  installer.template install<RunLengthHorzOp>();
324  installer.template install<RunLengthVertOp>();
325 
326  installer.template install<SegmentAreaOp<float,unsigned short> >();
327  installer.template install<SegmentStatisticsOp>("segmentStats");
328 
329  // Experimental
330  installer.template install<SuperProberOp>();
331 
332 
333  installer.template install<SlidingWindowHistogramOp>("windowHistogram");
334  installer.template install<SlidingWindowMedianOp>("median");
335  installer.template install<TransposeOp>();
336 
337  //static ImageOpCloner<ImageOp> image;
338  //static ImageOpCloner<SequentialImageOp> sequentialimage;
339  //bank.template install< >(sequentialimage, "sequentialimage");
340  //static ImageOpCloner<SlidingWindowOp> slidingWindow;
341  //bank.template install< >(slidingWindow);
342  //static ImageOpCloner<WindowOp> window;
343  //bank.install< >(window, "window");
344 
345  } catch (std::exception & e) {
346  std::cerr << "Bank " << e.what() << std::endl;
347  }
348 
349 
350 }
351 
352 
353 }
354 
355 }
356 
357 #endif
358 
359 // Drain
Definition: Bank.h:64
Definition: CastableIterator.h:57
static void deriveCmdName(std::string &name, char prefix=0)
Given a command class name like MyFileReadCommand, derives a respective command line option ("myFileR...
Definition: CommandBank.cpp:62
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition: Log.h:308
Logger & warn(const TT &... args)
Possible error, but execution can continue.
Definition: Log.h:426
A base class for smart maps providing methods for importing and exporting values, among others.
Definition: SmartMap.h:66
Definition: ImageOpBank.h:124
Image with static geometry.
Definition: ImageChannel.h:60
T getTypeMax() const
Returns the maximum value supported by the current storage type.
Definition: ImageConf.h:281
const iterator & begin()
Returns iterator pointing to the first image element.
Definition: ImageFrame.h:119
const iterator & end()
Returns the iterator pointing to the element after the last element of the image buffer.
Definition: ImageFrame.h:125
Definition: ImageOpBank.h:166
ImageOp & getComplete(const std::string &query, char separator=',', char assign='=', const drain::SmartMap< std::string > &aliasMap=drain::SmartMap< std::string >())
Supports querying operator with parameters set, eg. gaussianAverage,width=10,height=5.
Definition: ImageOpBank.cpp:43
OP & install(const std::string &name=OP().getName())
Add ImageOp command to registry (CommandBank).
Definition: ImageOpBank.h:184
Definition: ImageOpBank.h:106
Base class for image processing functions.
Definition: ImageOp.h:49
void traverseChannelsEqually(const ImageTray< const Channel > &src, ImageTray< Channel > &dst) const
Calls processWithTemp() if the frames overlap.
Definition: ImageOp.cpp:414
ImageOp(const std::string &name=__FUNCTION__, const std::string &description="")
Definition: ImageOp.h:156
Container applicable for Channels and Images, with alpha support.
Definition: ImageTray.h:267
Definition: ImageOpBank.h:129
virtual void traverseChannel(const Channel &src, Channel &dst) const
Apply to single channel.
Definition: ImageOpBank.h:144
Definition: ImageOpBank.h:118
void installImageOps(T &installer)
Definition: ImageOpBank.h:213
ImageOpBank & getImageOpBank()
Definition: ImageOpBank.cpp:124
Definition: DataSelector.cpp:1277
Wrapper for derived class S, returning base class T.
Definition: Cloner.h:117