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