SourceXtractorPlusPlus  0.10
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
39 
41 
42 
43 namespace SourceXtractor {
44 
46  const std::string &smoothing_box,
47  const WeightImageConfig::WeightType weight_type): m_weight_type(weight_type)
48 {
49  // initialize the parameters
50  m_cell_size = stringToIntVec(cell_size, std::string(","));
51  m_smoothing_box=stringToIntVec(smoothing_box, std::string(","));
52 
53  // double a single paramter if necessary
54  if (m_cell_size.size()<1){
55  throw Elements::Exception() << "Can not convert to 'int': '" << cell_size;
56  }
57  else if (m_cell_size.size()<2){
59  }
60  if (m_smoothing_box.size()<1){
61  throw Elements::Exception() << "Can not convert to 'int': '" << smoothing_box;
62  }
63  else if (m_smoothing_box.size()<2){
65  }
66 }
67 
71  WeightImage::PixelType variance_threshold) const {
72 
73  if (mask!=nullptr)
74  {
75  bck_model_logger.debug() << "\tMask image with size: (" << mask->getWidth() << "," << mask->getHeight() << ")";
76 
77  // make sure the dimensions are the same
78  if (image->getWidth()!=mask->getWidth())
79  throw Elements::Exception() << "X-dims do not match: image=" << image->getWidth() << " mask=" << mask->getWidth();
80  if (image->getHeight()!=mask->getHeight())
81  throw Elements::Exception() << "Y-dims do not match: image=" << image->getHeight() << " mask=" << mask->getHeight();
82  }
83 
84  if (variance_map!=nullptr)
85  {
86  bck_model_logger.debug() << "\tVariance image with size: (" << variance_map->getWidth() << "," << variance_map->getHeight() << ")";
87  // make sure the dimensions are the same
88  if (image->getWidth()!=variance_map->getWidth())
89  throw Elements::Exception() << "X-dims do not match: image=" << image->getWidth() << " variance=" << variance_map->getWidth();
90  if (image->getHeight()!=variance_map->getHeight())
91  throw Elements::Exception() << "Y-dims do not match: image=" << image->getHeight() << " variance=" << variance_map->getHeight();
92  }
93 
94  // create the background model
95  SeFloat bck_median;
96  SeFloat var_median;
97  auto bck_model = fromSE2Modeller(image, variance_map, mask, variance_threshold, bck_median, var_median);
98 
99  // give some feedback
100  bck_model_logger.info() << "Background for image: " << image->getRepr() << " median: " << bck_median << " rms: " << sqrt(var_median) << "!";
101 
102  // return model
103  return bck_model;
104 }
105 
107  std::shared_ptr<SE2BackgroundModeller> bck_modeller(new SE2BackgroundModeller(image, variance_map, mask, 0x0001));
110 
111  PIXTYPE sigFac=0.0;
112  //PIXTYPE weightThreshold=0.0;
113 
114  size_t bckCellSize[2] = {size_t(m_cell_size[0]),size_t(m_cell_size[1])};
115  size_t filterBoxSize[2] = {size_t(m_smoothing_box[0]),size_t(m_smoothing_box[1])};
116 
117  // create the background model and the rms model
118  bck_modeller->createSE2Models(splModelBckPtr, splModelVarPtr, sigFac, bckCellSize, variance_threshold, filterBoxSize);
119 
120  // assign the median and variance value
121  bck_median = splModelBckPtr->getMedian();
122  var_median = splModelVarPtr->getMedian();
123 
124  bck_model_logger.debug() << "\tMedian background value: "<< splModelBckPtr->getMedian();
125  bck_model_logger.debug() << "\tMedian variance value: "<< splModelVarPtr->getMedian();
126  bck_model_logger.debug() << "\tScaling value: "<< sigFac;
127 
128  // check for the weight type
130  bck_model_logger.debug() << "\tConstant variance image at value: "<< splModelVarPtr->getMedian();
131  // create a background model using the splines and the variance with a constant image from the median value
132  return BackgroundModel(BufferedImage<SeFloat>::create(splModelBckPtr),
133  ConstantImage<SeFloat>::create(image->getWidth(), image->getHeight(), splModelVarPtr->getMedian()),
134  99999);
135  }
136  else {
137  bck_model_logger.debug() << "\tVariable background and variance.";
138  // return the variable background model
139  return BackgroundModel(
140  BufferedImage<SeFloat>::create(splModelBckPtr),
141  BufferedImage<SeFloat>::create(splModelVarPtr),
142  sigFac
143  );
144  }
145 }
146 
148 {
149  std::vector<int> result;
150  int anInt=0;
151  std::size_t first;
152  std::size_t last;
153 
154  // convert the input string to a vector of strings along the commas
155  std::vector<std::string> stringVec=stringSplit(inString, delimiters);
156 
157  // go over all members
158  for (size_t index=0; index<stringVec.size(); index++)
159  {
160  // prepare trimming
161  first = stringVec[index].find_first_not_of(' ');
162  last = stringVec[index].find_last_not_of(' ');
163 
164  try
165  {
166  // try converting to int and append to result vector
167  anInt = boost::lexical_cast<size_t>(stringVec[index].substr(first, last-first+1));
168  result.push_back(anInt);
169  }
170  catch ( const boost::bad_lexical_cast &exc ) // conversion failed, exception thrown by lexical_cast and caught
171  {
172  throw Elements::Exception() << "Can not convert to 'int': '" << stringVec[index].substr(first, last-first+1) << "'";
173  }
174  }
175 
176  return result;
177 }
178 
180 {
182  std::string trimString;
183  size_t current;
184  size_t next = -1;
185  size_t first=0;
186  size_t last=0;
187 
188  // trim blanks from both sides;
189  // return the empty vector if there
190  // are only blanks
191  first = inString.find_first_not_of(' ');
192  if (first == std::string::npos)
193  return result;
194  last = inString.find_last_not_of(' ');
195  trimString = inString.substr(first, last-first+1);
196 
197  do
198  { // split along the delimiter
199  // and add to the result vector
200  current = next + 1;
201  next = trimString.find_first_of( delimiters, current );
202  result.push_back(trimString.substr( current, next - current ));
203  }
204  while (next != std::string::npos);
205 
206  return result;
207 }
208 
209 
210 }
STL class.
T find_first_not_of(T... args)
static Elements::Logging bck_model_logger
void info(const std::string &logMessage)
SeFloat32 SeFloat
Definition: Types.h:32
void debug(const std::string &logMessage)
STL class.
std::vector< std::string > stringSplit(const std::string inString, const std::string delimiters)
T push_back(T... args)
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 next(T... args)
T find_first_of(T... args)
T find_last_not_of(T... args)
T size(T... args)
T substr(T... args)
SE2BackgroundLevelAnalyzer(const std::string &cell_size, const std::string &smoothing_box, const WeightImageConfig::WeightType weight_type)
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
T sqrt(T... args)
std::vector< int > stringToIntVec(const std::string inString, const std::string delimiters)