SourceXtractorPlusPlus 1.0.3
SourceXtractor++, the next generation SExtractor
Loading...
Searching...
No Matches
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
27
28#include <memory>
29#include <algorithm>
30
34
35
36namespace SourceXtractor {
37
41 WeightImage::PixelType /*variance_threshold*/) const {
42
43 // FIXME we use a VectorImage the same size as input which won't allow larger than memory images to be processed
44
45 auto image_copy = VectorImage<DetectionImage::PixelType>::create(*image);
46 std::sort(image_copy->getData().begin(), image_copy->getData().end());
47 bck_model_logger.debug() << "Using the SimpleBackgroundLeverAnalyzer";
48
49 auto background_level = image_copy->getData()[image_copy->getData().size()/2]; // the median
50 auto background_level_map = ConstantImage<SeFloat>::create(image->getWidth(), image->getHeight(), background_level);
51
52 auto subtracted_image = SubtractImage<SeFloat>::create(image, background_level_map);
53
54 auto background_variance = getVariance(subtracted_image);
55 auto background_variance_map = ConstantImage<SeFloat>::create(image->getWidth(), image->getHeight(), background_variance);
56 bck_model_logger.debug() << "bg: " << background_level << " var: " << background_variance;
57
58 return BackgroundModel(background_level_map, background_variance_map, 1.0, std::sqrt(background_variance));
59}
60
61
63 // Note: We compute the RMS by only taking into consideration pixels
64 // below the background value.
66 Accessor accessor(image, Accessor::TOP_LEFT, 256, 1);
67
68 double variance = 0;
69 int pixels = 0;
70 for (int y = 0; y < accessor.getHeight(); y++) {
71 for (int x = 0; x < accessor.getWidth(); x++) {
72 auto value = accessor.getValue(x, y);
73 if (value < 0) {
74 variance += value * value;
75 pixels++;
76 }
77 }
78 }
79
80 if (pixels > 0) {
81 variance /= pixels;
82 }
83
84 return variance;
85}
86
87}
88
static std::shared_ptr< ConstantImage< T > > create(int width, int height, T constant_value)
Interface representing an image.
Definition Image.h:44
static std::shared_ptr< ProcessedImage< T, SubtractOperation< T > > > create(std::shared_ptr< const Image< T > > image_a, std::shared_ptr< const Image< T > > image_b)
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 SeFloat getVariance(std::shared_ptr< DetectionImage > image)
static std::shared_ptr< VectorImage< T > > create(Args &&... args)
SeFloat32 SeFloat
Definition Types.h:32
static Elements::Logging bck_model_logger
Definition Utils.h:25
T sort(T... args)
T sqrt(T... args)