SourceXtractorPlusPlus  0.15
Please provide a description of the project.
SE2BackgroundLevelAnalyzer.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 
24 #include <memory>
25 #include <algorithm>
26 
27 #include <iostream>
28 
29 #include <boost/lexical_cast.hpp>
31 #include "ElementsKernel/Exception.h" // for Elements Exception
32 #include "ElementsKernel/Logging.h" // for Logging::LogMessageStream, etc
40 
41 
42 namespace SourceXtractor {
43 
45  const std::vector<int>& smoothing_box,
46  const WeightImageConfig::WeightType weight_type)
47  : m_weight_type(weight_type)
48 {
49  assert(cell_size.size() > 0 && cell_size.size() <= 2);
50  assert(smoothing_box.size() > 0 && smoothing_box.size() <= 2);
51  m_cell_size[0] = cell_size.front();
52  m_cell_size[1] = cell_size.back();
53  assert(m_cell_size[0] > 0 && m_cell_size[1] > 0);
54  m_smoothing_box[0] = smoothing_box.front();
55  m_smoothing_box[1] = smoothing_box.back();
56  assert(m_smoothing_box[0] >= 0 && m_smoothing_box[1] >= 0);
57 }
58 
62  WeightImage::PixelType variance_threshold) const {
63 
64  if (mask!=nullptr)
65  {
66  bck_model_logger.debug() << "\tMask image with size: (" << mask->getWidth() << "," << mask->getHeight() << ")";
67 
68  // make sure the dimensions are the same
69  if (image->getWidth()!=mask->getWidth())
70  throw Elements::Exception() << "X-dims do not match: image=" << image->getWidth() << " mask=" << mask->getWidth();
71  if (image->getHeight()!=mask->getHeight())
72  throw Elements::Exception() << "Y-dims do not match: image=" << image->getHeight() << " mask=" << mask->getHeight();
73  }
74 
75  if (variance_map!=nullptr)
76  {
77  bck_model_logger.debug() << "\tVariance image with size: (" << variance_map->getWidth() << "," << variance_map->getHeight() << ")";
78  // make sure the dimensions are the same
79  if (image->getWidth()!=variance_map->getWidth())
80  throw Elements::Exception() << "X-dims do not match: image=" << image->getWidth() << " variance=" << variance_map->getWidth();
81  if (image->getHeight()!=variance_map->getHeight())
82  throw Elements::Exception() << "Y-dims do not match: image=" << image->getHeight() << " variance=" << variance_map->getHeight();
83  }
84 
85  // create the background model
86  SeFloat bck_median;
87  SeFloat var_median;
88  auto bck_model = fromSE2Modeller(image, variance_map, mask, variance_threshold, bck_median, var_median);
89 
90  // give some feedback
91  bck_model_logger.info() << "Background for image: " << image->getRepr() << " median: " << bck_median << " rms: " << sqrt(var_median) << "!";
92 
93  // return model
94  return bck_model;
95 }
96 
98  std::shared_ptr<SE2BackgroundModeller> bck_modeller(new SE2BackgroundModeller(image, variance_map, mask, 0x0001));
101 
102  PIXTYPE sigFac=0.0;
103  //PIXTYPE weightThreshold=0.0;
104 
105  size_t bckCellSize[2] = {size_t(m_cell_size[0]),size_t(m_cell_size[1])};
106  size_t filterBoxSize[2] = {size_t(m_smoothing_box[0]),size_t(m_smoothing_box[1])};
107 
108  // create the background model and the rms model
109  bck_modeller->createSE2Models(splModelBckPtr, splModelVarPtr, sigFac, bckCellSize, variance_threshold, filterBoxSize);
110 
111  // assign the median and variance value
112  bck_median = splModelBckPtr->getMedian();
113  var_median = splModelVarPtr->getMedian();
114 
115  auto medianVariance = splModelVarPtr->getMedian();
116  bck_model_logger.debug() << "\tMedian background value: "<< splModelBckPtr->getMedian();
117  bck_model_logger.debug() << "\tMedian variance value: " << medianVariance;
118  bck_model_logger.debug() << "\tScaling value: "<< sigFac;
119 
120  // check for the weight type
122  bck_model_logger.debug() << "\tConstant variance image at value: "<< splModelVarPtr->getMedian();
123  // create a background model using the splines and the variance with a constant image from the median value
124  return BackgroundModel(BufferedImage<SeFloat>::create(splModelBckPtr),
125  ConstantImage<SeFloat>::create(image->getWidth(), image->getHeight(), splModelVarPtr->getMedian()),
126  99999, std::sqrt(medianVariance));
127  }
128  else {
129  bck_model_logger.debug() << "\tVariable background and variance.";
130  // return the variable background model
131  return BackgroundModel(
132  BufferedImage<SeFloat>::create(splModelBckPtr),
133  BufferedImage<SeFloat>::create(splModelVarPtr),
134  sigFac, std::sqrt(medianVariance)
135  );
136  }
137 }
138 
139 }
T back(T... args)
void debug(const std::string &logMessage)
void info(const std::string &logMessage)
Interface representing an image.
Definition: Image.h:43
BackgroundModel fromSE2Modeller(std::shared_ptr< DetectionImage > image, std::shared_ptr< WeightImage > variance_map, std::shared_ptr< Image< unsigned char >> mask, WeightImage::PixelType variance_threshold, SeFloat &bck_median, SeFloat &var_median) const
SE2BackgroundLevelAnalyzer(const std::vector< int > &cell_size, const std::vector< int > &smoothing_box, const WeightImageConfig::WeightType weight_type)
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
T front(T... args)
SeFloat32 SeFloat
Definition: Types.h:32
static Elements::Logging bck_model_logger
Definition: Utils.h:25
T size(T... args)
T sqrt(T... args)