SourceXtractorPlusPlus  0.12
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MedianFilter.h
Go to the documentation of this file.
1 
18 #ifndef SOURCEXTRACTORPLUSPLUS_MEDIANFILTER_H
19 #define SOURCEXTRACTORPLUSPLUS_MEDIANFILTER_H
20 
23 
24 namespace SourceXtractor {
25 
39 template<typename T>
40 class MedianFilter {
41 public:
47  MedianFilter(int box_width, int box_height) : m_box_width(box_width), m_box_height(box_height) {
48  }
49 
55  MedianFilter(const std::array<int, 2>& box) : MedianFilter(box[0], box[1]) {
56  }
57 
70  auto operator()(const Image<T>& image, const Image<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 
98 private:
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 Image<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
MedianFilter(int box_width, int box_height)
Definition: MedianFilter.h:47
void setValue(int x, int y, T value) final
Definition: VectorImage.h:110
auto operator()(const Image< T > &image, const Image< T > &variance, T threshold=0) const -> std::pair< std::shared_ptr< VectorImage< T >>, std::shared_ptr< VectorImage< T >>>
Definition: MedianFilter.h:70
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
std::vector< T > getBox(const Image< T > &img, int x, int y) const
Definition: MedianFilter.h:138
T end(T...args)
static std::shared_ptr< VectorImage< T > > create(Args &&...args)
Definition: VectorImage.h:89
virtual int getHeight() const =0
Returns the height of the image in pixels.
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > y
Image implementation which keeps the pixel values in memory.
Definition: VectorImage.h:53
virtual T getValue(int x, int y) const =0
Returns the value of the pixel with the coordinates (x,y)
T make_pair(T...args)
MedianFilter(const std::array< int, 2 > &box)
Definition: MedianFilter.h:55
T size(T...args)
STL class.
T begin(T...args)
T back_inserter(T...args)
static T getMedian(std::vector< T > &data)
Definition: MedianFilter.h:106
Interface representing an image.
Definition: Image.h:43
static int clip(int position, int box_size, int image_size)
Definition: MedianFilter.h:126
T sort(T...args)
T inserter(T...args)
virtual int getWidth() const =0
Returns the width of the image in pixels.