FlowAverageWindowOp.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 FLOW_AVG_W_H_
32 #define FLOW_AVG_W_H_
33 
34 #include <drain/TypeUtils.h>
35 #include <algorithm>
36 
37 // #include "CopyOp.h"
38 // #include "Window.h"
39 //
40 #include "SlidingWindowOp.h"
41 
42 namespace drain
43 {
44 
45 namespace image
46 {
47 
49 
52 // TODO: redesign RadarWindowConfig<Polar> ? and
53 class FlowAverageWindow : public SlidingWindow<WindowConfig, MultiChannelWindowCore> {
54 
55 public:
56 
58 
59  FlowAverageWindow(int width=5, int height=0) : SlidingWindow<WindowConfig, MultiChannelWindowCore>(width, height){ //qualitySensitive(false)
60  clear();
61  };
62 
63  FlowAverageWindow(const WindowConfig & conf) : SlidingWindow<WindowConfig, MultiChannelWindowCore>(conf) { //qualitySensitive(false) {
64  clear();
65  };
66 
67  // ODIM odimSrc; future option.
68 
69  //bool qualitySensitive;
70 
71 protected:
72 
73  double sumMagnitude;
74  std::vector<double> sum;
75  /*
76  double sumU;
77  double sumV;
78  double sumU2;
79  double sumV2;
80  */
81  double sumW;
82  unsigned int count;
84  // drain::ValueScaling dstScaling
85 
86  void initialize(){
87 
88  drain::Logger mout(getImgLog(), "FlowAverageWindow", __FUNCTION__);
89 
90  mout.debug2("srcTray:\n" , srcTray );
91  mout.debug2("dstTray:\n" , dstTray );
92 
93  if (srcWeight.isEmpty())
94  mout.debug("no src alpha" );
95 
96  if (dstWeight.isEmpty())
97  mout.debug("no dst alpha" );
98 
99  if (!srcTray.checkGeometry()){
100  mout.special("scrTray geom: " , srcTray.getGeometry() );
101  mout.special("content geom: " , srcTray );
102  mout.error("srcTray geometry inconsistent" );
103  };
104 
105  if (!dstTray.checkGeometry()){
106  mout.special("dstTray geom: " , dstTray.getGeometry() );
107  mout.special("content geom: " , dstTray );
108  mout.error("dstTray geometry inconsistent" );
109  };
110 
111  setImageLimits();
112  setLoopLimits();
113  this->location.setLocation(0,0);
114 
115  limiter = this->dst.getConf().getLimiter<double>();
116 
117  sum.resize(dstTray.size(), 0.0);
118  fillBoth();
119  }
120 
122  void clear(){
123  std::fill(sum.begin(), sum.end(), 0);
124  sumMagnitude = 0.0;
125  sumW = 0.0;
126  count = 0;
127  };
128 
129  void setImageLimits() const {
130  this->coordinateHandler.set(this->src.getGeometry(), this->src.getCoordinatePolicy());
131  //src.adjustCoordinateHandler(this->coordinateHandler);
132  }
133 
134  virtual
135  void removePixel(Point2D<int> & p);
136 
137  virtual
138  void addPixel(Point2D<int> & p);
139 
140  virtual
141  void write();
142 
143 };
144 
146 
147 public:
148 
149  FlowAverageWindowWeighted(int width=5, int height=0) : FlowAverageWindow(width, height) {
150  };
151 
153  };
154 
155 
156  virtual
157  void removePixel(Point2D<int> & p);
158 
159  virtual
160  void addPixel(Point2D<int> & p);
161 
162  virtual
163  void write();
164 
165 };
166 
168 
171 class FlowAverageOp : public SlidingWindowOp<FlowAverageWindowWeighted> {
172 
173 public:
174 
175  FlowAverageOp() : SlidingWindowOp<FlowAverageWindowWeighted>(__FUNCTION__, "Window average that preserves the magnitude"){
176  };
177 
178  virtual inline
179  void traverseChannels(const ImageTray<const Channel> & src, ImageTray<Channel> & dst) const {
180  // drain::Logger mout(getImgLog(), __FUNCTION__,__FILE__); //REP (this->name+"[const ImageTray &, ImageTray &]", __FUNCTION__);
181  this->traverseMultiChannel(src, dst);
182  }
183 
184 };
185 
186 
187 
188 
189 
190 }
191 
192 }
193 
194 #endif // FLOW_AVG_W_H_
195 
196 // Drain
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition: Log.h:308
Logger & error(const TT &... args)
Echoes.
Definition: Log.h:412
Logger & special(const TT &... args)
Other useful information.
Definition: Log.h:527
Logger & debug(const TT &... args)
Public, yet typically used "internally", when TIMING=true.
Definition: Log.h:676
Logger & debug2(const TT &... args)
Debug information.
Definition: Log.h:686
Window average that preserves the magnitude.
Definition: FlowAverageWindowOp.h:171
Definition: FlowAverageWindowOp.h:145
virtual void addPixel(Point2D< int > &p)
Adds a pixel to window statistics. Unvalidated location.
Definition: FlowAverageWindowOp.cpp:147
virtual void removePixel(Point2D< int > &p)
Removes a pixel from window statistics. Unvalidated location.
Definition: FlowAverageWindowOp.cpp:121
virtual void write()
Write the result in the target image.
Definition: FlowAverageWindowOp.cpp:174
Window average that preserves the magnitude.
Definition: FlowAverageWindowOp.h:53
void initialize()
Sets class-specific initial values. Does not change general window state (e.g. location)....
Definition: FlowAverageWindowOp.h:86
virtual void addPixel(Point2D< int > &p)
Adds a pixel to window statistics. Unvalidated location.
Definition: FlowAverageWindowOp.cpp:70
void setImageLimits() const
Sets internal limits corresponding to image geometries. Typically using coordHandler.
Definition: FlowAverageWindowOp.h:129
virtual void removePixel(Point2D< int > &p)
Removes a pixel from window statistics. Unvalidated location.
Definition: FlowAverageWindowOp.cpp:49
virtual void write()
Write the result in the target image.
Definition: FlowAverageWindowOp.cpp:89
void clear()
Clears statistics.
Definition: FlowAverageWindowOp.h:122
Container applicable for Channels and Images, with alpha support.
Definition: ImageTray.h:267
Template for operators applying pipeline-like sliding window.
Definition: SlidingWindowOp.h:59
Window implementation that uses incremental update of state.
Definition: SlidingWindow.h:50
virtual void fillBoth()
Clears and computes the statistics for the current location.
Definition: SlidingWindow.h:396
Base class for configurations applied in image processing windows, e.g. for operators of type WindowO...
Definition: Window.h:56
Point2D< int > location
Current location of this window.
Definition: Window.h:520
void setLoopLimits()
Sets the actual traversal range inside the window. Sometimes applied dynamically by reset().
Definition: Window.h:608
Class for ensuring that variable of type D remains within limits of type S.
Definition: TypeUtils.h:653
Definition: DataSelector.cpp:1277