Loading...
Searching...
No Matches
FunctorPack.h
1/*
2
3MIT License
4
5Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30*/
31#ifndef FUNCTOR_PACK_H_
32#define FUNCTOR_PACK_H_
33
34#include <cmath>
35
36#include "Functor.h"
37
38
39namespace drain
40{
41
42
43
44
46
56// Inversely: f = (f'-offset)/scale = a*f+b, where a=1/scale and b=-offset/scale.
58
59public:
60
61 ScalingFunctor(double scale = 1.0, double bias = 0.0) : UnaryFunctor(__FUNCTION__, "Rescales values linerarly: y = scale*x + bias", scale, bias){
62 this->getParameters().link("scale", this->scale);
63 this->getParameters().link("bias", this->bias);
64 this->setScale(scale, bias);
65 };
66
67 ScalingFunctor(const ScalingFunctor & ftor) : UnaryFunctor(ftor) {
68 this->parameters.copyStruct(ftor.getParameters(), ftor, *this);
69 updateBean();
70 }
71
72 //virtual
73 inline
74 double operator()(double s) const {
75 return this->scaleFinal*s + this->biasFinal;
76 };
77
78protected:
79
80 ScalingFunctor(const std::string & name, const std::string & description, double scale = 1.0, double bias = 0.0) :
81 UnaryFunctor(name, description, scale, bias){
82 this->setScale(scale, bias);
83 };
84
85};
86
87
88
90
98
99public:
100
101 NegateFunctor() : ScalingFunctor(__FUNCTION__, "Inverts values.", -1.0, 1.0) {
102 //updateScale(); // needed?
103 }
104
105
106
107};
108
110
118
119public:
120
121 RemappingFunctor(double fromValue = 0.0, double toValue = 0.0) : UnaryFunctor(__FUNCTION__, "Rescales intensities linerarly") , fromValue(fromValue), toValue(toValue) {
122 this->getParameters().link("fromValue", this->fromValue = fromValue);
123 this->getParameters().link("toValue", this->toValue = toValue);
124 };
125
126 RemappingFunctor(const RemappingFunctor & ftor) : UnaryFunctor(ftor){
127 parameters.copyStruct(ftor.parameters, ftor, *this);
128 }
129
130
131 inline
132 double operator()(double s) const {
133 // this->scale*s + this->bias;
134 if (s == fromValue)
135 return toValue;
136 else
137 return s;
138 };
139
140 double fromValue = 0.0;
141 double toValue = 0.0;
142
143};
144
145
147
155
156public:
157
158 ThresholdFunctor(double threshold = 0.5, double replace = 0.0) : UnaryFunctor(__FUNCTION__, "Resets values lower than a threshold") , threshold(threshold), replace(replace) {
159 this->getParameters().link("threshold", this->threshold = threshold);
160 this->getParameters().link("replace", this->replace = replace);
161 };
162
163 ThresholdFunctor(const ThresholdFunctor & ftor) : UnaryFunctor(ftor){
164 parameters.copyStruct(ftor.parameters, ftor, *this);
165 }
166
167
168 inline
169 double operator()(double s) const {
170 if (s < threshold)
171 return replace;
172 else
173 return s;
174 };
175
176 double threshold = 0.5;
177 double replace = 0.0;
178
179};
180
182
189class BinaryThresholdFunctor : public UnaryFunctor { // : public ThresholdFunctor {
190
191public:
192
193 /*
194 BinaryThresholdFunctor(double threshold = 0.5, double replace = 0.0, double replaceHigh = 1.0) : ThresholdFunctor(threshold, replace), replaceHigh(replaceHigh) {
195 this->getParameters().link("replaceHigh", this->replaceHigh = replaceHigh);
196 };
197 */
198
199 BinaryThresholdFunctor(double threshold = 0.5, double replace = 0.0, double replaceHigh = 1.0) : UnaryFunctor(__FUNCTION__, "Resets values lower and higher than a threshold") {
200 //, threshold(threshold), replace(replace)
201 this->getParameters().link("threshold", this->threshold = threshold);
202 this->getParameters().link("replace", this->replace = replace);
203 this->getParameters().link("replaceHigh", this->replaceHigh = replaceHigh);
204 };
205
207 parameters.copyStruct(ftor.parameters, ftor, *this);
208 }
209
210 inline
211 double operator()(double s) const {
212 if (s < threshold)
213 return replace;
214 else
215 return replaceHigh;
216 };
217
218 double threshold = 0.5;
219 double replace = 0.0;
220 double replaceHigh = 1.0;
221
222};
223
224
225
227
253{
254
255public:
256
257 GammaFunctor(double gamma = 1.0) : UnaryFunctor(__FUNCTION__, "Gamma correction for brightness."){
258 this->getParameters().link("gamma", this->gamma = gamma, "0.0...");
259 };
260
261 GammaFunctor(const GammaFunctor & ftor) : UnaryFunctor(ftor){
262 this->getParameters().link("gamma", this->gamma = ftor.gamma, "0.0..");
263 //this->getParameters().copyStruct(ftor.getParameters(), ftor, *this);
264 };
265
266 //virtual
267 inline
268 double operator()(double s) const {
269 return this->scale * pow(s, 1.0/gamma);
270 };
271
272 double gamma = 1.0;
273
274
275};
276
277
279
291
292public:
293
294 AdditionFunctor(double scale = 1.0, double bias = 0.0) : BinaryFunctor(__FUNCTION__, "Adds values", scale, bias){
295 this->getParameters().link("scale", this->scale);
296 this->getParameters().link("bias", this->bias);
297 };
298
299 inline
300 double operator()(double s1, double s2) const {
301 return this->scaleFinal*(s1 + s2) + this->biasFinal;
302 };
303};
304
305
307
316
317public:
318
319 SubtractionFunctor(double scale = 1.0, double bias = 0.0) : BinaryFunctor(__FUNCTION__, "Subtracts values", scale, bias){ // , bool LIMIT=false
320 this->getParameters().link("scale", this->scale);
321 this->getParameters().link("bias", this->bias);
322 };
323
324 inline
325 double operator()(double s1, double s2) const {
326 return this->scaleFinal*(s1 - s2) + this->biasFinal;
327 };
328};
329
330
331
333
354
355public:
356
357 MultiplicationFunctor(double scale = 1.0, double bias = 0.0) : BinaryFunctor(__FUNCTION__, "Rescales intensities linerarly", scale, bias){
358 this->getParameters().link("scale", this->scale);
359 this->getParameters().link("bias", this->bias);
360 // updateBean();
361 };
362
363 inline
364 double operator()(double s1, double s2) const {
365 return this->scaleFinal*(s1*s2) + this->biasFinal;
366 };
367};
368
370
378
379public:
380
381 DivisionFunctor(double scale = 1.0, double bias = 0.0) : BinaryFunctor(__FUNCTION__, "Rescales intensities linerarly", scale, bias){
382 this->getParameters().link("scale", this->scale);
383 this->getParameters().link("bias", this->bias);
384 //updateBean();
385 };
386
387 inline
388 double operator()(double s1, double s2) const {
389 if (s2 != 0.0)
390 return this->scale*(s1/s2) + this->bias;
391 else
392 return NAN;
393 };
394};
395
396
398
423
424public:
425
426 MixerFunctor(double coeff=0.5, double scale=1.0, double bias=0.0) : BinaryFunctor(__FUNCTION__, "Rescales intensities linerarly", scale, bias), coeff(coeff), scaleFinal2(1.0){
427 this->getParameters().link("coeff", this->coeff);
428 this->getParameters().link("scale", this->scale);
429 this->getParameters().link("bias", this->bias);
430 updateBean();
431 //updateScale();
432 };
433
434 MixerFunctor(const MixerFunctor & ftor) : BinaryFunctor(ftor){
435 parameters.copyStruct(ftor.parameters, ftor, *this); // ~coeff
436 //std::cerr << "copy const" << __FUNCTION__ << std::endl;
437 }
438
439
440 inline
441 double operator()(double s1, double s2) const {
442 return this->scaleFinal*s1 + this->scaleFinal2*s2 + this->biasFinal;
443 };
444
445 inline
446 virtual
447 void updateScale() const {
448 this->scaleFinal = this->scale*this->coeff;
449 this->scaleFinal2 = this->scale*(1.0-this->coeff);
450 this->biasFinal = this->bias;
451 }
452
453 double coeff;
454
455protected:
456 mutable
457 double scaleFinal2;
458};
459
460
461
462
464
473
474public:
475
476 MaximumFunctor(double scale = 1.0, double bias = 0.0) : BinaryFunctor(__FUNCTION__, "Maximum of two values.", scale, bias){
477 updateBean();
478 };
479
480 inline
481 double operator()(double s1, double s2) const {
482 return this->scale * std::max(static_cast<double>(s1), static_cast<double>(s2)) + this->bias;
483 };
484};
485
486
487
489
498
499public:
500
501 MinimumFunctor(double scale = 1.0, double bias = 0.0) : BinaryFunctor(__FUNCTION__, "Minimum of two values.", scale, bias){
502 updateBean();
503 };
504
505 inline
506 double operator()(double s1, double s2) const {
507 return this->scale * std::min(static_cast<double>(s1), static_cast<double>(s2)) + this->bias;
508 };
509};
510
511
512
513
514}
515
516#endif /* MATH_OP_PACK */
517
518// Drain
Adds a intensity values .
Definition FunctorPack.h:290
Definition Functor.h:135
Thresholds intensity values.
Definition FunctorPack.h:189
Divides image by another image.
Definition FunctorPack.h:377
double scale
Relative scale, typically 1. Optional.
Definition Functor.h:99
double scaleFinal
Scaling factor after encodings of src and dst images are known.
Definition Functor.h:106
double bias
"Relative" bias, typically 0. Optional.
Definition Functor.h:102
virtual void updateBean() const override
Called after setParameters()
Definition Functor.h:65
double biasFinal
Scaling factor after encodings of src and dst images are known.
Definition Functor.h:110
Gamma correction. Intensity is mapped as f' = f^(gamma)
Definition FunctorPack.h:253
Definition FunctorPack.h:472
Minimum intensity. Prescaled input.
Definition FunctorPack.h:497
Blends an image to another with given proportion.
Definition FunctorPack.h:422
Multiplies two images, with optional post scaling and offset.
Definition FunctorPack.h:353
Inverts intensities: f' = f_max - f.
Definition FunctorPack.h:97
void copyStruct(const ReferenceMap &m, const T &src, T &dst, extLinkPolicy policy=RESERVE)
Experimental. Copies references and values of a structure to another.
Definition ReferenceMap.h:399
Maps a single intensity value to another value.
Definition FunctorPack.h:117
Rescales intensities linearly: f' = scale*f + offset.
Definition FunctorPack.h:57
Subtracts image from another image.
Definition FunctorPack.h:315
Thresholds intensity values.
Definition FunctorPack.h:154
Definition DataSelector.cpp:1277
Definition Functor.h:116