SourceXtractorPlusPlus  0.13
Please provide a description of the project.
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 }
PixelCoordinateList.h
SourceXtractor::PixelCoordinateList
Definition: PixelCoordinateList.h:31
SourceXtractor::TileManager::getInstance
static std::shared_ptr< TileManager > getInstance()
Definition: TileManager.h:136
std::lock
T lock(T... args)
SourceXtractor::PixelCoordinate
A pixel coordinate made of two integers m_x and m_y.
Definition: PixelCoordinate.h:37
SourceXtractor::BFSSegmentation::Tile
Definition: BFSSegmentation.h:65
std::shared_ptr
STL class.
SourceXtractor::Image::getWidth
virtual int getWidth() const =0
Returns the width of the image in pixels.
SourceId.h
std::vector
STL class.
std::vector::size
T size(T... args)
SourceXtractor::Segmentation::LabellingListener::publishSource
void publishSource(std::shared_ptr< SourceInterface > source) const
Definition: Segmentation.h:100
SourceXtractor::BFSSegmentation::getTiles
std::vector< BFSSegmentation::Tile > getTiles(const DetectionImage &image) const
Definition: BFSSegmentation.cpp:106
std::lock_guard< std::recursive_mutex >
SourceXtractor::Image::getHeight
virtual int getHeight() const =0
Returns the height of the image in pixels.
std::vector::back
T back(T... args)
SourceXtractor::MultithreadedMeasurement::g_global_mutex
static std::recursive_mutex g_global_mutex
Definition: MultithreadedMeasurement.h:53
SourceXtractor::BFSSegmentation::VisitedMap::markVisited
void markVisited(PixelCoordinate pc)
Definition: BFSSegmentation.h:48
SourceXtractor::Image< SeFloat >
SourceXtractor::HilbertCurve::getCurve
std::vector< PixelCoordinate > getCurve() const
Definition: HilbertCurve.h:34
pc
constexpr double pc
SourceXtractor::SourceId
Definition: SourceId.h:31
SourceXtractor
Definition: Aperture.h:30
SourceXtractor::Image::getValue
virtual T getValue(int x, int y) const =0
Returns the value of the pixel with the coordinates (x,y)
SourceXtractor::PixelCoordinate::m_x
int m_x
Definition: PixelCoordinate.h:38
SourceXtractor::BFSSegmentation::m_max_delta
int m_max_delta
Definition: BFSSegmentation.h:76
SourceXtractor::PixelCoordinate::m_y
int m_y
Definition: PixelCoordinate.h:38
HilbertCurve.h
SourceXtractor::BFSSegmentation::VisitedMap::wasVisited
bool wasVisited(PixelCoordinate pc) const
Definition: BFSSegmentation.h:52
std::vector::pop_back
T pop_back(T... args)
TileManager.h
Frame.h
SourceXtractor::BFSSegmentation::m_source_factory
std::shared_ptr< SourceFactory > m_source_factory
Definition: BFSSegmentation.h:75
std::min
T min(T... args)
SourceXtractor::Segmentation::LabellingListener
Definition: Segmentation.h:94
std::vector::emplace_back
T emplace_back(T... args)
BFSSegmentation.h
x
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
Definition: MoffatModelFittingTask.cpp:94
SourceXtractor::HilbertCurve
Definition: HilbertCurve.h:28
MultithreadedMeasurement.h
SourceXtractor::BFSSegmentation::VisitedMap
Definition: BFSSegmentation.h:44
PixelCoordinate.h
std::max
T max(T... args)
y
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > y
Definition: MoffatModelFittingTask.cpp:94
SourceXtractor::BFSSegmentation::labelImage
void labelImage(Segmentation::LabellingListener &listener, std::shared_ptr< const DetectionImageFrame > frame) override
Definition: BFSSegmentation.cpp:38
SourceXtractor::BFSSegmentation::labelSource
void labelSource(PixelCoordinate pc, Segmentation::LabellingListener &listener, DetectionImage &detection_image, VisitedMap &visited_map) const
Definition: BFSSegmentation.cpp:59