SourceXtractorPlusPlus
0.21
SourceXtractor++, the next generation SExtractor
Loading...
Searching...
No Matches
SEImplementation
src
lib
Segmentation
BFSSegmentation.cpp
Go to the documentation of this file.
1
18
#include <memory>
19
#include <vector>
20
#include <list>
21
#include <iostream>
22
23
#include "
SEUtils/PixelCoordinate.h
"
24
#include "
SEUtils/HilbertCurve.h
"
25
26
#include "
SEFramework/Frame/Frame.h
"
27
#include "
SEFramework/Image/ImageAccessor.h
"
28
#include "
SEFramework/Image/TileManager.h
"
29
30
#include "
SEImplementation/Property/PixelCoordinateList.h
"
31
#include "
SEImplementation/Property/SourceId.h
"
32
33
#include "
SEImplementation/Measurement/MultithreadedMeasurement.h
"
34
35
#include "
SEImplementation/Segmentation/BFSSegmentation.h
"
36
37
namespace
SourceXtractor
{
38
39
void
BFSSegmentation::labelImage
(
Segmentation::LabellingListener
&
listener
,
40
std::shared_ptr<const DetectionImageFrame>
frame
) {
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
59
void
BFSSegmentation::labelSource
(
PixelCoordinate
pc,
60
Segmentation::LabellingListener
&
listener
,
61
DetectionImage
&
detection_image
,
62
VisitedMap
&
visited_map
)
const
{
63
using
DetectionAccessor
=
ImageAccessor<DetectionImage::PixelType>
;
64
DetectionAccessor
detectionAccessor
(
detection_image
);
65
66
PixelCoordinate
offsets
[] {
PixelCoordinate
(1,0),
PixelCoordinate
(0,-1),
PixelCoordinate
(-1,0),
PixelCoordinate
(0,1),
67
PixelCoordinate
(-1,-1),
PixelCoordinate
(1,-1),
PixelCoordinate
(-1,1),
PixelCoordinate
(1,1)};
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(
std::move
(
source
));
107
}
108
109
std::vector<BFSSegmentation::Tile>
BFSSegmentation::getTiles
(
const
DetectionImage
&
image
)
const
{
110
int
tile_width
=
TileManager::getInstance
()->getTileWidth();
111
int
tile_height
=
TileManager::getInstance
()->getTileHeight();
112
113
std::vector<BFSSegmentation::Tile>
tiles
;
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()) {
126
tiles
.emplace_back(
BFSSegmentation::Tile
{
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
}
BFSSegmentation.h
Frame.h
HilbertCurve.h
ImageAccessor.h
x
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
Definition
MoffatModelFittingTask.cpp:94
y
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > y
Definition
MoffatModelFittingTask.cpp:94
MultithreadedMeasurement.h
PixelCoordinateList.h
PixelCoordinate.h
SourceId.h
TileManager.h
SourceXtractor::BFSSegmentation::VisitedMap
Definition
BFSSegmentation.h:44
SourceXtractor::BFSSegmentation::m_max_delta
int m_max_delta
Definition
BFSSegmentation.h:76
SourceXtractor::BFSSegmentation::getTiles
std::vector< BFSSegmentation::Tile > getTiles(const DetectionImage &image) const
Definition
BFSSegmentation.cpp:109
SourceXtractor::BFSSegmentation::labelSource
void labelSource(PixelCoordinate pc, Segmentation::LabellingListener &listener, DetectionImage &detection_image, VisitedMap &visited_map) const
Definition
BFSSegmentation.cpp:59
SourceXtractor::BFSSegmentation::labelImage
void labelImage(Segmentation::LabellingListener &listener, std::shared_ptr< const DetectionImageFrame > frame) override
Definition
BFSSegmentation.cpp:39
SourceXtractor::BFSSegmentation::m_source_factory
std::shared_ptr< SourceFactory > m_source_factory
Definition
BFSSegmentation.h:75
SourceXtractor::HilbertCurve
Definition
HilbertCurve.h:28
SourceXtractor::Image< SeFloat >
SourceXtractor::PixelCoordinateList
Definition
PixelCoordinateList.h:33
SourceXtractor::Segmentation::LabellingListener
Definition
Segmentation.h:83
SourceXtractor::SourceId
Definition
SourceId.h:32
SourceXtractor::TileManager::getInstance
static std::shared_ptr< TileManager > getInstance()
Definition
TileManager.cpp:140
std::function
std::max
T max(T... args)
std::min
T min(T... args)
std::move
T move(T... args)
SourceXtractor
Definition
Aperture.h:30
SourceXtractor::BFSSegmentation::Tile
Definition
BFSSegmentation.h:65
SourceXtractor::PixelCoordinate
A pixel coordinate made of two integers m_x and m_y.
Definition
PixelCoordinate.h:37
Generated by
1.10.0