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