SourceXtractorPlusPlus  0.13
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BFSSegmentation.cpp
Go to the documentation of this file.
1 
18 #include <memory>
19 #include <vector>
20 #include <list>
21 #include <iostream>
22 
24 #include "SEUtils/HilbertCurve.h"
25 
28 
31 
33 
35 
36 namespace SourceXtractor {
37 
40  auto detection_image = frame->getThresholdedImage();
41  auto tiles = getTiles(*detection_image);
42 
43  VisitedMap visited(detection_image->getWidth(), detection_image->getHeight());
44 
46 
47  for (auto& tile : tiles) {
48  for (int y=0; y<tile.height; y++) {
49  for (int x=0; x<tile.width; x++) {
50  PixelCoordinate pixel = tile.offset + PixelCoordinate(x,y);
51  if (!visited.wasVisited(pixel) && detection_image->getValue(pixel) > 0.0) {
52  labelSource(pixel, listener, *detection_image, visited);
53  }
54  }
55  }
56  }
57 }
58 
61  DetectionImage& detection_image,
62  VisitedMap& visited_map) const {
65 
66  std::vector<PixelCoordinate> source_pixels;
67  std::vector<PixelCoordinate> pixels_to_process;
68 
69  visited_map.markVisited(pc);
70  pixels_to_process.emplace_back(pc);
71 
72  PixelCoordinate minPixel = pc;
73  PixelCoordinate maxPixel = pc;
74 
75  while (pixels_to_process.size() > 0) {
76  auto pixel = pixels_to_process.back();
77  pixels_to_process.pop_back();
78  source_pixels.emplace_back(pixel);
79 
80  minPixel.m_x = std::min(minPixel.m_x, pixel.m_x);
81  minPixel.m_y = std::min(minPixel.m_y, pixel.m_y);
82  maxPixel.m_x = std::max(maxPixel.m_x, pixel.m_x);
83  maxPixel.m_y = std::max(maxPixel.m_y, pixel.m_y);
84 
85  if (maxPixel.m_x - minPixel.m_x > m_max_delta || maxPixel.m_y - minPixel.m_y > m_max_delta) {
86  // The source extends over a too large area, ignore it
87  return;
88  }
89 
90  for (auto& offset : offsets) {
91  auto new_pixel = pixel + offset;
92 
93  if (!visited_map.wasVisited(new_pixel) && detection_image.getValue(new_pixel) > 0.0) {
94  visited_map.markVisited(new_pixel);
95  pixels_to_process.emplace_back(new_pixel);
96  }
97  }
98  }
99 
100  auto source = m_source_factory->createSource();
101  source->setProperty<PixelCoordinateList>(source_pixels);
102  source->setProperty<SourceId>();
103  listener.publishSource(source);
104 }
105 
107  int tile_width = TileManager::getInstance()->getTileWidth();
108  int tile_height = TileManager::getInstance()->getTileHeight();
109 
111 
112 
113  int size = std::max((image.getWidth() + tile_width - 1) / tile_width,
114  (image.getHeight() + tile_height - 1) / tile_height);
115 
116  HilbertCurve curve(size);
117 
118  for (auto& coord : curve.getCurve()) {
119  int x = coord.m_x * tile_width;
120  int y = coord.m_y * tile_height;
121 
122  if (x < image.getWidth() && y < image.getHeight()) {
124  PixelCoordinate(x, y),
125  std::min(tile_width, image.getWidth() - x),
126  std::min(tile_height, image.getHeight() - y)
127  });
128  }
129  }
130 
131  return tiles;
132 }
133 
134 }
static std::shared_ptr< TileManager > getInstance()
Definition: TileManager.h:136
std::shared_ptr< SourceFactory > m_source_factory
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
void labelSource(PixelCoordinate pc, Segmentation::LabellingListener &listener, DetectionImage &detection_image, VisitedMap &visited_map) const
virtual int getHeight() const =0
Returns the height of the image in pixels.
std::vector< BFSSegmentation::Tile > getTiles(const DetectionImage &image) const
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > y
void labelImage(Segmentation::LabellingListener &listener, std::shared_ptr< const DetectionImageFrame > frame) override
T min(T...args)
std::vector< PixelCoordinate > getCurve() const
Definition: HilbertCurve.h:34
virtual T getValue(int x, int y) const =0
Returns the value of the pixel with the coordinates (x,y)
T pop_back(T...args)
T lock(T...args)
T max(T...args)
A pixel coordinate made of two integers m_x and m_y.
T size(T...args)
STL class.
T back(T...args)
bool wasVisited(PixelCoordinate pc) const
Interface representing an image.
Definition: Image.h:43
virtual int getWidth() const =0
Returns the width of the image in pixels.
void publishSource(std::shared_ptr< SourceInterface > source) const
Definition: Segmentation.h:100
T emplace_back(T...args)