Loading...
Searching...
No Matches
EdgeTracker.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 EDGE_TRACKER_H
32#define EDGE_TRACKER_H
33
34#include <drain/image/ProbingControl.h>
35#include <list>
36
37#include "Direction.h"
38#include "ImageChannel.h"
39#include "ImageFile.h"
40
41// #include "FilePng.h"
42
43namespace drain
44{
45
46namespace image
47{
48
49typedef std::list<drain::image::Position> Contour;
50
52
62
71template <class S, class D> //, class C=ProberCriteria! >
73
74public:
75
76 Position pos;
77 Direction::value_t dir = Direction::NONE;
78
79 Position startPos;
80 Direction::value_t startDir = Direction::NONE;
81
82 // PositionTuple pos;
83 // Direction::value_t dir = Direction::NONE;
84
85 EdgeTracker(const Channel & src, ProberControl & control) : control(control), src(src){
86 init();
87 control.markerImage.setGeometry(src.getGeometry().area);
88 }
89
90 virtual
91 ~EdgeTracker(){};
92
93 typedef S src_t;
94 typedef D dst_t;
95
96 // typedef std::vector<drain::Point2D<short int> > step_t;
97 // static
98 // const step_t steps;
99
100 Contour contour;
101
102 void setStart(const Position & startPos, Direction::value_t startDir){
103 pos = startPos;
104 dir = startDir;
105 this->startPos = startPos;
106 this->startDir = startDir;
107 };
108
109 //ProberControl::move_status next(){
110 bool next(){
111
112 drain::Logger mout(getImgLog(), __FILE__, __FUNCTION__);
113
114 mout.accept<LOG_WARNING>("From ", pos, " to: ", Direction::arrows[dir], ' ', '<', dir , '>');
115
116 Position p = pos;
117
118 if (control.move(p, dir) == ProberControl::MOVE_ACCEPTED){ // p changes in this condition
119 if (control.isValidPixel(src, p)){
120 mout.accept<LOG_WARNING>(p);
121 pos = p;
122 contour.push_back(pos);
123 dir = DIR_TURN_DEG(dir, 225);
124 /* if (Direction::isDiagonal(dir)){
125 Position p(startPos.i, pos.j);
126 control.markBlocked(pos, dir+90deg)
127 }
128 */
129 //control.markBlockedOut(startPos, DIR_TURN_DEG(dir, -90));
130 }
131 else {
132 mout.pending<LOG_WARNING>(" outside segment, keeping ", pos);
133 //mout.pending<LOG_WARNING>(pos, ' ', Direction::arrows[dir], " ", p, " outside segment ");
134 // control.markBlockedIn(pos0, dir);
135 dir = DIR_TURN_DEG(dir, 45);
136 }
137 }
138 else {
139 mout.reject<LOG_WARNING>("illegal move");
140 //mout.reject<LOG_WARNING>(p, '>', dir, "->", pos, " illegal move ");
141 dir = DIR_TURN_DEG(dir, 45);
142 }
143 // control.markBlocked(pos, dir); // NOT NEEDED...
144
145 return (! (dir == startDir) && (pos == startPos) );
146
147 //while ((dir != startDir) && (pos != startPos));
148
149 }
150
151
160 void track(const Position & startPos, Direction::value_t startDir){
161
162 drain::Logger mout(getImgLog(), __FILE__, __FUNCTION__);
163
164 setStart(startPos, startDir);
165
166 mout.accept<LOG_WARNING>("Start ", pos, ':', Direction::arrows[dir], ' ', '<', dir , '>');
167
168 while (next()){
169 // mout.accept<LOG_WARNING>("New edge pos: ", pos);
170 mout.accept<LOG_WARNING>("Tracking ", pos, ':', Direction::arrows[dir]); // , ' ', '<', dir , '>'
171 }
172 /*
173 Position p;
174
175 // Idea: try to move, and if not possible, turn clockwise.
176 do {
177
178 p = pos;
179 if (control.move(p, dir) == ProberControl::MOVE_ACCEPTED){ // Note: pos was changed in this condition
180 if (control.isValidPixel(src, p)){
181 mout.accept<LOG_WARNING>(p);
182 pos = p;
183 contour.push_back(pos);
184 dir = DIR_TURN_DEG(dir, 225);
185 //control.markBlockedOut(startPos, DIR_TURN_DEG(dir, -90));
186 }
187 else {
188 mout.pending<LOG_WARNING>(p, '>', dir, "->", pos, " outside segment ");
189 // control.markBlockedIn(pos0, dir);
190 dir = DIR_TURN_DEG(dir, 45);
191 }
192 }
193 else {
194 mout.reject<LOG_WARNING>(p, '>', dir, "->", pos, " illegal move ");
195 dir = DIR_TURN_DEG(dir, 45);
196 }
197 // control.markBlocked(pos, dir); // NOT NEEDED...
198
199
200 }
201 while (! (dir == startDir) && (pos == startPos) );
202 */
203
204 mout.note("Writing debug image");
205
206 ImageFile::write(control.markerImage, "edge.png");
207 exit(0);
208
209 }
210
211 /*
212 void setParams(const C & conf){
213 this->conf = conf;
214 }
215 */
216
218 virtual
219 void init(){
220
221 /*
222 drain::Logger mout(getImgLog(), __FILE__, __FUNCTION__);
223 handler.set(src.getGeometry(), src.getCoordinatePolicy());
224 // src.adjustCoordinateHandler(handler);
225 mout.debug(handler );
226 mout.debug2(src );
227 mout.debug2(*dst );
228 */
229 }
230
231
233
241 // consider protected:
243
244 // This could be in control, but is in (Super)Prober, inherited from SegmentProber
245 //CoordinateHandler2D handler;
246
247 const Channel & src;
248
249
250
251
252};
253
254
255//template <class S, class D>
256//const typename EdgeTracker<S,D>::step_t steps = {{0,0}, {1,0}};
257
258
259template <class S, class D>
260std::ostream & operator<<(std::ostream & ostr, const EdgeTracker<S,D> & prober){
261 return ostr;
262}
263
264} // image::
265
266} // drain::
267
268#endif /* SEGMENT_PROBER_H_ */
269
270
271
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:313
Logger & pending(const TT &... args)
Report a conditional accept/reject, to be completed next.
Definition Log.h:597
Logger & note(const TT &... args)
For top-level information.
Definition Log.h:490
Logger & reject(const TT &... args)
Some input has been rejected, for example by a syntax.
Definition Log.h:611
Logger & accept(const TT &... args)
Some input has been accepted, for example by a syntax.
Definition Log.h:583
Image with static geometry.
Definition ImageChannel.h:58
Container for parameters of SegmentProber.
Definition EdgeTracker.h:72
ProberControl & control
A convenience function for traversing a whole image.
Definition EdgeTracker.h:242
void track(const Position &startPos, Direction::value_t startDir)
Definition EdgeTracker.h:160
virtual void init()
Called after src and dst have been set, but before processing. See clear().
Definition EdgeTracker.h:219
virtual void setGeometry(size_t width, size_t height, size_t imageChannels=1, size_t alphaChannels=0)
Resizes the image, keeps the current type.
Definition Image.h:87
Definition DataSelector.cpp:1277
Definition Direction.h:46
Container for parameters of SegmentProber.
Definition ProbingControl.h:60