SlidingWindowMedianOp.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 SLIDINGWINDOWMEDIANOP_H_
32 #define SLIDINGWINDOWMEDIANOP_H_
33 
34 
35 #include "SlidingWindowHistogramOp.h"
36 
37 namespace drain
38 {
39 
40 namespace image
41 {
42 
43 
44 
65 //template <class R = WindowCore>
66 class SlidingWindowMedian : public SlidingWindowHistogram<WindowCore> {
67 
68  public:
69 
70  SlidingWindowMedian(int width=1,int height=0, int bins=256) : SlidingWindowHistogram<WindowCore>(width, height, bins) {
71  //this->setInfo("A pipeline implementation of window median.","width,height,percentage",p);
72  }
73 
74  SlidingWindowMedian(const conf_t & conf) : SlidingWindowHistogram<WindowCore>(conf) {
75  //this->setInfo("A pipeline implementation of window median.","width,height,percentage",p);
76  }
77 
78 
79  virtual
80  inline
81  void write(){
82  /*
83  if (this->debugDiag(4)){
84  std::cerr << location << '\t' << histogram.getMedian<double>() << '\n';
85  }
86  */
87  this->dst.put(this->location, histogram.getMedian<double>() );
88  }
89 
90 
91 };
92 
94 
95  public:
96 
98 
99  SlidingWindowMedianWeighted(int width=1,int height=0, int bins=256) : SlidingWindowHistogramWeighted(width, height, bins) {
100  }
101 
102  SlidingWindowMedianWeighted(const unweighted::conf_t & conf) : SlidingWindowHistogramWeighted(conf) {
103  }
104 
105 
106  virtual
107  inline
108  void write(){
109  dst.put(location, histogram.getMedian<double>() );
110  dstWeight.put(location, histogram.getSum<double>() / histogram.getSize() ); // TODO divBy0
111  }
112 
113  /*
114  virtual
115  inline
116  void toStream(std::ostream & ostr) const {
117  Window<HistogramWindowConfig>::toStream(ostr);
118  ostr << histogram;
119  }
120  */
121 
122 };
123 
124 
125 class SlidingWindowMedianOp : public SlidingWindowOp<SlidingWindowMedianWeighted>
126 {
127 public:
128 
129  SlidingWindowMedianOp(int width=1, int height=1, double percentage=0.5, int bins=256)
130  : SlidingWindowOp<SlidingWindowMedianWeighted>("SlidingWindowMedian", "A pipeline implementation of window median."){
131  parameters.link("percentage", this->conf.percentage = percentage);
132  parameters.link("bins", this->conf.bins = bins);
133  }
134 
135 
136 };
137 
138 }
139 }
140 
141 #endif // MEDIAN
142 
143 // Drain
T getSum() const
Sum of the samples.
Definition: Histogram.h:290
void put(size_t i, T x)
Sets the intensity in location i to x. See \address.
Definition: ImageFrame.h:192
Definition: SlidingWindowHistogramOp.h:173
Base class for median and str histogram based statistics.
Definition: SlidingWindowHistogramOp.h:67
Definition: SlidingWindowMedianOp.h:126
Definition: SlidingWindowMedianOp.h:93
virtual void write()
Write the result in the target image.
Definition: SlidingWindowMedianOp.h:108
Definition: SlidingWindowMedianOp.h:66
virtual void write()
Write the result in the target image.
Definition: SlidingWindowMedianOp.h:81
Template for operators applying pipeline-like sliding window.
Definition: SlidingWindowOp.h:59
Point2D< int > location
Current location of this window.
Definition: Window.h:520
Definition: DataSelector.cpp:1277