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
157
158public:
159
160 ThresholdFunctor(double threshold = 0.5, double replace = 0.0) : UnaryFunctor(__FUNCTION__, "Resets values lower than a threshold") , threshold(threshold), replace(replace) {
161 this->getParameters().link("threshold", this->threshold = threshold);
162 this->getParameters().link("replace", this->replace = replace);
163 };
164
165 ThresholdFunctor(const ThresholdFunctor & ftor) : UnaryFunctor(ftor){
166 parameters.copyStruct(ftor.parameters, ftor, *this);
167 }
168
169
170 virtual inline
171 double operator()(double s) const override {
172 if (s < threshold)
173 return replace;
174 else
175 return s;
176 };
177
178 double threshold = 0.5;
179 double replace = 0.0;
180
181};
182
184
194class BinaryThresholdFunctor : public UnaryFunctor { // : public ThresholdFunctor {
195
196public:
197
198 BinaryThresholdFunctor(double threshold = 0.5, double replaceLow = 0.0, double replaceHigh = 1.0) : UnaryFunctor(__FUNCTION__, "Resets values lower and higher than a threshold") {
199 this->getParameters().link("threshold", this->threshold.tuple(threshold,threshold), "min[:max]").fillArray = true;
200 this->getParameters().link("replace", this->replace.tuple(replaceLow, replaceHigh), "min[:max]");
201 };
202
204 parameters.copyStruct(ftor.parameters, ftor, *this);
205 }
206
207 virtual inline
208 double operator()(double s) const override {
209 if (s < threshold.min)
210 return replace.min;
211 else if (s > threshold.max)
212 return replace.max;
213 else
214 return s;
215 };
216
217 Range<double> threshold = {0.5, 0.5};
218 Range<double> replace = {0.0, 1.0};
219
220};
221
222
223
225
251{
252
253public:
254
255 GammaFunctor(double gamma = 1.0) : UnaryFunctor(__FUNCTION__, "Gamma correction for brightness."){
256 this->getParameters().link("gamma", this->gamma = gamma, "0.0...");
257 };
258
259 GammaFunctor(const GammaFunctor & ftor) : UnaryFunctor(ftor){
260 this->getParameters().link("gamma", this->gamma = ftor.gamma, "0.0..");
261 //this->getParameters().copyStruct(ftor.getParameters(), ftor, *this);
262 };
263
264 //virtual
265 inline
266 double operator()(double s) const {
267 return this->scale * pow(s, 1.0/gamma);
268 };
269
270 double gamma = 1.0;
271
272};
273
274
276
288
289public:
290
291 AdditionFunctor(double scale = 1.0, double bias = 0.0) : BinaryFunctor(__FUNCTION__, "Adds values", scale, bias){
292 this->getParameters().link("scale", this->scale);
293 this->getParameters().link("bias", this->bias);
294 };
295
296 inline
297 double operator()(double s1, double s2) const {
298 return this->scaleFinal*(s1 + s2) + this->biasFinal;
299 };
300};
301
302
304
313
314public:
315
316 SubtractionFunctor(double scale = 1.0, double bias = 0.0) : BinaryFunctor(__FUNCTION__, "Subtracts values", scale, bias){ // , bool LIMIT=false
317 this->getParameters().link("scale", this->scale);
318 this->getParameters().link("bias", this->bias);
319 };
320
321 inline
322 double operator()(double s1, double s2) const {
323 return this->scaleFinal*(s1 - s2) + this->biasFinal;
324 };
325};
326
327
328
330
351
352public:
353
354 MultiplicationFunctor(double scale = 1.0, double bias = 0.0) : BinaryFunctor(__FUNCTION__, "Rescales intensities linerarly", scale, bias){
355 this->getParameters().link("scale", this->scale);
356 this->getParameters().link("bias", this->bias);
357 // updateBean();
358 };
359
360 inline
361 double operator()(double s1, double s2) const {
362 return this->scaleFinal*(s1*s2) + this->biasFinal;
363 };
364};
365
367
375
376public:
377
378 DivisionFunctor(double scale = 1.0, double bias = 0.0) : BinaryFunctor(__FUNCTION__, "Rescales intensities linerarly", scale, bias){
379 this->getParameters().link("scale", this->scale);
380 this->getParameters().link("bias", this->bias);
381 //updateBean();
382 };
383
384 inline
385 double operator()(double s1, double s2) const {
386 if (s2 != 0.0)
387 return this->scale*(s1/s2) + this->bias;
388 else
389 return NAN;
390 };
391};
392
393
395
420
421public:
422
423 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){
424 this->getParameters().link("coeff", this->coeff);
425 this->getParameters().link("scale", this->scale);
426 this->getParameters().link("bias", this->bias);
427 updateBean();
428 //updateScale();
429 };
430
431 MixerFunctor(const MixerFunctor & ftor) : BinaryFunctor(ftor){
432 parameters.copyStruct(ftor.parameters, ftor, *this); // ~coeff
433 //std::cerr << "copy const" << __FUNCTION__ << std::endl;
434 }
435
436
437 inline
438 double operator()(double s1, double s2) const {
439 return this->scaleFinal*s1 + this->scaleFinal2*s2 + this->biasFinal;
440 };
441
442 inline
443 virtual
444 void updateScale() const {
445 this->scaleFinal = this->scale*this->coeff;
446 this->scaleFinal2 = this->scale*(1.0-this->coeff);
447 this->biasFinal = this->bias;
448 }
449
450 double coeff = 1.0;
451
452protected:
453
454 mutable
455 double scaleFinal2 = 1.0;
456
457};
458
459
460
461
463
472
473public:
474
475 MaximumFunctor(double scale = 1.0, double bias = 0.0) : BinaryFunctor(__FUNCTION__, "Maximum of two values.", scale, bias){
476 updateBean();
477 };
478
479 inline
480 double operator()(double s1, double s2) const {
481 return this->scale * std::max(static_cast<double>(s1), static_cast<double>(s2)) + this->bias;
482 };
483};
484
485
486
488
497
498public:
499
500 MinimumFunctor(double scale = 1.0, double bias = 0.0) : BinaryFunctor(__FUNCTION__, "Minimum of two values.", scale, bias){
501 updateBean();
502 };
503
504 inline
505 double operator()(double s1, double s2) const {
506 return this->scale * std::min(static_cast<double>(s1), static_cast<double>(s2)) + this->bias;
507 };
508};
509
510
511
512
513}
514
515#endif /* MATH_OP_PACK */
516
517// Drain
Adds a intensity values .
Definition FunctorPack.h:287
Definition Functor.h:135
Thresholds intensity values.
Definition FunctorPack.h:194
Divides image by another image.
Definition FunctorPack.h:374
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:251
Definition FunctorPack.h:471
Minimum intensity. Prescaled input.
Definition FunctorPack.h:496
Blends an image to another with given proportion.
Definition FunctorPack.h:419
Multiplies two images, with optional post scaling and offset.
Definition FunctorPack.h:350
Inverts intensities: f' = f_max - f.
Definition FunctorPack.h:97
Definition Range.h:52
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:407
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:312
Thresholds intensity values.
Definition FunctorPack.h:156
Definition DataSelector.cpp:1277
Definition Functor.h:116