SlidingWindowHistogramOp.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 SLIDINGWINDOWHISTOGRAM_H_
32 #define SLIDINGWINDOWHISTOGRAM_H_
33 
34 
35 
36 #include "SlidingWindowOp.h"
37 //#include "SlidingOp.h" // NEW 2015
38 #include "drain/util/Histogram.h"
39 
40 namespace drain
41 {
42 
43 namespace image
44 {
45 
47 
48  int bins;
49  float percentage;
50  std::string valueFunc;
51 
52 };
53 
55  public:
56 
57 };
58 
60 
66 template <class R = WindowCore>
67 class SlidingWindowHistogram : public SlidingWindow<HistogramWindowConfig, R> {
68 
69 public:
70 
71  SlidingWindowHistogram(int width=1, int height=0, int bins=256) : SlidingWindow<HistogramWindowConfig, R>(width,height) {
72  histogram.setSize(bins);
73  };
74 
76  histogram.setSize(conf.bins); // needed?
77  };
78 
79 
80 
81  /*
82  * TODO: weight = > weightSUm
83  * TODO: multiple channels (banks)?
84  * FIXME: Polar coord problem
85  */
90  virtual inline
91  void initialize(){
92 
93  drain::Logger mout(getImgLog(), __FILE__, __FUNCTION__);
94 
95  this->setImageLimits();
96  this->setLoopLimits();
97  this->location.setLocation(0, 0);
98 
99  histogram.setSize(this->conf.bins);
100  histogram.setSampleCount(this->getArea());
101  //histogram.setScale(src.getMin<int>(), src.getMax<int>(), dst.getMin<int>(), dst.getMax<int>());
102  histogram.setRange(this->src.getConf().template getTypeMin<int>(), this->src.getConf().template getTypeMax<int>());
103 
104  if (!this->conf.valueFunc.empty())
105  histogram.setValueFunc(this->conf.valueFunc.at(0));
106  histogram.setMedianPosition(this->conf.percentage);
107 
108  mout.warn("a=", this->getArea()); // , " sampleCountMedian=", histogram.sampleCountMedian);
109  mout.warn("histogram=", histogram, " ");
110  mout.debug3(this->coordinateHandler);
111 
112  }
113 
114  virtual inline
115  void clear(){
116 
117  drain::Logger mout(getImgLog(),"SlidingWindowHistogramWeighted", __FUNCTION__);
118  histogram.clearBins();
119 
120  }
121 
122 
123 
124 protected:
125 
126  drain::Histogram histogram;
127 
128  virtual inline
129  void setImageLimits() const {
130  this->coordinateHandler.set(this->src.getGeometry(), this->src.getCoordinatePolicy());
131  // this->src.adjustCoordinateHandler(this->coordinateHandler);
132  }
133 
134  virtual inline
136  if (this->coordinateHandler.validate(p))
137  histogram.decrement(this->src.template get<int>(p));
138  };
139 
140  virtual inline
142 
143  //if (this->debugDiag(4))std::cerr << this->location << '\n';
144 
145  if (this->coordinateHandler.validate(p))
146  histogram.increment(this->src.template get<int>(p));
147  };
148 
149  inline virtual
150  void write(){
151  this->dst.put(this->location, histogram.getValue());
152  };
153 
154 
155  /*
156  virtual
157  void updateHorz();
158 
159  virtual
160  void updateVert();
161  */
162 
163 };
164 
165 /*
166 class SlidingWindowHistogram : public SlidingWindowHistogramBase<WindowCore> {
167 public:
168  SlidingWindowHistogram(const unweighted::conf_t & conf) : SlidingWindowHistogramBase<WindowCore>(conf) { };
169 
170 };
171 */
172 
173 class SlidingWindowHistogramWeighted : public SlidingWindowHistogram<WeightedWindowCore> {
174 
175 public:
176 
178 
179  SlidingWindowHistogramWeighted(int width=1, int height=0, int bins=256) : SlidingWindowHistogram<WeightedWindowCore>(width,height,bins) { };
180 
181  SlidingWindowHistogramWeighted(const unweighted::conf_t & conf) : SlidingWindowHistogram<WeightedWindowCore>(conf) { };
182 
183 
184  virtual
185  void write(){
186  this->dst.put(this->location, this->histogram.getValue());
187  //dstWeight.put(location, histogram.getSum<double>() / histogram.getSampleCount() ); // faulty?
188  this->dstWeight.put(this->location, this->histogram.getMean<double>()); // faulty?
189  };
190 
191 protected:
192 
193 
194  virtual inline
196  /*
197  if (this->debugDiag(4)){
198  std::cerr << this->location << '\n';
199  }
200  */
201  if (this->coordinateHandler.validate(p))
202  this->histogram.increment(this->src.get<int>(p), this->srcWeight.get<int>(p));
203  };
204 
205  virtual inline
207  if (this->coordinateHandler.validate(p))
208  this->histogram.decrement(this->src.get<int>(p), this->srcWeight.get<int>(p));
209  };
210 
211 };
212 
214 
234 class SlidingWindowHistogramOp : public SlidingWindowOp<SlidingWindowHistogramWeighted>
235 {
236 public:
237 
238  SlidingWindowHistogramOp(int width=1, int height=1, std::string valueFunc="a", double percentage=0.5, int bins=256)
239 : SlidingWindowOp<SlidingWindowHistogramWeighted>("SlidingWindowHistogram",
240  "A pipeline implementation of window histogram; valueFunc=[asmdvNX] (avg,sum,median,stddev,variance,miN,maX)"){
241  parameters.link("valueFunc", this->conf.valueFunc = valueFunc, "asmdvXN");
242  parameters.link("percentage", this->conf.percentage = percentage);
243  parameters.link("bins", this->conf.bins = bins);
244  }
245 
246 };
247 
248 } // image::
249 
250 } // drain::
251 
252 #endif
253 
254 // Drain
Class for computing a histogram and some statistics: average, min, max, mean, std....
Definition: Histogram.h:61
void clearBins()
Does not change the size of the histogram.
Definition: Histogram.h:94
void setRange(double dataMin, double dataMax)
Set range of original (physical) values to be mapped on the limited number of bins....
Definition: Histogram.cpp:76
void setMedianPosition(double pos)
Set location of the median, if not in the middle (50%).
Definition: Histogram.h:177
T getMean() const
Unscaled mean.
Definition: Histogram.h:305
void setSampleCount(long int n)
Does not change the size of the histogram.
Definition: Histogram.h:113
void setSize(size_t s)
Sets the number of bins; the resolution of the histogram.
Definition: Histogram.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
bool validate(Point2D< int > &p) const
Handles the coordinate, returning true if the position is reversible.
Definition: CoordinateHandler.h:223
Definition: SlidingWindowHistogramOp.h:54
void put(size_t i, T x)
Sets the intensity in location i to x. See \address.
Definition: ImageFrame.h:192
T get(size_t i) const
Gets the intensity at location i. See address().
Definition: ImageFrame.h:254
Window histogram for computing [asmdvNX] = iAverage, sum, median, stddev, variance,...
Definition: SlidingWindowHistogramOp.h:235
Definition: SlidingWindowHistogramOp.h:173
virtual void write()
Write the result in the target image.
Definition: SlidingWindowHistogramOp.h:185
virtual void removePixel(Point2D< int > &p)
Removes a pixel from window statistics. Unvalidated location.
Definition: SlidingWindowHistogramOp.h:206
virtual void addPixel(Point2D< int > &p)
Adds a pixel to window statistics. Unvalidated location.
Definition: SlidingWindowHistogramOp.h:195
Base class for median and str histogram based statistics.
Definition: SlidingWindowHistogramOp.h:67
virtual void setImageLimits() const
Sets internal limits corresponding to image geometries. Typically using coordHandler.
Definition: SlidingWindowHistogramOp.h:129
virtual void write()
Write the result in the target image.
Definition: SlidingWindowHistogramOp.h:150
virtual void removePixel(Point2D< int > &p)
Removes a pixel from window statistics. Unvalidated location.
Definition: SlidingWindowHistogramOp.h:135
virtual void addPixel(Point2D< int > &p)
Adds a pixel to window statistics. Unvalidated location.
Definition: SlidingWindowHistogramOp.h:141
virtual void initialize()
Definition: SlidingWindowHistogramOp.h:91
virtual void clear()
Clears the applied statistics. Redefined in derived classes.
Definition: SlidingWindowHistogramOp.h:115
Template for operators applying pipeline-like sliding window.
Definition: SlidingWindowOp.h:59
Window implementation that uses incremental update of state.
Definition: SlidingWindow.h:50
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
size_t getArea()
Returns the nominal area in pixels.
Definition: Window.h:486
Definition: DataSelector.cpp:1277
Definition: SlidingWindowHistogramOp.h:46