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