lsst.afw  13.0-110-ge674a0930
Background.h
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008-2015 AURA/LSST.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <https://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 #if !defined(LSST_AFW_MATH_BACKGROUND_H)
26 #define LSST_AFW_MATH_BACKGROUND_H
27 /*
28  * Estimate image backgrounds
29  */
30 #include <boost/preprocessor/seq.hpp>
31 #include <memory>
32 #include "lsst/daf/base/Citizen.h"
33 #include "lsst/pex/exceptions.h"
34 #include "lsst/afw/geom/Box.h"
38 
39 namespace lsst {
40 namespace afw {
41 namespace math {
42 
43 //
44 // Remember to update stringToUndersampleStyle if you change this.
45 // If this happens often, we can play CPP games to put the definition in exactly one place, although swig
46 // may not be happy (so we could think m4 thoughts instead)
47 //
52 UndersampleStyle stringToUndersampleStyle(std::string const& style);
53 
58 public:
66  BackgroundControl(int const nxSample, int const nySample,
67  StatisticsControl const sctrl = StatisticsControl(), Property const prop = MEANCLIP,
69  : _style(Interpolate::AKIMA_SPLINE),
70  _nxSample(nxSample),
71  _nySample(nySample),
72  _undersampleStyle(THROW_EXCEPTION),
73  _sctrl(new StatisticsControl(sctrl)),
74  _prop(prop),
75  _actrl(new ApproximateControl(actrl)) {
76  if (nxSample <= 0 || nySample <= 0) {
77  throw LSST_EXCEPT(lsst::pex::exceptions::LengthError,
78  str(boost::format("You must specify at least one point, not %dx%d") % nxSample %
79  nySample));
80  }
81  }
82 
92  BackgroundControl(int const nxSample, int const nySample, StatisticsControl const& sctrl,
93  std::string const& prop,
95  : _style(Interpolate::AKIMA_SPLINE),
96  _nxSample(nxSample),
97  _nySample(nySample),
98  _undersampleStyle(THROW_EXCEPTION),
99  _sctrl(new StatisticsControl(sctrl)),
100  _prop(stringToStatisticsProperty(prop)),
101  _actrl(new ApproximateControl(actrl)) {
102  if (nxSample <= 0 || nySample <= 0) {
103  throw LSST_EXCEPT(lsst::pex::exceptions::LengthError,
104  str(boost::format("You must specify at least one point, not %dx%d") % nxSample %
105  nySample));
106  }
107  }
108  // And now the two old APIs (preserved for backward compatibility)
120  BackgroundControl(Interpolate::Style const style, int const nxSample = 10, int const nySample = 10,
121  UndersampleStyle const undersampleStyle = THROW_EXCEPTION,
122  StatisticsControl const sctrl = StatisticsControl(), Property const prop = MEANCLIP,
124 
125  )
126  : _style(style),
127  _nxSample(nxSample),
128  _nySample(nySample),
129  _undersampleStyle(undersampleStyle),
130  _sctrl(new StatisticsControl(sctrl)),
131  _prop(prop),
132  _actrl(new ApproximateControl(actrl)) {
133  if (nxSample <= 0 || nySample <= 0) {
134  throw LSST_EXCEPT(lsst::pex::exceptions::LengthError,
135  str(boost::format("You must specify at least one point, not %dx%d") % nxSample %
136  nySample));
137  }
138  }
139 
153  BackgroundControl(std::string const& style, int const nxSample = 10, int const nySample = 10,
154  std::string const& undersampleStyle = "THROW_EXCEPTION",
155  StatisticsControl const sctrl = StatisticsControl(),
156  std::string const& prop = "MEANCLIP",
158  : _style(math::stringToInterpStyle(style)),
159  _nxSample(nxSample),
160  _nySample(nySample),
161  _undersampleStyle(math::stringToUndersampleStyle(undersampleStyle)),
162  _sctrl(new StatisticsControl(sctrl)),
163  _prop(stringToStatisticsProperty(prop)),
164  _actrl(new ApproximateControl(actrl)) {
165  if (nxSample <= 0 || nySample <= 0) {
166  throw LSST_EXCEPT(lsst::pex::exceptions::LengthError,
167  str(boost::format("You must specify at least one point, not %dx%d") % nxSample %
168  nySample));
169  }
170  }
171 
172  virtual ~BackgroundControl() {}
173  void setNxSample(int nxSample) {
174  if (nxSample <= 0) {
175  throw LSST_EXCEPT(lsst::pex::exceptions::LengthError,
176  str(boost::format("nxSample must be position, not %d") % nxSample));
177  }
178  _nxSample = nxSample;
179  }
180  void setNySample(int nySample) {
181  if (nySample <= 0) {
182  throw LSST_EXCEPT(lsst::pex::exceptions::LengthError,
183  str(boost::format("nySample must be position, not %d") % nySample));
184  }
185  _nySample = nySample;
186  }
187 
188  void setInterpStyle(Interpolate::Style const style) { _style = style; }
189  // overload to take a string
190  void setInterpStyle(std::string const& style) { _style = math::stringToInterpStyle(style); }
191 
192  void setUndersampleStyle(UndersampleStyle const undersampleStyle) {
193  _undersampleStyle = undersampleStyle;
194  }
195  // overload to take a string
196  void setUndersampleStyle(std::string const& undersampleStyle) {
197  _undersampleStyle = math::stringToUndersampleStyle(undersampleStyle);
198  }
199 
200  int getNxSample() const { return _nxSample; }
201  int getNySample() const { return _nySample; }
203  if (_style < 0 || _style >= Interpolate::NUM_STYLES) {
204  throw LSST_EXCEPT(lsst::pex::exceptions::InvalidParameterError,
205  str(boost::format("Style %d is invalid") % _style));
206  }
207  return _style;
208  }
209  UndersampleStyle getUndersampleStyle() const { return _undersampleStyle; }
210  std::shared_ptr<StatisticsControl> getStatisticsControl() { return _sctrl; }
211  std::shared_ptr<StatisticsControl const> getStatisticsControl() const { return _sctrl; }
212 
213  Property getStatisticsProperty() const { return _prop; }
214  void setStatisticsProperty(Property prop) { _prop = prop; }
215  void setStatisticsProperty(std::string prop) { _prop = stringToStatisticsProperty(prop); }
216 
217  void setApproximateControl(std::shared_ptr<ApproximateControl> actrl) { _actrl = actrl; }
218  std::shared_ptr<ApproximateControl> getApproximateControl() { return _actrl; }
219  std::shared_ptr<ApproximateControl const> getApproximateControl() const { return _actrl; }
220 
221 private:
222  Interpolate::Style _style; // style of interpolation to use
223  int _nxSample; // number of grid squares to divide image into to sample in x
224  int _nySample; // number of grid squares to divide image into to sample in y
225  UndersampleStyle _undersampleStyle; // what to do when nx,ny are too small for the requested interp style
226  std::shared_ptr<StatisticsControl> _sctrl; // statistics control object
227  Property _prop; // statistics Property
228  std::shared_ptr<ApproximateControl> _actrl; // approximate control object
229 };
230 
234 class Background : public daf::base::Citizen {
235 protected:
249  template <typename ImageT>
250  explicit Background(ImageT const& img, BackgroundControl const& bgCtrl);
251 
262  explicit Background(geom::Box2I const imageBBox, int const nx, int const ny);
264  virtual ~Background() {}
265 
266 public:
267  typedef float InternalPixelT;
268 
270  virtual Background& operator+=(float const delta) = 0;
272  virtual Background& operator-=(float const delta) = 0;
280  template <typename PixelT>
281  std::shared_ptr<lsst::afw::image::Image<PixelT>> getImage(
282  Interpolate::Style const interpStyle,
283  UndersampleStyle const undersampleStyle = THROW_EXCEPTION) const {
284  return getImage<PixelT>(_imgBBox, interpStyle, undersampleStyle);
285  }
293  template <typename PixelT>
294  std::shared_ptr<lsst::afw::image::Image<PixelT>> getImage(
295  std::string const& interpStyle, std::string const& undersampleStyle = "THROW_EXCEPTION") const {
296  return getImage<PixelT>(math::stringToInterpStyle(interpStyle),
297  stringToUndersampleStyle(undersampleStyle));
298  }
304  template <typename PixelT>
305  std::shared_ptr<lsst::afw::image::Image<PixelT>> getImage(
306  lsst::afw::geom::Box2I const& bbox, Interpolate::Style const interpStyle,
307  UndersampleStyle const undersampleStyle = THROW_EXCEPTION) const {
308  return _getImage(bbox, interpStyle, undersampleStyle, static_cast<PixelT>(0));
309  }
315  template <typename PixelT>
316  std::shared_ptr<lsst::afw::image::Image<PixelT>> getImage(
317  lsst::afw::geom::Box2I const& bbox, std::string const& interpStyle,
318  std::string const& undersampleStyle = "THROW_EXCEPTION") const {
319  return _getImage(bbox, math::stringToInterpStyle(interpStyle),
320  stringToUndersampleStyle(undersampleStyle), static_cast<PixelT>(0));
321  }
322 
327  template <typename PixelT>
328  std::shared_ptr<lsst::afw::image::Image<PixelT>> getImage() const {
329  return getImage<PixelT>(_bctrl->getInterpStyle(), _bctrl->getUndersampleStyle());
330  }
336  Interpolate::Style getAsUsedInterpStyle() const { return _asUsedInterpStyle; }
340  UndersampleStyle getAsUsedUndersampleStyle() const { return _asUsedUndersampleStyle; }
347  std::shared_ptr<math::Approximate<InternalPixelT>> getApproximate(
348  ApproximateControl const& actrl,
349  UndersampleStyle const undersampleStyle = THROW_EXCEPTION) const {
350  InternalPixelT disambiguate = 0;
351  return _getApproximate(actrl, undersampleStyle, disambiguate);
352  }
356  geom::Box2I getImageBBox() const { return _imgBBox; }
357 
358  std::shared_ptr<BackgroundControl> getBackgroundControl() { return _bctrl; }
359  std::shared_ptr<BackgroundControl const> getBackgroundControl() const { return _bctrl; }
360 
361 protected:
363  std::shared_ptr<BackgroundControl> _bctrl;
366 
367  std::vector<double> _xcen;
368  std::vector<double> _ycen;
369  std::vector<int> _xorig;
370  std::vector<int> _yorig;
371  std::vector<int> _xsize;
372  std::vector<int> _ysize;
373  /*
374  * We want getImage to be present in the base class, but a templated virtual function
375  * is impossible. So we'll solve the dilemma with a hack: explicitly defined
376  * virtual functions for the image types we need
377  */
378 // We'll evaluate LSST_makeBackground_get{Approximation,Image} for each type in
379 // LSST_makeBackground_get{Approximation,Image}_types,
380 // setting v to the second arg (i.e. "= 0" for the first invocation). The first agument, m, is ignores
381 
382 // Desired types
383 #define LSST_makeBackground_getImage_types (Background::InternalPixelT)
384 #define LSST_makeBackground_getApproximate_types (Background::InternalPixelT)
385 #define LSST_makeBackground_getImage(m, v, T) \
386  virtual std::shared_ptr<lsst::afw::image::Image<T>> _getImage( \
387  lsst::afw::geom::Box2I const& bbox, \
388  Interpolate::Style const interpStyle, /* Style of the interpolation */ \
389  UndersampleStyle const undersampleStyle = \
390  THROW_EXCEPTION, /* Behaviour if there are too few points */ \
391  T = 0 /* disambiguate */ \
392  ) const v;
393 
394 #define LSST_makeBackground_getApproximate(m, v, T) \
395  virtual std::shared_ptr<Approximate<T>> _getApproximate( \
396  ApproximateControl const& actrl, /* Approximation style */ \
397  UndersampleStyle const undersampleStyle = \
398  THROW_EXCEPTION, /* Behaviour if there are too few points */ \
399  T = 0 /* disambiguate */ \
400  ) const v;
401 
404 private:
405  Background(Background const&);
406  Background& operator=(Background const&);
411  void _setCenOrigSize(int const width, int const height, int const nxSample, int const nySample);
412 };
413 
436 class BackgroundMI : public Background {
437 public:
438  template <typename ImageT>
465  explicit BackgroundMI(ImageT const& img, BackgroundControl const& bgCtrl);
472  explicit BackgroundMI(geom::Box2I const imageDimensions,
473  image::MaskedImage<InternalPixelT> const& statsImage);
474 
480  virtual BackgroundMI& operator+=(float const delta);
486  virtual BackgroundMI& operator-=(float const delta);
487 
500  double getPixel(Interpolate::Style const style, int const x, int const y) const;
508  double getPixel(int const x, int const y) const { return getPixel(_bctrl->getInterpStyle(), x, y); }
513 
514 private:
516  _statsImage; // statistical properties for the grid of subimages
517  mutable std::vector<std::vector<double>> _gridColumns; // interpolated columns for the bicubic spline
518 
519  void _setGridColumns(Interpolate::Style const interpStyle, UndersampleStyle const undersampleStyle,
520  int const iX, std::vector<int> const& ypix) const;
521 
522 #if defined(LSST_makeBackground_getImage)
525 #if 0 // keep for use in Background instantiations
526 #undef LSST_makeBackground_getImage_types
527 #undef LSST_makeBackground_getApproximate_types
528 #endif
529 #undef LSST_makeBackground_getImage
530 #undef LSST_makeBackground_getApproximate
531 #endif
532  // Here's the worker function for _getImage (non-virtual; it's templated in BackgroundMI, not Background)
536  template <typename PixelT>
537  std::shared_ptr<image::Image<PixelT>> doGetImage(geom::Box2I const& bbox,
538  Interpolate::Style const interpStyle_,
539  UndersampleStyle const undersampleStyle) const;
540  // and for _getApproximate
541  template <typename PixelT>
542  std::shared_ptr<Approximate<PixelT>> doGetApproximate(ApproximateControl const& actrl,
543  UndersampleStyle const undersampleStyle) const;
544 };
550 template <typename ImageT>
551 std::shared_ptr<Background> makeBackground(ImageT const& img, BackgroundControl const& bgCtrl) {
552  return std::shared_ptr<Background>(new BackgroundMI(img, bgCtrl));
553 }
554 }
555 }
556 } // lsst::afw::math
557 
558 #endif // LSST_AFW_MATH_BACKGROUND_H
float InternalPixelT
type used for any internal images, and returned by getApproximate
Definition: Background.h:267
std::vector< int > _xsize
x size of sub images
Definition: Background.h:371
UndersampleStyle stringToUndersampleStyle(std::string const &style)
Conversion function to switch a string to an UndersampleStyle.
Definition: Background.cc:119
Interpolate::Style getAsUsedInterpStyle() const
Return the Interpolate::Style that we actually used in the last call to getImage() ...
Definition: Background.h:336
std::vector< double > _xcen
x center pix coords of sub images
Definition: Background.h:367
Interpolate::Style stringToInterpStyle(std::string const &style)
Conversion function to switch a string to an Interpolate::Style.
Definition: Interpolate.cc:257
std::shared_ptr< lsst::afw::image::Image< PixelT > > getImage(std::string const &interpStyle, std::string const &undersampleStyle="THROW_EXCEPTION") const
Method to interpolate and return the background for entire image.
Definition: Background.h:294
std::vector< int > _yorig
y origin ...
Definition: Background.h:370
void setNySample(int nySample)
Definition: Background.h:180
std::shared_ptr< lsst::afw::image::Image< PixelT > > getImage(lsst::afw::geom::Box2I const &bbox, Interpolate::Style const interpStyle, UndersampleStyle const undersampleStyle=THROW_EXCEPTION) const
Definition: Background.h:305
Property getStatisticsProperty() const
Definition: Background.h:213
Interpolate::Style getInterpStyle() const
Definition: Background.h:202
std::shared_ptr< math::Approximate< InternalPixelT > > getApproximate(ApproximateControl const &actrl, UndersampleStyle const undersampleStyle=THROW_EXCEPTION) const
Method to return an approximation to the background.
Definition: Background.h:347
geom::Box2I _imgBBox
size and origin of input image
Definition: Background.h:362
BackgroundControl(Interpolate::Style const style, int const nxSample=10, int const nySample=10, UndersampleStyle const undersampleStyle=THROW_EXCEPTION, StatisticsControl const sctrl=StatisticsControl(), Property const prop=MEANCLIP, ApproximateControl const actrl=ApproximateControl(ApproximateControl::UNKNOWN, 1))
Definition: Background.h:120
std::shared_ptr< Background > makeBackground(ImageT const &img, BackgroundControl const &bgCtrl)
A convenience function that uses function overloading to make the correct type of Background...
Definition: Background.h:551
std::shared_ptr< ApproximateControl > getApproximateControl()
Definition: Background.h:218
lsst::afw::image::MaskedImage< InternalPixelT > getStatsImage() const
Return the image of statistical quantities extracted from the image.
Definition: Background.h:512
void setInterpStyle(Interpolate::Style const style)
Definition: Background.h:188
#define LSST_makeBackground_getApproximate_types
Definition: Background.h:384
std::vector< double > _ycen
y center ...
Definition: Background.h:368
BackgroundControl(int const nxSample, int const nySample, StatisticsControl const &sctrl, std::string const &prop, ApproximateControl const actrl=ApproximateControl(ApproximateControl::UNKNOWN, 1))
Overload constructor to handle string for statistical operator.
Definition: Background.h:92
void setStatisticsProperty(std::string prop)
Definition: Background.h:215
Pass parameters to a Background object.
Definition: Background.h:57
UndersampleStyle getAsUsedUndersampleStyle() const
Return the UndersampleStyle that we actually used in the last call to getImage()
Definition: Background.h:340
A virtual base class to evaluate image background levels.
Definition: Background.h:234
An integer coordinate rectangle.
Definition: Box.h:55
Property stringToStatisticsProperty(std::string const property)
Conversion function to switch a string to a Property (see Statistics.h)
Definition: Statistics.cc:619
std::shared_ptr< lsst::afw::image::Image< PixelT > > getImage() const
Method to interpolate and return the background for entire image.
Definition: Background.h:328
Control how to make an approximation.
Definition: Approximate.h:50
void setUndersampleStyle(std::string const &undersampleStyle)
Definition: Background.h:196
Pass parameters to a Statistics object.
Definition: Statistics.h:91
estimate sample N-sigma clipped mean (N set in StatisticsControl, default=3)
Definition: Statistics.h:72
Interpolate::Style _asUsedInterpStyle
the style we actually used
Definition: Background.h:364
A base class for image defects.
Definition: cameraGeom.dox:3
std::shared_ptr< BackgroundControl > getBackgroundControl()
Definition: Background.h:358
virtual ~Background()
dtor
Definition: Background.h:264
std::shared_ptr< StatisticsControl > getStatisticsControl()
Definition: Background.h:210
std::shared_ptr< ApproximateControl const > getApproximateControl() const
Definition: Background.h:219
void setUndersampleStyle(UndersampleStyle const undersampleStyle)
Definition: Background.h:192
double x
BackgroundControl(std::string const &style, int const nxSample=10, int const nySample=10, std::string const &undersampleStyle="THROW_EXCEPTION", StatisticsControl const sctrl=StatisticsControl(), std::string const &prop="MEANCLIP", ApproximateControl const actrl=ApproximateControl(ApproximateControl::UNKNOWN, 1))
Overload constructor to handle strings for both interp and undersample styles.
Definition: Background.h:153
#define LSST_makeBackground_getImage_types
Definition: Background.h:383
#define LSST_makeBackground_getImage(m, v, T)
Definition: Background.h:385
UndersampleStyle getUndersampleStyle() const
Definition: Background.h:209
Extent< double, N > & operator+=(Extent< double, N > &lhs, Extent< int, N > const &rhs)
Definition: Extent.h:413
BackgroundControl(int const nxSample, int const nySample, StatisticsControl const sctrl=StatisticsControl(), Property const prop=MEANCLIP, ApproximateControl const actrl=ApproximateControl(ApproximateControl::UNKNOWN, 1))
Definition: Background.h:66
double getPixel(int const x, int const y) const
Return the background value at a point.
Definition: Background.h:508
std::vector< int > _xorig
x origin pix coords of sub images
Definition: Background.h:369
std::shared_ptr< BackgroundControl > _bctrl
control info set by user.
Definition: Background.h:363
std::vector< int > _ysize
y size ...
Definition: Background.h:372
UndersampleStyle _asUsedUndersampleStyle
the undersampleStyle we actually used
Definition: Background.h:365
void setInterpStyle(std::string const &style)
Definition: Background.h:190
std::shared_ptr< lsst::afw::image::Image< PixelT > > getImage(Interpolate::Style const interpStyle, UndersampleStyle const undersampleStyle=THROW_EXCEPTION) const
Method to interpolate and return the background for entire image.
Definition: Background.h:281
std::shared_ptr< StatisticsControl const > getStatisticsControl() const
Definition: Background.h:211
#define LSST_makeBackground_getApproximate(m, v, T)
Definition: Background.h:394
A class to evaluate image background levels.
Definition: Background.h:436
Extent< double, N > & operator-=(Extent< double, N > &lhs, Extent< int, N > const &rhs)
Definition: Extent.h:423
std::shared_ptr< lsst::afw::image::Image< PixelT > > getImage(lsst::afw::geom::Box2I const &bbox, std::string const &interpStyle, std::string const &undersampleStyle="THROW_EXCEPTION") const
Definition: Background.h:316
Property
control what is calculated
Definition: Statistics.h:63
void setStatisticsProperty(Property prop)
Definition: Background.h:214
std::shared_ptr< BackgroundControl const > getBackgroundControl() const
Definition: Background.h:359
void setNxSample(int nxSample)
Definition: Background.h:173
void setApproximateControl(std::shared_ptr< ApproximateControl > actrl)
Definition: Background.h:217
geom::Box2I getImageBBox() const
Return the input image&#39;s (PARENT) bounding box.
Definition: Background.h:356