SourceXtractorPlusPlus 0.21
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#include <array>
24
25namespace SourceXtractor {
26
40template<typename T>
42public:
50
57 }
58
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
97 }
98
99private:
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
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > y
T back_inserter(T... args)
std::vector< T > getBox(const VectorImage< T > &img, int x, int y) const
MedianFilter(int box_width, int box_height)
static T getMedian(std::vector< T > &data)
static int clip(int position, int box_size, int image_size)
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 > > >
MedianFilter(const std::array< int, 2 > &box)
Image implementation which keeps the pixel values in memory.
Definition VectorImage.h:52
static std::shared_ptr< VectorImage< T > > create(Args &&... args)
T inserter(T... args)
T make_pair(T... args)
STL namespace.
T sort(T... args)