SourceXtractorPlusPlus
0.21
SourceXtractor++, the next generation SExtractor
Loading...
Searching...
No Matches
SEImplementation
SEImplementation
Background
SE
MedianFilter.h
Go to the documentation of this file.
1
18
#ifndef SOURCEXTRACTORPLUSPLUS_MEDIANFILTER_H
19
#define SOURCEXTRACTORPLUSPLUS_MEDIANFILTER_H
20
21
#include "
SEFramework/Image/VectorImage.h
"
22
#include "
SEFramework/Image/WriteableImage.h
"
23
#include <array>
24
25
namespace
SourceXtractor
{
26
40
template
<
typename
T>
41
class
MedianFilter
{
42
public
:
48
MedianFilter
(
int
box_width
,
int
box_height
) :
m_box_width
(
box_width
),
m_box_height
(
box_height
) {
49
}
50
56
explicit
MedianFilter
(
const
std::array<int, 2>
&
box
) :
MedianFilter
(
box
[0],
box
[1]) {
57
}
58
71
auto
operator()
(
const
VectorImage<T>
&
image
,
const
VectorImage<T>
&
variance
,
72
T
threshold
= 0)
const
->
std
::
pair
<
std
::
shared_ptr
<
VectorImage
<T>>,
std
::
shared_ptr
<
VectorImage
<T>>> {
73
assert
(
image
.getWidth() ==
variance
.getWidth());
74
assert
(
image
.getHeight() ==
variance
.getHeight());
75
76
auto
out_img
=
VectorImage<T>::create
(
image
.getWidth(),
image
.getHeight());
77
auto
out_var
=
VectorImage<T>::create
(
image
.getWidth(),
image
.getHeight());
78
79
for
(
int
y
= 0;
y
<
image
.getHeight(); ++
y
) {
80
for
(
int
x
= 0;
x
<
image
.getWidth(); ++
x
) {
81
auto
box
=
getBox
(
image
,
x
,
y
);
82
auto
median
=
getMedian
(
box
);
83
auto
value =
image
.getValue(
x
,
y
);
84
if
(
std::abs
(
median
- value) >=
threshold
) {
85
out_img
->setValue(
x
,
y
,
median
);
86
auto
var_box
=
getBox
(
variance
,
x
,
y
);
87
out_var
->setValue(
x
,
y
,
getMedian
(
var_box
));
88
}
89
else
{
90
out_img
->setValue(
x
,
y
, value);
91
out_var
->setValue(
x
,
y
,
variance
.getValue(
x
,
y
));
92
}
93
}
94
}
95
96
return
std::make_pair
(
out_img
,
out_var
);
97
}
98
99
private
:
100
int
m_box_width
,
m_box_height
;
101
107
static
T
getMedian
(
std::vector<T>
& data) {
108
std::sort
(data.begin(), data.end());
109
auto
nitems
= data.size();
110
if
(
nitems
% 2 == 1)
111
return
data[
nitems
/ 2];
112
return
(data[
nitems
/ 2] + data[
nitems
/ 2 - 1]) / 2;
113
}
114
127
static
int
clip
(
int
position,
int
box_size
,
int
image_size
) {
128
box_size
/= 2;
129
if
(
box_size
> position)
130
return
position;
131
if
(
box_size
>
image_size
- position - 1)
132
return
image_size
- position - 1;
133
return
box_size
;
134
}
135
139
std::vector<T>
getBox
(
const
VectorImage<T>
&
img
,
int
x
,
int
y
)
const
{
140
int
hw
=
clip
(
x
,
m_box_width
,
img
.getWidth());
141
int
hh
=
clip
(
y
,
m_box_height
,
img
.getHeight());
142
std::vector<T>
data;
143
auto
inserter
=
std::back_inserter
(data);
144
for
(
int
iy
= -
hh
;
iy
<
hh
+ 1; ++
iy
) {
145
for
(
int
ix
= -
hw
;
ix
<
hw
+ 1; ++
ix
) {
146
*
inserter
=
img
.getValue(
x
+
ix
,
y
+
iy
);
147
++
inserter
;
148
}
149
}
150
return
data;
151
}
152
};
153
154
}
// end of namespace SourceXtractor
155
156
#endif
//SOURCEXTRACTORPLUSPLUS_MEDIANFILTER_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
VectorImage.h
WriteableImage.h
std::back_inserter
T back_inserter(T... args)
SourceXtractor::MedianFilter
Definition
MedianFilter.h:41
SourceXtractor::MedianFilter::getBox
std::vector< T > getBox(const VectorImage< T > &img, int x, int y) const
Definition
MedianFilter.h:139
SourceXtractor::MedianFilter::MedianFilter
MedianFilter(int box_width, int box_height)
Definition
MedianFilter.h:48
SourceXtractor::MedianFilter::m_box_width
int m_box_width
Definition
MedianFilter.h:100
SourceXtractor::MedianFilter::getMedian
static T getMedian(std::vector< T > &data)
Definition
MedianFilter.h:107
SourceXtractor::MedianFilter::clip
static int clip(int position, int box_size, int image_size)
Definition
MedianFilter.h:127
SourceXtractor::MedianFilter::m_box_height
int m_box_height
Definition
MedianFilter.h:100
SourceXtractor::MedianFilter::operator()
auto operator()(const VectorImage< T > &image, const VectorImage< T > &variance, T threshold=0) const -> std::pair< std::shared_ptr< VectorImage< T > >, std::shared_ptr< VectorImage< T > > >
Definition
MedianFilter.h:71
SourceXtractor::MedianFilter::MedianFilter
MedianFilter(const std::array< int, 2 > &box)
Definition
MedianFilter.h:56
SourceXtractor::VectorImage
Image implementation which keeps the pixel values in memory.
Definition
VectorImage.h:52
SourceXtractor::VectorImage::create
static std::shared_ptr< VectorImage< T > > create(Args &&... args)
Definition
VectorImage.h:100
std::function
std::inserter
T inserter(T... args)
std::make_pair
T make_pair(T... args)
SourceXtractor
Definition
Aperture.h:30
std
STL namespace.
std::pair
std::shared_ptr
std::sort
T sort(T... args)
Generated by
1.10.0