SourceXtractorPlusPlus
0.21
SourceXtractor++, the next generation SExtractor
Loading...
Searching...
No Matches
SEImplementation
src
lib
Partition
AttractorsPartitionStep.cpp
Go to the documentation of this file.
1
22
#include <limits>
23
24
#include "
SEFramework/Image/Image.h
"
25
#include "
SEFramework/Image/ImageAccessor.h
"
26
#include "
SEFramework/Property/DetectionFrame.h
"
27
28
#include "
SEImplementation/Property/PixelCoordinateList.h
"
29
#include "
SEImplementation/Plugin/DetectionFrameSourceStamp/DetectionFrameSourceStamp.h
"
30
#include "
SEImplementation/Plugin/PixelBoundaries/PixelBoundaries.h
"
31
#include "
SEImplementation/Partition/AttractorsPartitionStep.h
"
32
33
namespace
SourceXtractor
{
34
35
std::vector<std::unique_ptr<SourceInterface>
>
AttractorsPartitionStep::partition
(
std::unique_ptr<SourceInterface>
source
)
const
{
36
using
Accessor
=
ImageAccessor<DetectionImage::PixelType>
;
37
Accessor
stamp
(
source
->getProperty<
DetectionFrameSourceStamp
>().
getStamp
());
38
auto
&
detection_frame
=
source
->getProperty<
DetectionFrame
>();
39
auto
&
bounds
=
source
->getProperty<
PixelBoundaries
>();
40
41
auto
bbox_min
=
bounds
.getMin();
42
auto
bbox_max
=
bounds
.getMax();
43
44
auto
value_function
= [
bbox_min
,
bbox_max
, &
stamp
](
PixelCoordinate
coord) {
45
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) {
46
return
std::numeric_limits<DetectionImage::PixelType>::lowest
();
47
}
48
auto
offset_coord
= coord -
bbox_min
;
49
return
stamp
.getValue(
offset_coord
.m_x,
offset_coord
.m_y);
50
};
51
52
std::vector<std::pair<PixelCoordinate, PixelCoordinate>
>
pixel_coordinates
;
53
auto
& pixel_list =
source
->getProperty<
PixelCoordinateList
>().getCoordinateList();
54
pixel_coordinates
.reserve(pixel_list.size());
55
for
(
auto
&
pixel
: pixel_list) {
56
pixel_coordinates
.emplace_back(
pixel
,
pixel
);
57
}
58
59
std::unordered_map<PixelCoordinate, std::vector<PixelCoordinate>
>
attractors
;
60
61
attractPixels
(
pixel_coordinates
,
attractors
,
value_function
);
62
auto
merged
=
mergeAttractors
(
attractors
);
63
64
// If we end up with a single group use the original group
65
std::vector<std::unique_ptr<SourceInterface>
>
sources
;
66
if
(
merged
.size() == 1) {
67
sources
.emplace_back(
std::move
(
source
));
68
}
else
{
69
for
(
auto
&
source_pixels
:
merged
) {
70
auto
new_source
=
m_source_factory
->createSource();
71
new_source
->setProperty<
PixelCoordinateList
>(
source_pixels
);
72
new_source
->setProperty<
DetectionFrame
>(
detection_frame
.getEncapsulatedFrame());
73
sources
.emplace_back(
std::move
(
new_source
));
74
}
75
}
76
return
sources
;
77
}
78
79
void
AttractorsPartitionStep::attractPixels
(
80
const
std::vector
<
std::pair<PixelCoordinate, PixelCoordinate>
>&
pixels_with_origin
,
81
std::unordered_map
<
PixelCoordinate
,
std::vector<PixelCoordinate>
>&
attractors
,
82
std::function
<
DetectionImage::PixelType
(
PixelCoordinate
)>
value_function
)
const
{
83
84
PixelCoordinate
offsets
[5] {
85
PixelCoordinate
( 0, 0),
86
PixelCoordinate
(-1, 0),
87
PixelCoordinate
( 0, -1),
88
PixelCoordinate
( 1, 0),
89
PixelCoordinate
( 0, 1)
90
};
91
92
if
(
pixels_with_origin
.size() == 0) {
93
return
;
94
}
95
96
std::vector<std::pair<PixelCoordinate, PixelCoordinate>
>
pixels_to_be_processed
;
97
98
for
(
auto
&
pixel_origin
:
pixels_with_origin
) {
99
auto
pixel
=
pixel_origin
.first;
100
auto
origin
=
pixel_origin
.second;
101
102
DetectionImage::PixelType
values
[5];
103
for
(
int
i
=0;
i
<5;
i
++) {
104
values
[
i
] =
value_function
(
pixel
+
offsets
[
i
]);
105
}
106
107
size_t
max
= 0;
108
for
(
int
i
=1;
i
<3;
i
++) {
109
if
(
values
[
i
] >
values
[
max
]) {
110
max
=
i
;
111
}
112
}
113
for
(
int
i
=3;
i
<5;
i
++) {
114
if
(
values
[
i
] >=
values
[
max
]) {
115
max
=
i
;
116
}
117
}
118
119
if
(
max
== 0) {
120
// We are at the attractor pixel
121
attractors
[
pixel
].push_back(
origin
);
122
}
else
{
123
pixels_to_be_processed
.push_back(
124
std::pair<PixelCoordinate, PixelCoordinate>
(
pixel
+
offsets
[
max
],
origin
));
125
}
126
}
127
attractPixels
(
pixels_to_be_processed
,
attractors
,
value_function
);
128
}
129
130
std::vector<std::vector<PixelCoordinate>
>
AttractorsPartitionStep::mergeAttractors
(
131
const
std::unordered_map
<
PixelCoordinate
,
std::vector<PixelCoordinate>
>&
attractors
)
const
{
132
std::vector<std::vector<PixelCoordinate>
>
merged
;
133
std::vector<PixelCoordinate>
bbox_min
;
134
std::vector<PixelCoordinate>
bbox_max
;
135
136
for
(
auto
&
attractor
:
attractors
) {
137
auto
coord =
attractor
.first;
138
auto
&
pixels
=
attractor
.second;
139
bool
done =
false
;
140
141
for
(
size_t
i
=0;
i
<
merged
.size();
i
++) {
142
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) {
143
bbox_min
[
i
] =
PixelCoordinate
(
std::min
(coord.m_x,
bbox_min
[
i
].m_x),
std::min
(coord.m_y,
bbox_min
[
i
].m_y));
144
bbox_max
[
i
] =
PixelCoordinate
(
std::max
(coord.m_x,
bbox_max
[
i
].m_x),
std::max
(coord.m_y,
bbox_max
[
i
].m_y));
145
merged
[
i
].insert(
merged
[
i
].
begin
(),
pixels
.begin(),
pixels
.end());
146
done =
true
;
147
break
;
148
}
149
}
150
if
(!done) {
151
merged
.push_back(
pixels
);
152
bbox_min
.push_back(coord);
153
bbox_max
.push_back(coord);
154
}
155
}
156
return
merged
;
157
}
158
159
}
// SEImplementation namespace
160
161
162
AttractorsPartitionStep.h
DetectionFrameSourceStamp.h
DetectionFrame.h
ImageAccessor.h
Image.h
PixelBoundaries.h
PixelCoordinateList.h
std::begin
T begin(T... args)
SourceXtractor::AttractorsPartitionStep::mergeAttractors
std::vector< std::vector< PixelCoordinate > > mergeAttractors(const std::unordered_map< PixelCoordinate, std::vector< PixelCoordinate > > &attractors) const
Definition
AttractorsPartitionStep.cpp:130
SourceXtractor::AttractorsPartitionStep::attractPixels
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
Definition
AttractorsPartitionStep.cpp:79
SourceXtractor::AttractorsPartitionStep::partition
std::vector< std::unique_ptr< SourceInterface > > partition(std::unique_ptr< SourceInterface > source) const override
Definition
AttractorsPartitionStep.cpp:35
SourceXtractor::AttractorsPartitionStep::m_source_factory
std::shared_ptr< SourceFactory > m_source_factory
Definition
AttractorsPartitionStep.h:56
SourceXtractor::DetectionFrameSourceStamp
A copy of the rectangular region of the detection image just large enough to include the whole Source...
Definition
DetectionFrameSourceStamp.h:36
SourceXtractor::DetectionFrameSourceStamp::getStamp
const DetectionVectorImage & getStamp() const
Definition
DetectionFrameSourceStamp.h:59
SourceXtractor::DetectionFrame
Definition
DetectionFrame.h:33
SourceXtractor::Image< SeFloat >::PixelType
SeFloat PixelType
Definition
Image.h:48
SourceXtractor::PixelBoundaries
The bounding box of all the pixels in the source. Both min and max coordinate are inclusive.
Definition
PixelBoundaries.h:37
SourceXtractor::PixelCoordinateList
Definition
PixelCoordinateList.h:33
std::function
std::numeric_limits::lowest
T lowest(T... args)
std::max
T max(T... args)
std::min
T min(T... args)
std::move
T move(T... args)
SourceXtractor
Definition
Aperture.h:30
SourceXtractor::PixelCoordinate
A pixel coordinate made of two integers m_x and m_y.
Definition
PixelCoordinate.h:37
std::unordered_map
std::vector
Generated by
1.10.0