Loading...
Searching...
No Matches
SegmentAreaOp.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 SEGMENTAREAOP_H
32#define SEGMENTAREAOP_H
33
34#include <math.h>
35
36#include "drain/image/SegmentProber.h"
37
38//#include "ImageOp.h"
39//#include "FloodFillOp.h"
40
41#include "SegmentOp.h"
42
43namespace drain
44{
45namespace image
46{
47
48/*
49 * Problem: --iSegmentArea 64:255,FuzzyStep:100:1
50 * actually is SegmentArea(64:255,FuzzyStep(100:100,1))
51 *
52 * Consider: FuzzyStep/100:100/1
53 *
54 * Or prefix cmd:
55 * --functor FuzzyStep,100:100,1
56 * --functor FuzzyStep|100:100,1
57 * --functor FuzzyStep[100:100,1]
58 *
59 * or last-arg-comma-support?
60 * a,b,1,2,3
61 * A=a,B=c,C=C,D,E
62 */
63
64
66
94template <class S, class D, class T=SizeProber>
96{
97public:
98
99 typedef S src_t;
100 typedef D dst_t;
108 inline // static_cast<double>(0xffff)
109 SegmentAreaOp(double min=1.0, double max=std::numeric_limits<double>::max()) :
110 //SegmentAreaOp(double min=1.0, double max=static_cast<double>(0xffff)) :
111 SegmentOp(__FUNCTION__, "Computes segment sizes.") {
112 parameters.link("intensity", this->intensity.tuple(), "min:max");
113 parameters.link("functor", this->functorStr); // eg. "FuzzyStep:params"
114 this->intensity.set(min, max);
115 };
116
117 inline // double max=static_cast<double>(0xffff)
118 SegmentAreaOp(const drain::UnaryFunctor & ftor, double min=1.0, double max=std::numeric_limits<double>::max()) : //, const std::string & mapping="d", double scale=1.0, double offset=0.0) :
119 SegmentOp(__FUNCTION__, "Computes segment sizes", ftor) {
120 parameters.link("intensity", this->intensity.tuple(), "min:max");
121 this->intensity.set(min, max);
122 };
123
124 SegmentAreaOp(const SegmentAreaOp & op) : SegmentOp(op){
125 parameters.copyStruct(op.getParameters(), op, *this);
126 }
127
129 //void makeCompatible(const ImageFrame & src, Image & dst) const;
130 void getDstConf(const ImageConf & src, ImageConf & dst) const;
131
132 inline
133 void traverseChannels(const ImageTray<const Channel> & src, ImageTray<Channel> & dst) const {
134 //drain::Logger mout(getImgLog(), __FILE__, __FUNCTION__); //REPL this->name+"[const ImageTray &, ImageTray &]", __FUNCTION__);
136 }
137
138 virtual
139 void traverseChannel(const Channel & src, Channel & dst) const;
140
141
142};
143
144
145template <class S, class D, class T>
147 // void SegmentAreaOp<S,D>::makeCompatible(const ImageFrame & src, Image & dst) const {
148
149 drain::Logger mout(getImgLog(), __FUNCTION__, getName());
150
151 const std::type_info & t = typeid(dst_t); // typeid(typename T::dst_t);
152
153 /*if (!dst.typeIsSet()){
154 dst.setType(t);
155 }
156 else*/
157 if (Type::call<typeIsFloat>(dst.getType())){
158 dst.setType(t);
159 mout.warn("float valued destination data not supported, setting: " , Type::getTypeChar(t) );
160 //throw std::runtime_error("SegmentAreaOp: float valued destination image not supported.");
161 }
162
163 dst.setArea(src.getGeometry());
164 dst.setChannelCount(std::max(src.getImageChannelCount(),dst.getImageChannelCount()), dst.getAlphaChannelCount());
165
166 //if (clearDst)
167 // dst.clear();
168
169}
170
171template <class S, class D, class T>
173
174 drain::Logger mout(getImgLog(), __FUNCTION__, getName());
175
177 raw.min = src.getScaling().inv(this->intensity.min);
178 raw.max = (this->intensity.max == std::numeric_limits<double>::max()) ? src.getConf().getTypeMax<src_t>() : src.getScaling().inv(this->intensity.max);
179
180 if (raw.min <= src.getConf().getTypeMin<src_t>()){
181 mout.warn("src scaling: " , src.getScaling() );
182 mout.warn("original range: " , intensity );
183 mout.warn("raw (code) range: " , raw );
184 mout.warn("min value=" , (double)raw.min , " less or smaller than storage type min=" , src.getConf().getTypeMin<src_t>() );
185 }
186
187 mout.special("raw range: " , (double)raw.min , '-' , (double)raw.max );
188 mout.debug2("src: " , src );
189 mout.debug2("dst: " , dst );
190
191 //SizeProber sizeProber(src, dst);
192 T sizeProber(src, dst);
193 sizeProber.conf.anchor.set(raw.min, raw.max);
194 mout.debug2("areaProber:" , sizeProber );
195
196 FillProber floodFill(src, dst);
197 floodFill.conf.anchor.set(raw.min, raw.max); // Min = raw.min;
198 mout.debug2("Floodfill: " , floodFill );
199
200 const double scale = drain::Type::call<typeNaturalMax>(dst.getType());
201 // const double scale = drain::Type::call<drain::typeIsSmallInt>(dst.getType()) ? dst.getEncoding().getTypeMax<double>() : 1.0;
202
203 const UnaryFunctor & ftor = getFunctor(scale); // scale problematic, but needed for const Ftor
204
205 mout.debug("Scale: " , scale );
206 mout.debug("Final functor: " , ftor.getName() , '(' , ftor.getParameters() , ')' );
207 /*
208 mout.warn("Functor: 1 =>" , ftor(1.0) );
209 mout.warn("Functor: 100 =>" , ftor(100.0) );
210 mout.warn("Functor: 10000 =>" , ftor(10000.0) );
211 */
212 //mout.debug2();
213
214 const CoordinatePolicy & cp = src.getCoordinatePolicy();
215 const bool HORZ_MODE = ((cp.xUnderFlowPolicy != EdgePolicy::POLAR) && (cp.xOverFlowPolicy != EdgePolicy::POLAR));
216
217 if (HORZ_MODE){
218 mout.attention("scan HORIZONTAL, coordPolicy=", cp);
219 }
220 else {
221 mout.attention("scan VERTICAL, coordPolicy=", cp);
222 }
223
224 typedef drain::typeLimiter<dst_t> Limiter;
225 typename Limiter::value_t limit = dst.getConf().getLimiter<dst_t>();
226
227 const size_t width = src.getWidth();
228 const size_t height = src.getHeight();
229
230 size_t sizeMapped;
231 for (size_t i=0; i<width; i++){
232 for (size_t j=0; j<height; j++){
233
234 // STAGE 1: detect size.
235 // Note: retries same locations again and again. Could return true in success, i.e. first pixel was univisited and accepted.
236 sizeProber.probe(i,j, HORZ_MODE);
237
238 if (sizeProber.size > 0){
239
240 // STAGE 2: mark the segment with size
241 sizeMapped = limit(ftor(sizeProber.size));
242 if (sizeMapped == 0)
243 sizeMapped = 1;
244
245 // mout.warn() << "found segment at " << i << ',' << j << " f=" << src.get<float>(i,j);
246 // mout << " size=" << sizeProber.size << " => "<< sizeMapped << mout.endl;
247
248 floodFill.conf.markerValue = sizeMapped;
249 floodFill.probe(i,j, HORZ_MODE);
250
251 }
252 }
253 }
254
255
256}
257
258
259} // image
260
261} // drain
262
263#endif
264
265// Drain
virtual const std::string & getName() const
Return the name of an instance.
Definition BeanLike.h:80
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 & debug(const TT &... args)
Debug information.
Definition Log.h:667
Logger & special(const TT &... args)
Other useful information.
Definition Log.h:532
Logger & attention(const TT &... args)
Possible error, but execution can continue. Special type of Logger::warn().
Definition Log.h:477
Logger & debug2(const TT &... args)
Debug information.
Definition Log.h:677
Definition Range.h:52
void copyStruct(const ReferenceMap &m, const T &src, T &dst, extLinkPolicy policy=RESERVE)
Experimental. Copies references and values of a structure to another.
Definition ReferenceMap.h:415
double inv(double y) const
Inverse scaling: given physically meaningful value y, returns the corresponding code value.
Definition ValueScaling.h:300
Image with static geometry.
Definition ImageChannel.h:58
Policies for coordinate underflows and overflows.
Definition CoordinatePolicy.h:100
const std::type_info & getType() const
Linear scaling.
Definition ImageConf.h:110
void setType(const std::type_info &t)
Set storage type.
Definition ImageConf.h:121
T getTypeMax() const
Returns the maximum value supported by the current storage type.
Definition ImageConf.h:281
T getTypeMin() const
Returns the minimum value supported by the current storage type.
Definition ImageConf.h:267
Definition SegmentProber.h:488
Struct for image (excluding data)
Definition ImageConf.h:333
const CoordinatePolicy & getCoordinatePolicy() const
Coord policy.
Definition ImageLike.h:174
const std::type_info & getType() const
Get the storage type.
Definition ImageLike.h:100
void traverseChannelsSeparately(const ImageTray< const Channel > &src, ImageTray< Channel > &dst) const
Process each (src,dst) channel pair independently. Raise error if their counts differ.
Definition ImageOp.cpp:340
Computes sizes of connected pixel areas.
Definition SegmentAreaOp.h:96
virtual void traverseChannel(const Channel &src, Channel &dst) const
Apply to single channel.
Definition SegmentAreaOp.h:172
SegmentAreaOp(double min=1.0, double max=std::numeric_limits< double >::max())
Definition SegmentAreaOp.h:109
void getDstConf(const ImageConf &src, ImageConf &dst) const
Resizes dst to width and height of src. Ensures integer type.
Definition SegmentAreaOp.h:146
A base class for computing statistics of segments. As segment is an area of connected pixels.
Definition SegmentOp.h:53
D markerValue
"fill value", also dynamically changing visit marker?
Definition SegmentProber.h:75
void probe(int i, int j, bool HORIZONTAL)
Start probings.
Definition SegmentProber.h:206
Class for ensuring that variable of type D remains within limits of type S.
Definition TypeUtils.h:98
Definition DataSelector.cpp:1277
UnaryFunctor & getFunctor(const std::string &nameAndParams, char separator)
Returns functor the parameters of which have been set.
Definition FunctorBank.cpp:77
Definition Functor.h:116