SourceXtractorPlusPlus  0.12
Please provide a description of the project.
SimpleBackgroundAnalyzer.cpp
Go to the documentation of this file.
1 
17 /*
18  * Background.cpp
19  *
20  * Created on: Oct 11, 2016
21  * Author: mschefer
22  */
23 
26 
27 #include <memory>
28 #include <algorithm>
29 
33 
34 
35 namespace SourceXtractor {
36 
40  WeightImage::PixelType /*variance_threshold*/) const {
41 
42  // FIXME we use a VectorImage the same size as input which won't allow larger than memory images to be processed
43 
44  auto image_copy = VectorImage<DetectionImage::PixelType>::create(*image);
45  std::sort(image_copy->getData().begin(), image_copy->getData().end());
46  bck_model_logger.debug() << "Using the SimpleBackgroundLeverAnalyzer";
47 
48  auto background_level = image_copy->getData()[image_copy->getData().size()/2]; // the median
49  auto background_level_map = ConstantImage<SeFloat>::create(image->getWidth(), image->getHeight(), background_level);
50 
51  auto subtracted_image = SubtractImage<SeFloat>::create(image, background_level_map);
52 
53  auto background_variance = getVariance(subtracted_image);
54  auto background_variance_map = ConstantImage<SeFloat>::create(image->getWidth(), image->getHeight(), background_variance);
55  bck_model_logger.debug() << "bg: " << background_level << " var: " << background_variance;
56 
57  return BackgroundModel(background_level_map, background_variance_map, 1.0, std::sqrt(background_variance));
58 }
59 
60 
62  // Note: We compute the RMS by only taking into consideration pixels
63  // below the background value.
64 
65  double variance = 0;
66  int pixels = 0;
67  for (int y=0; y < image->getHeight(); y++) {
68  for (int x=0; x < image->getWidth(); x++) {
69  auto value = image->getValue(x, y);
70  if (value < 0) {
71  variance += value * value;
72  pixels++;
73  }
74  }
75  }
76 
77  if (pixels > 0) {
78  variance /= pixels;
79  }
80 
81  return variance;
82 }
83 
84 }
85 
BackgroundModel analyzeBackground(std::shared_ptr< DetectionImage > image, std::shared_ptr< WeightImage > variance_map, std::shared_ptr< Image< unsigned char >> mask, WeightImage::PixelType variance_threshold) const override
static Elements::Logging bck_model_logger
Definition: Utils.h:25
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
SeFloat32 SeFloat
Definition: Types.h:32
static SeFloat getVariance(std::shared_ptr< DetectionImage > image)
void debug(const std::string &logMessage)
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > y
static std::shared_ptr< VectorImage< T > > create(Args &&... args)
Definition: VectorImage.h:89
static std::shared_ptr< ConstantImage< T > > create(int width, int height, T constant_value)
Definition: ConstantImage.h:42
Interface representing an image.
Definition: Image.h:43
T sort(T... args)
T sqrt(T... args)
static std::shared_ptr< ProcessedImage< T, P > > create(std::shared_ptr< const Image< T >> image_a, std::shared_ptr< const Image< T >> image_b)