SourceXtractorPlusPlus  0.13
Please provide a description of the project.
SEBackgroundLevelAnalyzer.cpp
Go to the documentation of this file.
1 
19 
29 
30 namespace SourceXtractor {
31 
33  const std::vector<int>& smoothing_box,
34  const WeightImageConfig::WeightType weight_type)
35  : m_weight_type(weight_type) {
36  assert(cell_size.size() > 0 && cell_size.size() < 3);
37  assert(smoothing_box.size() > 0 && smoothing_box.size() < 3);
38  m_cell_size[0] = cell_size.front();
39  m_cell_size[1] = cell_size.back();
40  m_smoothing_box[0] = smoothing_box.front();
41  m_smoothing_box[1] = smoothing_box.back();
42 }
43 
46  std::vector<float> ratios;
47  ratios.reserve(variance->getWidth() * variance->getHeight());
48 
49  for (int y = 0; y < variance->getHeight(); ++y) {
50  for (int x = 0; x < variance->getWidth(); ++x) {
51  auto w = weight->getValue(x, y);
52  if (w > 0) {
53  auto v = variance->getValue(x, y);
54  auto ratio = (v * v) / w;
55  if (ratio > 0) {
56  ratios.emplace_back(ratio);
57  }
58  }
59  }
60  }
61 
62  std::sort(ratios.begin(), ratios.end());
63  if (ratios.size() % 2 == 1) {
64  return ratios[ratios.size() / 2];
65  }
66  return (ratios[ratios.size() / 2] + ratios[ratios.size() / 2 - 1]) / 2;
67 }
68 
70  auto v = img.getData();
71  std::sort(v.begin(), v.end());
72  auto nitems = v.size();
73  if (nitems % 2 == 1) {
74  return v[nitems / 2];
75  }
76  return (v[nitems / 2] + v[nitems / 2 - 1]) / 2;
77 }
78 
81  std::shared_ptr<Image<unsigned char>> mask, WeightImage::PixelType variance_threshold) const {
82 
84 
85  if (mask != nullptr) {
86  bck_model_logger.debug() << "\tMask image with size: (" << mask->getWidth() << "," << mask->getHeight() << ")";
87 
88  // make sure the dimensions are the same
89  if (image->getWidth() != mask->getWidth())
90  throw Elements::Exception() << "X-dims do not match: image=" << image->getWidth() << " mask=" << mask->getWidth();
91  if (image->getHeight() != mask->getHeight())
92  throw Elements::Exception() << "Y-dims do not match: image=" << image->getHeight() << " mask="
93  << mask->getHeight();
94 
95  image = MaskedImage<DetectionImage::PixelType, uint8_t>::create(image, mask, mask_value);
96  }
97 
98  if (variance_map != nullptr) {
99  bck_model_logger.debug() << "\tVariance image with size: (" << variance_map->getWidth() << ","
100  << variance_map->getHeight() << ")";
101  // make sure the dimensions are the same
102  if (image->getWidth() != variance_map->getWidth())
103  throw Elements::Exception() << "X-dims do not match: image=" << image->getWidth() << " variance="
104  << variance_map->getWidth();
105  if (image->getHeight() != variance_map->getHeight())
106  throw Elements::Exception() << "Y-dims do not match: image=" << image->getHeight() << " variance="
107  << variance_map->getHeight();
108 
109  // Anything above the threshold is masked out
111  image, variance_map, mask_value, variance_threshold);
113  variance_map, variance_map, mask_value, variance_threshold);
114  }
115 
116  // Create histogram model for the image
117  ImageMode<DetectionImage::PixelType> histo(image, variance_map, m_cell_size[0], m_cell_size[1], mask_value, 2, 5, 3);
118  auto mode = histo.getModeImage();
119  auto var = histo.getSigmaImage();
120 
121  // Interpolate missing values
122  // The result is "materialized" into a VectorImage to avoid redundant computations on the next steps
127 
128  // Smooth with the smooth_box (median filtering)
130  auto median = getMedian(*mode);
131  auto median_sigma = getMedian(*var);
132 
133  SeFloat scaling = 99999;
134 
136 
137  if (variance_map) {
138  // Create histogram model for the variance image
139  auto weight = histo.getVarianceModeImage();
140  auto weight_var = histo.getVarianceSigmaImage();
141  // Interpolate missing values
144  // Smooth with the smooth_box (median filtering)
145  std::tie(weight, weight_var) = MedianFilter<WeightImage::PixelType>(m_smoothing_box)(*weight, *weight_var);
146  // Compute scaling
147  scaling = computeScaling(var, weight);
148  // Transform RMS to variance
152  final_var, image->getWidth(), image->getHeight(),
154  )
155  );
156  }
157  else {
158  final_var = ConstantImage<DetectionImage::PixelType>::create(image->getWidth(), image->getHeight(),
159  median_sigma * median_sigma);
160  }
161 
162  bck_model_logger.info() << "Background for image: " << image->getRepr() << " median: " << median
163  << " rms: " << median_sigma << "!";
164 
167  mode, image->getWidth(), image->getHeight(),
169  )
170  );
171 
172  return BackgroundModel(final_bg, final_var, scaling, median_sigma);
173 }
174 
175 } // end of namespace SourceXtractor
SEBackgroundLevelAnalyzer.h
std::shared_ptr
STL class.
SourceXtractor::Image< SeFloat >::PixelType
SeFloat PixelType
Definition: Image.h:47
ConstantImage.h
std::vector::reserve
T reserve(T... args)
SourceXtractor::SeFloat
SeFloat32 SeFloat
Definition: Types.h:32
std::make_shared
T make_shared(T... args)
std::vector< int >
std::vector::size
T size(T... args)
SourceXtractor::WeightImageConfig::WeightType
WeightType
Definition: WeightImageConfig.h:36
SourceXtractor::ScaledImageSource
Definition: ScaledImageSource.h:34
SourceXtractor::ImageMode::getVarianceSigmaImage
std::shared_ptr< VectorImage< T > > getVarianceSigmaImage() const
Definition: ImageMode.cpp:84
SourceXtractor::ImageMode::getModeImage
std::shared_ptr< VectorImage< T > > getModeImage() const
Definition: ImageMode.cpp:69
SourceXtractor::BackgroundModel
Definition: BackgroundAnalyzer.h:33
SourceXtractor::MaskedImage::create
static std::shared_ptr< MaskedImage< T, M, Operator > > create(const std::shared_ptr< Image< T >> &image, const std::shared_ptr< Image< M >> &mask, T replacement, M mask_flag=0x01)
Definition: MaskedImage.h:75
std::vector::back
T back(T... args)
SourceXtractor::ImageMode::getSigmaImage
std::shared_ptr< VectorImage< T > > getSigmaImage() const
Definition: ImageMode.cpp:74
SourceXtractor::Image< unsigned char >
SourceXtractor::BufferedImage::create
static std::shared_ptr< BufferedImage< T > > create(std::shared_ptr< const ImageSource > source, std::shared_ptr< TileManager > tile_manager=TileManager::getInstance())
Definition: BufferedImage.cpp:34
std::vector::front
T front(T... args)
std::sort
T sort(T... args)
std::numeric_limits::lowest
T lowest(T... args)
std::tie
T tie(T... args)
SourceXtractor::computeScaling
static float computeScaling(const std::shared_ptr< VectorImage< DetectionImage::PixelType >> &variance, const std::shared_ptr< VectorImage< WeightImage::PixelType >> &weight)
Definition: SEBackgroundLevelAnalyzer.cpp:44
Utils.h
SourceXtractor::SEBackgroundLevelAnalyzer::m_smoothing_box
std::array< int, 2 > m_smoothing_box
Definition: SEBackgroundLevelAnalyzer.h:41
SourceXtractor
Definition: Aperture.h:30
std::ratio
ReplaceUndefImage.h
BufferedImage.h
SourceXtractor::VectorImage::create
static std::shared_ptr< VectorImage< T > > create(Args &&... args)
Definition: VectorImage.h:89
SourceXtractor::ConstantImage::create
static std::shared_ptr< ConstantImage< T > > create(int width, int height, T constant_value)
Definition: ConstantImage.h:42
ScaledImageSource.h
Elements::Exception
SourceXtractor::SEBackgroundLevelAnalyzer::analyzeBackground
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
Definition: SEBackgroundLevelAnalyzer.cpp:79
ProcessedImage.h
Elements::Logging::debug
void debug(const std::string &logMessage)
SourceXtractor::VectorImage::getData
const std::vector< T > & getData() const
Definition: VectorImage.h:127
SourceXtractor::SEBackgroundLevelAnalyzer::m_cell_size
std::array< int, 2 > m_cell_size
Definition: SEBackgroundLevelAnalyzer.h:40
Elements::Logging::info
void info(const std::string &logMessage)
SourceXtractor::ProcessedImage::create
static std::shared_ptr< ProcessedImage< T, P > > create(std::shared_ptr< const Image< T >> image_a, std::shared_ptr< const Image< T >> image_b)
Definition: ProcessedImage.h:54
SourceXtractor::ReplaceUndefImage
Definition: ReplaceUndefImage.h:31
SourceXtractor::ImageMode
Definition: ImageMode.h:44
SourceXtractor::ImageMode::getVarianceModeImage
std::shared_ptr< VectorImage< T > > getVarianceModeImage() const
Definition: ImageMode.cpp:79
std::vector::emplace_back
T emplace_back(T... args)
ImageMode.h
SourceXtractor::bck_model_logger
static Elements::Logging bck_model_logger
Definition: Utils.h:25
SourceXtractor::getMedian
static float getMedian(const VectorImage< DetectionImage::PixelType > &img)
Definition: SEBackgroundLevelAnalyzer.cpp:69
x
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
Definition: MoffatModelFittingTask.cpp:94
SourceXtractor::SEBackgroundLevelAnalyzer::SEBackgroundLevelAnalyzer
SEBackgroundLevelAnalyzer(const std::vector< int > &cell_size, const std::vector< int > &smoothing_box, const WeightImageConfig::WeightType weight_type)
Definition: SEBackgroundLevelAnalyzer.cpp:32
std::vector::begin
T begin(T... args)
SourceXtractor::VectorImage< DetectionImage::PixelType >
MedianFilter.h
SourceXtractor::MedianFilter
Definition: MedianFilter.h:40
std::vector::end
T end(T... args)
MaskedImage.h
y
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > y
Definition: MoffatModelFittingTask.cpp:94