SourceXtractorPlusPlus  0.15
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 
29 
32 
34 
36 
37 namespace SourceXtractor {
38 
41  auto detection_image = frame->getThresholdedImage();
42  auto tiles = getTiles(*detection_image);
43 
44  VisitedMap visited(detection_image->getWidth(), detection_image->getHeight());
45 
46  for (auto& tile : tiles) {
47  auto chunk = detection_image->getChunk(tile.offset.m_x, tile.offset.m_y, tile.width, tile.height);
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) && chunk->getValue(x, y) > 0.0) {
52  labelSource(pixel, listener, *detection_image, visited);
53  }
54  }
55  }
56  }
57 }
58 
61  DetectionImage& detection_image,
62  VisitedMap& visited_map) const {
63  using DetectionAccessor = ImageAccessor<DetectionImage::PixelType>;
64  DetectionAccessor detectionAccessor(detection_image);
65 
68 
69  std::vector<PixelCoordinate> source_pixels;
70  std::vector<PixelCoordinate> pixels_to_process;
71 
72  visited_map.markVisited(pc);
73  pixels_to_process.emplace_back(pc);
74 
75  PixelCoordinate minPixel = pc;
76  PixelCoordinate maxPixel = pc;
77 
78  while (pixels_to_process.size() > 0) {
79  auto pixel = pixels_to_process.back();
80  pixels_to_process.pop_back();
81  source_pixels.emplace_back(pixel);
82 
83  minPixel.m_x = std::min(minPixel.m_x, pixel.m_x);
84  minPixel.m_y = std::min(minPixel.m_y, pixel.m_y);
85  maxPixel.m_x = std::max(maxPixel.m_x, pixel.m_x);
86  maxPixel.m_y = std::max(maxPixel.m_y, pixel.m_y);
87 
88  if (maxPixel.m_x - minPixel.m_x > m_max_delta || maxPixel.m_y - minPixel.m_y > m_max_delta) {
89  // The source extends over a too large area, ignore it
90  return;
91  }
92 
93  for (auto& offset : offsets) {
94  auto new_pixel = pixel + offset;
95 
96  if (!visited_map.wasVisited(new_pixel) && detectionAccessor.getValue(new_pixel) > 0.0) {
97  visited_map.markVisited(new_pixel);
98  pixels_to_process.emplace_back(new_pixel);
99  }
100  }
101  }
102 
103  auto source = m_source_factory->createSource();
104  source->setProperty<PixelCoordinateList>(source_pixels);
105  source->setProperty<SourceId>();
106  listener.publishSource(source);
107 }
108 
110  int tile_width = TileManager::getInstance()->getTileWidth();
111  int tile_height = TileManager::getInstance()->getTileHeight();
112 
114 
115 
116  int size = std::max((image.getWidth() + tile_width - 1) / tile_width,
117  (image.getHeight() + tile_height - 1) / tile_height);
118 
119  HilbertCurve curve(size);
120 
121  for (auto& coord : curve.getCurve()) {
122  int x = coord.m_x * tile_width;
123  int y = coord.m_y * tile_height;
124 
125  if (x < image.getWidth() && y < image.getHeight()) {
127  PixelCoordinate(x, y),
128  std::min(tile_width, image.getWidth() - x),
129  std::min(tile_height, image.getHeight() - y)
130  });
131  }
132  }
133 
134  return tiles;
135 }
136 
137 }
PixelCoordinateList.h
SourceXtractor::PixelCoordinateList
Definition: PixelCoordinateList.h:31
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
SourceXtractor::ImageAccessor
Definition: ImageAccessor.h:41
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:109
SourceXtractor::Image::getHeight
virtual int getHeight() const =0
Returns the height of the image in pixels.
std::vector::back
T back(T... args)
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:32
SourceXtractor
Definition: Aperture.h:30
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
ImageAccessor.h
SourceXtractor::BFSSegmentation::labelImage
void labelImage(Segmentation::LabellingListener &listener, std::shared_ptr< const DetectionImageFrame > frame) override
Definition: BFSSegmentation.cpp:39
SourceXtractor::BFSSegmentation::labelSource
void labelSource(PixelCoordinate pc, Segmentation::LabellingListener &listener, DetectionImage &detection_image, VisitedMap &visited_map) const
Definition: BFSSegmentation.cpp:59
SourceXtractor::TileManager::getInstance
static std::shared_ptr< TileManager > getInstance()
Definition: TileManager.cpp:136