Loading...
Searching...
No Matches
GaussianWindow.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 GaussianWindow_H_
32#define GaussianWindow_H_
33
34#include "Window.h"
35//#include "WindowOp.h"
36//#include "SlidingWindow.h"
37
38namespace drain
39{
40
41namespace image
42{
43
46
47public:
48
49 double radius;
50
51};
52
54
58template <bool DIR=true, class R=WindowCore>
59class GaussianStripe : public Window<GaussianWindowConf,R> { // consider scaleWeight
60
61public:
62
63 typedef float value_t;
64
65 GaussianStripe(int n, double radius = 1.0) : Window<GaussianWindowConf,R>(DIR?n:1, DIR?1:n), weightSum(0), value(0), scaleResult(1.0) {
66 this->conf.radius = radius;
67 w = 0.0;
68 sumW = 0.0;
69 };
70
71 virtual
73
74 virtual inline
75 void setImageLimits() const {
76 this->coordinateHandler.set(this->src.getConf());
77 //this->src.adjustCoordinateHandler(this->coordinateHandler);
78 }
79
80
81 void initialize(){
82
83 drain::Logger mout(getImgLog(), "GaussianStripe", __FUNCTION__);
84
85 this->setImageLimits();
86 this->setLoopLimits();
87
88 //const int n = this->conf.width*this->conf.height; // width*1 or 1*height
89 // (DIR?this->conf.width:this->conf.height);
90 const int n = std::max(this->conf.frame.width, this->conf.frame.height); // width*1 or 1*height
91 const int bias = std::min(this->iRange.min, this->jRange.min);
92 lookUp.resize(n);
93 //weightSum = 0.0;
94 value_t f;
95 const double radiusAbs = this->conf.radius * static_cast<value_t>(n)*0.5;
96 const double radiusAbs2 = radiusAbs*radiusAbs;
97 if (radiusAbs <= 0.0){
98 mout.error("Zero radius2: " , radiusAbs );
99 }
100
101 int iNorm;
102 mout.debug("frame:" , this->conf.frame );
103 for (int i = 0; i < n; ++i) {
104 iNorm = (i + bias);
105 f = exp2(-static_cast<double>(iNorm*iNorm) / radiusAbs2);
106 lookUp[i] = f;
107 weightSum += f;
108 mout.debug(i , '\t' , iNorm , '\t' , f , '\t' , weightSum );
109 }
110 mout.debug("weightSum = " , weightSum );
111
112 scaleResult = this->src.getScaling().getScale() / this->dst.getScaling().getScale();
113 mout.debug("scale = " , scaleResult );
114
115 //this->coordinateHandler.setLimits(srcWidth, srcHeight);
116 //this->coordinateHandler.setLimits(this->src.getWidth(), this->src.getHeight());
117
118 }
119
120
121 // Copied from sliding window
122 void setSize(size_t width = 1){
124 }
125
126protected:
127
128 virtual
129 void setSize(size_t width, size_t height){
130 drain::Logger mout(getImgLog(), "SlidingStripe", __FUNCTION__);
131 if (height > 1)
132 mout.warn("horz stripe, height(" , height , ") discarded" );
134 }
135
136
137protected:
138
139 std::vector<value_t> lookUp;
140
141
142 virtual
143 void write() {
144 this->dst.put(this->location, value);
145 };
146
147protected:
148
149 // Truncated sum of the gaussian weights (typically close to 1.0)
150 value_t weightSum;
151
152 mutable
153 value_t value;
154
155 value_t scaleResult;
156 // value_t scaleResultWeight; // consider!
157
158 virtual
159 void update(); // = 0;
160
161 mutable
162 Point2D<int> locationTmp;
163
164 // Weighted version only:
166 value_t w;
168 value_t sumW;
169
170};
171
172
174
178template <bool DIR=true>
179class GaussianStripeWeighted : public GaussianStripe<DIR, WeightedWindowCore> {
180
181public:
182
183 GaussianStripeWeighted(int n, double radius=1.0) : GaussianStripe<DIR, WeightedWindowCore>(n, radius) {
184 };
185
186protected:
187
188 virtual
189 inline
190 void write(){
191 this->dst.put(this->location, this->value);
192 this->dstWeight.put(this->location, this->weightSum);
193 };
194
195
196};
197
198
199} // image::
200
201} // drain::
202
203#endif
204
205// Drain
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:312
Logger & warn(const TT &... args)
Possible error, but execution can continue.
Definition Log.h:430
Logger & debug(const TT &... args)
Debug information.
Definition Log.h:666
Logger & error(const TT &... args)
Echoes.
Definition Log.h:416
double getScale() const
Returns the intensity scaling factor. See set setScale()
Definition ValueScaling.h:252
Definition GaussianWindow.h:179
virtual void write()
At each location, the result of computation to dst image(s).
Definition GaussianWindow.h:190
Definition GaussianWindow.h:59
virtual void setImageLimits() const
Sets internal limits corresponding to image geometries. Typically using coordHandler.
Definition GaussianWindow.h:75
virtual void write()
At each location, the result of computation to dst image(s).
Definition GaussianWindow.h:143
void initialize()
Definition GaussianWindow.h:81
virtual void setSize(size_t width, size_t height)
Sets the window size.
Definition GaussianWindow.h:129
value_t sumW
Current weighted sum.
Definition GaussianWindow.h:168
virtual void update()
At each location, this is called to calculate and store something in members.
value_t w
Current weight.
Definition GaussianWindow.h:166
Extends WindowConfig with radius.
Definition GaussianWindow.h:45
void put(size_t i, T x)
Sets the intensity in location i to x. See \address.
Definition ImageFrame.h:192
Base class for configurations applied in image processing windows, e.g. for operators of type WindowO...
Definition Window.h:56
Base class for windows applied by WindowOp's.
Definition Window.h:404
Point2D< int > location
Current location of this window.
Definition Window.h:523
virtual void setSize(size_t width, size_t height)
Sets the window size.
Definition Window.h:479
void setLoopLimits()
Sets the actual traversal range inside the window. Sometimes applied dynamically by reset().
Definition Window.h:608
Range< int > iRange
Studies source and destination images and decides whether scaling (SCALE=true) should be set.
Definition Window.h:584
Definition DataSelector.cpp:1277
Definition Point.h:48