SourceXtractorPlusPlus  0.11
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Lutz.cpp
Go to the documentation of this file.
1 
17 /*
18  * Lutz.cpp
19  *
20  * Created on: Jan 17, 2017
21  * Author: mschefer
22  */
23 
24 
29 
33 
35 
36 namespace SourceXtractor {
37 
38 // class Lutz
39 //
40 //
41 
42 
43 enum class LutzStatus {
44  COMPLETE = 0,
45  INCOMPLETE,
46  NONOBJECT,
47  OBJECT
48 };
49 
50 enum class LutzMarker {
51  ZERO = 0,
52  S,
53  S0,
54  F,
55  F0
56 };
57 
58 
59 void Lutz::labelImage(LutzListener& listener, const DetectionImage& image, PixelCoordinate offset) {
60  int width = image.getWidth() + 1; // one extra pixel
61 
62  std::vector<LutzMarker> marker(image.getWidth()+1);
63  std::fill(marker.begin(), marker.end(), LutzMarker::ZERO);
64 
65  std::vector<PixelGroup> group_stack;
66  std::vector<LutzStatus> ps_stack;
67 
69  //std::shared_ptr<VectorImage<unsigned int>> check_image=VectorImage<unsigned int>::create(image.getWidth(), image.getHeight());
70 
71  int lines = image.getHeight();
72  int chunk_height = TileManager::getInstance()->getTileHeight();
74 
75  for (int y = 0; y < lines; y++) {
78 
79  if (y % chunk_height == 0) {
81  chunk = image.getChunk(0, y, image.getWidth(), std::min(chunk_height, lines - y));
82  }
83 
84  int dy = y % chunk_height;
85  for (int x=0; x < width; x++) {
86  DetectionImage::PixelType value = (x == width - 1) ? 0.0 : chunk->getValue(x, dy);
87 
88  LutzMarker last_marker = marker[x];
89  marker[x] = LutzMarker::ZERO;
90 
91  bool in_object = value > 0.0;
92  if (in_object) {
93  // We have an object pixel
94  //check_image->setValue(x, y, 1);
95  if (cs != LutzStatus::OBJECT) {
96  // Previous pixel not object, start new segment
97 
98  cs = LutzStatus::OBJECT;
99 
100  if (ps == LutzStatus::OBJECT) {
101  // Pixel touches segment on preceding scan
102 
103  if (group_stack.back().start == -1) {
104  // First pixel of object on current scan
105  marker[x] = LutzMarker::S;
106  group_stack.back().start = x;
107  } else {
108  marker[x] = LutzMarker::S0;
109  }
110  } else {
111  // Start of completely new pixel group
112  ps_stack.push_back(ps);
114  group_stack.emplace_back();
115  marker[x] = LutzMarker::S;
116  group_stack.back().start = x;
117  }
118  }
119  }
120 
121  if (last_marker != LutzMarker::ZERO) {
122  // There is a marker from the previous scan to process
123  // This is done for both object and non-object pixels
124 
125  if (last_marker == LutzMarker::S) {
126  // Start of pixel group on preceding scan
127  ps_stack.push_back(ps);
128  if (cs == LutzStatus::NONOBJECT) {
129  // The S marker is the first encounter with this group
131 
132  group_stack.emplace_back(std::move(inc_group_map.at(x)));
133  inc_group_map.erase(x);
134 
135  group_stack.back().start = -1;
136  } else {
137  // Add group to current group
138  auto prev_group = inc_group_map.at(x);
139  inc_group_map.erase(x);
140 
141  group_stack.back().merge_pixel_list(prev_group);
142  }
143  ps = LutzStatus::OBJECT;
144  }
145 
146  if (last_marker == LutzMarker::S0) {
147  // Start of secondary segment of group on preceding scan
148 
149  if (cs == LutzStatus::OBJECT && ps == LutzStatus::COMPLETE) {
150  // Current group is joined to preceding group
151  ps_stack.pop_back();
152  auto old_group = std::move(group_stack.back());
153  group_stack.pop_back();
154  group_stack.back().merge_pixel_list(old_group);
155 
156  if (group_stack.back().start == -1) {
157  group_stack.back().start = old_group.start;
158  } else {
159  marker[old_group.start] = LutzMarker::S0;
160  }
161  }
162  ps = LutzStatus::OBJECT;
163  }
164 
165  if (last_marker == LutzMarker::F0) {
167  }
168 
169  if (last_marker == LutzMarker::F) {
170 
171 
172  ps = ps_stack.back();
173  ps_stack.pop_back();
174 
175  if (cs == LutzStatus::NONOBJECT && ps == LutzStatus::COMPLETE) {
176  // If no more of current group to come then finish it
177  auto old_group = std::move(group_stack.back());
178  group_stack.pop_back();
179  if (old_group.start == -1) {
180  // Pixel group completed
181  listener.publishGroup(old_group);
182  } else {
183  marker[old_group.end] = LutzMarker::F;
184  inc_group_map[old_group.start] = old_group;
185  }
186  ps = ps_stack.back();
187  ps_stack.pop_back();
188  }
189  }
190  }
191 
192  if (in_object) {
193  // Update current group by current pixel
194  group_stack.back().pixel_list.push_back(PixelCoordinate(x, y) + offset);
195 
196  } else {
197  // The current pixel is not object
198 
199  if (cs == LutzStatus::OBJECT) {
200  // Previous pixel was object. Finish segment
202 
203  if (ps != LutzStatus::COMPLETE) {
204  // End of segment but not necessarily of section
205  marker[x] = LutzMarker::F0;
206  group_stack.back().end = x;
207  } else {
208  // End of final segment of group section
209  ps = ps_stack.back();
210  ps_stack.pop_back();
211 
212  marker[x] = LutzMarker::F;
213 
214  auto old_group = group_stack.back();
215  group_stack.pop_back();
216 
217  inc_group_map[old_group.start] = old_group;
218  }
219  }
220  }
221  }
222  listener.notifyProgress(y + 1, lines);
223  }
224 
225  //FitsWriter::writeFile<unsigned int>(*check_image, "segCheck.fits");
226  // Process the pixel groups left in the inc_group_map
227  for (auto& group : inc_group_map) {
228  listener.publishGroup(group.second);
229  }
230 }
231 
232 void LutzList::publishGroup(PixelGroup& pixel_group) {
233  m_groups.push_back(pixel_group);
234 }
235 
236 
237 }
virtual void publishGroup(PixelGroup &pixel_group) override
Definition: Lutz.cpp:232
static std::shared_ptr< TileManager > getInstance()
Definition: TileManager.h:137
virtual void notifyProgress(int, int)
Definition: Lutz.h:56
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
virtual void publishGroup(PixelGroup &pixel_group)=0
virtual int getHeight() const =0
Returns the height of the image in pixels.
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > y
void labelImage(LutzListener &listener, const DetectionImage &image, PixelCoordinate offset=PixelCoordinate(0, 0))
Definition: Lutz.cpp:59
virtual std::shared_ptr< ImageChunk< T > > getChunk(int x, int y, int width, int height) const =0
T min(T...args)
T push_back(T...args)
T erase(T...args)
T pop_back(T...args)
T lock(T...args)
A pixel coordinate made of two integers m_x and m_y.
T move(T...args)
STL class.
std::vector< PixelGroup > m_groups
Definition: Lutz.h:83
T back(T...args)
Interface representing an image.
Definition: Image.h:43
T fill(T...args)
virtual int getWidth() const =0
Returns the width of the image in pixels.
std::shared_ptr< EngineParameter > dy
T emplace_back(T...args)