SourceXtractorPlusPlus  0.14
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AttractorsPartitionStep.cpp
Go to the documentation of this file.
1 
22 #include <limits>
23 
26 
31 
32 namespace SourceXtractor {
33 
35  auto& stamp = source->getProperty<DetectionFrameSourceStamp>().getStamp();
36  auto& detection_frame = source->getProperty<DetectionFrame>();
37  auto& bounds = source->getProperty<PixelBoundaries>();
38 
39  auto bbox_min = bounds.getMin();
40  auto bbox_max = bounds.getMax();
41 
42  auto value_function = [bbox_min, bbox_max, &stamp](PixelCoordinate coord) {
43  if (coord.m_x < bbox_min.m_x || coord.m_x > bbox_max.m_x || coord.m_y < bbox_min.m_y || coord.m_y > bbox_max.m_y) {
45  }
46  auto offset_coord = coord - bbox_min;
47  return stamp.getValue(offset_coord.m_x, offset_coord.m_y);
48  };
49 
51  auto& pixel_list = source->getProperty<PixelCoordinateList>().getCoordinateList();
52  pixel_coordinates.reserve(pixel_list.size());
53  for (auto& pixel : pixel_list) {
54  pixel_coordinates.emplace_back(pixel, pixel);
55  }
56 
58 
59  attractPixels(pixel_coordinates, attractors, value_function);
60  auto merged = mergeAttractors(attractors);
61 
62  // If we end up with a single group use the original group
63  if (merged.size() == 1) {
64  return { source };
65  } else {
67  for (auto& source_pixels : merged) {
68  auto new_source = m_source_factory->createSource();
69  new_source->setProperty<PixelCoordinateList>(source_pixels);
70  new_source->setProperty<DetectionFrame>(detection_frame.getEncapsulatedFrame());
71 
72  sources.push_back(new_source);
73  }
74  return sources;
75  }
76 }
77 
82 
83  PixelCoordinate offsets[5] {
84  PixelCoordinate( 0, 0),
85  PixelCoordinate(-1, 0),
86  PixelCoordinate( 0, -1),
87  PixelCoordinate( 1, 0),
88  PixelCoordinate( 0, 1)
89  };
90 
91  if (pixels_with_origin.size() == 0) {
92  return;
93  }
94 
96 
97  for (auto& pixel_origin : pixels_with_origin) {
98  auto pixel = pixel_origin.first;
99  auto origin = pixel_origin.second;
100 
101  DetectionImage::PixelType values[5];
102  for (int i=0; i<5; i++) {
103  values[i] = value_function(pixel + offsets[i]);
104  }
105 
106  size_t max = 0;
107  for (int i=1; i<3; i++) {
108  if (values[i] > values[max]) {
109  max = i;
110  }
111  }
112  for (int i=3; i<5; i++) {
113  if (values[i] >= values[max]) {
114  max = i;
115  }
116  }
117 
118  if (max == 0) {
119  // We are at the attractor pixel
120  attractors[pixel].push_back(origin);
121  } else {
122  pixels_to_be_processed.push_back(
123  std::pair<PixelCoordinate, PixelCoordinate>(pixel + offsets[max], origin));
124  }
125  }
126  attractPixels(pixels_to_be_processed, attractors, value_function);
127 }
128 
134 
135  for (auto& attractor : attractors) {
136  auto coord = attractor.first;
137  auto& pixels = attractor.second;
138  bool done = false;
139 
140  for (size_t i=0; i < merged.size(); i++) {
141  if (coord.m_x >= bbox_min[i].m_x-1 && coord.m_x <= bbox_max[i].m_x+1 && coord.m_y >= bbox_min[i].m_y-1 && coord.m_y <= bbox_max[i].m_y+1) {
142  bbox_min[i] = PixelCoordinate(std::min(coord.m_x, bbox_min[i].m_x), std::min(coord.m_y, bbox_min[i].m_y));
143  bbox_max[i] = PixelCoordinate(std::max(coord.m_x, bbox_max[i].m_x), std::max(coord.m_y, bbox_max[i].m_y));
144  merged[i].insert(merged[i].begin(), pixels.begin(), pixels.end());
145  done = true;
146  break;
147  }
148  }
149  if (!done) {
150  merged.push_back(pixels);
151  bbox_min.push_back(coord);
152  bbox_max.push_back(coord);
153  }
154  }
155  return merged;
156 }
157 
158 } // SEImplementation namespace
159 
160 
161 
virtual std::vector< std::shared_ptr< SourceInterface > > partition(std::shared_ptr< SourceInterface > source) const override
The bounding box of all the pixels in the source. Both min and max coordinate are inclusive...
std::vector< std::vector< PixelCoordinate > > mergeAttractors(const std::unordered_map< PixelCoordinate, std::vector< PixelCoordinate >> &attractors) const
T min(T...args)
T push_back(T...args)
T lowest(T...args)
A copy of the rectangular region of the detection image just large enough to include the whole Source...
T max(T...args)
A pixel coordinate made of two integers m_x and m_y.
T insert(T...args)
T size(T...args)
STL class.
void attractPixels(const std::vector< std::pair< PixelCoordinate, PixelCoordinate >> &pixels_with_origin, std::unordered_map< PixelCoordinate, std::vector< PixelCoordinate >> &attractors, std::function< DetectionImage::PixelType(PixelCoordinate)> value_function) const
T begin(T...args)
std::shared_ptr< SourceFactory > m_source_factory
T reserve(T...args)
T emplace_back(T...args)