LSSTApplications  1.1.2+25,10.0+13,10.0+132,10.0+133,10.0+224,10.0+41,10.0+8,10.0-1-g0f53050+14,10.0-1-g4b7b172+19,10.0-1-g61a5bae+98,10.0-1-g7408a83+3,10.0-1-gc1e0f5a+19,10.0-1-gdb4482e+14,10.0-11-g3947115+2,10.0-12-g8719d8b+2,10.0-15-ga3f480f+1,10.0-2-g4f67435,10.0-2-gcb4bc6c+26,10.0-28-gf7f57a9+1,10.0-3-g1bbe32c+14,10.0-3-g5b46d21,10.0-4-g027f45f+5,10.0-4-g86f66b5+2,10.0-4-gc4fccf3+24,10.0-40-g4349866+2,10.0-5-g766159b,10.0-5-gca2295e+25,10.0-6-g462a451+1
LSSTDataManagementBasePackage
Background.h
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
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 <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 #if !defined(LSST_AFW_MATH_BACKGROUND_H)
26 #define LSST_AFW_MATH_BACKGROUND_H
27 
32 #include <boost/preprocessor/seq.hpp>
33 #include "boost/shared_ptr.hpp"
34 #include "lsst/daf/base/Citizen.h"
35 #include "lsst/pex/exceptions.h"
36 #include "lsst/afw/geom/Box.h"
39 
40 namespace lsst {
41 namespace afw {
42 namespace math {
43 class ApproximateControl;
44 template<typename T> class Approximate;
45 
46 //
47 // Remember to update stringToUndersampleStyle if you change this.
48 // If this happens often, we can play CPP games to put the definition in exactly one place, although swig
49 // may not be happy (so we could think m4 thoughts instead)
50 //
55 };
56 UndersampleStyle stringToUndersampleStyle(std::string const &style);
57 
63 public:
65  int const nxSample,
66  int const nySample,
67  StatisticsControl const sctrl = StatisticsControl(),
68  Property const prop = MEANCLIP
69  )
70  : _style(Interpolate::AKIMA_SPLINE),
71  _nxSample(nxSample), _nySample(nySample),
73  _sctrl(new StatisticsControl(sctrl)),
74  _prop(prop) {
75  if (nxSample <= 0 || nySample <= 0) {
76  throw LSST_EXCEPT(lsst::pex::exceptions::LengthError,
77  str(boost::format("You must specify at least one point, not %dx%d")
78  % nxSample % nySample)
79  );
80  }
81  }
82 
87  int const nxSample,
88  int const nySample,
89  StatisticsControl const &sctrl,
90  std::string const &prop
91  )
92  : _style(Interpolate::AKIMA_SPLINE),
93  _nxSample(nxSample), _nySample(nySample),
95  _sctrl(new StatisticsControl(sctrl)),
97  if (nxSample <= 0 || nySample <= 0) {
98  throw LSST_EXCEPT(lsst::pex::exceptions::LengthError,
99  str(boost::format("You must specify at least one point, not %dx%d")
100  % nxSample % nySample)
101  );
102  }
103  }
104  // And now the two old APIs (preserved for backward compatibility)
109  Interpolate::Style const style,
110  int const nxSample = 10,
111  int const nySample = 10,
112  UndersampleStyle const undersampleStyle = THROW_EXCEPTION,
113  StatisticsControl const sctrl = StatisticsControl(),
114  Property const prop = MEANCLIP
115  )
116  : _style(style),
117  _nxSample(nxSample), _nySample(nySample),
118  _undersampleStyle(undersampleStyle),
119  _sctrl(new StatisticsControl(sctrl)),
120  _prop(prop) {
121  if (nxSample <= 0 || nySample <= 0) {
122  throw LSST_EXCEPT(lsst::pex::exceptions::LengthError,
123  str(boost::format("You must specify at least one point, not %dx%d")
124  % nxSample % nySample)
125  );
126  }
127  }
128 
135  std::string const &style,
136  int const nxSample = 10,
137  int const nySample = 10,
138  std::string const &undersampleStyle = "THROW_EXCEPTION",
139  StatisticsControl const sctrl = StatisticsControl(),
140  std::string const &prop = "MEANCLIP"
141  )
142  : _style(math::stringToInterpStyle(style)),
143  _nxSample(nxSample), _nySample(nySample),
144  _undersampleStyle(math::stringToUndersampleStyle(undersampleStyle)),
145  _sctrl(new StatisticsControl(sctrl)),
147  if (nxSample <= 0 || nySample <= 0) {
148  throw LSST_EXCEPT(lsst::pex::exceptions::LengthError,
149  str(boost::format("You must specify at least one point, not %dx%d")
150  % nxSample % nySample)
151  );
152  }
153  }
154 
155  virtual ~BackgroundControl() {}
156  void setNxSample (int nxSample) {
157  if (nxSample <= 0) {
158  throw LSST_EXCEPT(lsst::pex::exceptions::LengthError,
159  str(boost::format("nxSample must be position, not %d") % nxSample));
160  }
161  _nxSample = nxSample;
162  }
163  void setNySample (int nySample) {
164  if (nySample <= 0) {
165  throw LSST_EXCEPT(lsst::pex::exceptions::LengthError,
166  str(boost::format("nySample must be position, not %d") % nySample));
167  }
168  _nySample = nySample;
169  }
170 
171  void setInterpStyle (Interpolate::Style const style) { _style = style; }
172  // overload to take a string
173  void setInterpStyle (std::string const &style) { _style = math::stringToInterpStyle(style); }
174 
175  void setUndersampleStyle (UndersampleStyle const undersampleStyle) {
176  _undersampleStyle = undersampleStyle;
177  }
178  // overload to take a string
179  void setUndersampleStyle (std::string const &undersampleStyle) {
181  }
182 
183  int getNxSample() const { return _nxSample; }
184  int getNySample() const { return _nySample; }
186  if (_style < 0 || _style >= Interpolate::NUM_STYLES) {
187  throw LSST_EXCEPT(lsst::pex::exceptions::InvalidParameterError,
188  str(boost::format("Style %d is invalid") % _style));
189 
190  }
191  return _style;
192  }
196 
198  void setStatisticsProperty(Property prop) { _prop = prop; }
199  void setStatisticsProperty(std::string prop) { _prop = stringToStatisticsProperty(prop); }
200 
201 private:
202  Interpolate::Style _style; // style of interpolation to use
203  int _nxSample; // number of grid squares to divide image into to sample in x
204  int _nySample; // number of grid squares to divide image into to sample in y
205  UndersampleStyle _undersampleStyle; // what to do when nx,ny are too small for the requested interp style
206  PTR(StatisticsControl) _sctrl; // statistics control object
207  Property _prop; // statistics Property
208 };
209 
214 class Background : public daf::base::Citizen {
215 protected:
216  template<typename ImageT>
217  explicit Background(ImageT const& img, BackgroundControl const& bgCtrl);
218 
219  explicit Background(geom::Box2I const imageBBox, int const nx, int const ny);
221  virtual ~Background() { }
222 public:
223  typedef float InternalPixelT;
224 
226  virtual void operator+=(float const delta) = 0;
228  virtual void operator-=(float const delta) = 0;
234  template<typename PixelT>
236  Interpolate::Style const interpStyle,
237  UndersampleStyle const undersampleStyle=THROW_EXCEPTION
238  ) const {
239  return getImage<PixelT>(_imgBBox, interpStyle, undersampleStyle);
240  }
246  template<typename PixelT>
248  std::string const &interpStyle,
249  std::string const &undersampleStyle="THROW_EXCEPTION"
250  ) const {
251  return getImage<PixelT>(math::stringToInterpStyle(interpStyle),
252  stringToUndersampleStyle(undersampleStyle));
253  }
254  template<typename PixelT>
256  lsst::afw::geom::Box2I const& bbox,
257  Interpolate::Style const interpStyle,
258  UndersampleStyle const undersampleStyle=THROW_EXCEPTION
259  ) const {
260  return _getImage(bbox, interpStyle, undersampleStyle, static_cast<PixelT>(0));
261  }
262  template<typename PixelT>
264  lsst::afw::geom::Box2I const& bbox,
265  std::string const& interpStyle,
266  std::string const& undersampleStyle="THROW_EXCEPTION"
267  ) const {
268  return _getImage(bbox, math::stringToInterpStyle(interpStyle),
269  stringToUndersampleStyle(undersampleStyle), static_cast<PixelT>(0));
270  }
271 
276  template<typename PixelT>
278  return getImage<PixelT>(_bctrl.getInterpStyle(), _bctrl.getUndersampleStyle());
279  }
286  return _asUsedInterpStyle;
287  }
292  return _asUsedUndersampleStyle;
293  }
298  ApproximateControl const& actrl,
299  UndersampleStyle const undersampleStyle=THROW_EXCEPTION
300  ) const {
301  InternalPixelT disambiguate = 0;
302  return _getApproximate(actrl, undersampleStyle, disambiguate);
303  }
307  geom::Box2I getImageBBox() const { return _imgBBox; }
308 
309 protected:
314 
315  std::vector<double> _xcen;
316  std::vector<double> _ycen;
317  std::vector<int> _xorig;
318  std::vector<int> _yorig;
319  std::vector<int> _xsize;
320  std::vector<int> _ysize;
321  /*
322  * We want getImage to be present in the base class, but a templated virtual function
323  * is impossible. So we'll solve the dilemma with a hack: explicitly defined
324  * virtual functions for the image types we need
325  */
326 #if !defined(SWIG)
327 // We'll evaluate LSST_makeBackground_get{Approximation,Image} for each type in
328 // LSST_makeBackground_get{Approximation,Image}_types,
329 // setting v to the second arg (i.e. "= 0" for the first invocation). The first agument, m, is ignores
330 
331 // Desired types
332 #define LSST_makeBackground_getImage_types (double)(float)(int)
333 #define LSST_makeBackground_getApproximate_types (Background::InternalPixelT)
334 #define LSST_makeBackground_getImage(m, v, T) \
335  virtual PTR(lsst::afw::image::Image<T>) _getImage( \
336  lsst::afw::geom::Box2I const& bbox, \
337  Interpolate::Style const interpStyle, /* Style of the interpolation */ \
338  UndersampleStyle const undersampleStyle=THROW_EXCEPTION, /* Behaviour if there are too few points */\
339  T = 0 /* disambiguate */ \
340  ) const v;
341 
342 #define LSST_makeBackground_getApproximate(m, v, T) \
343  virtual PTR(Approximate<T>) _getApproximate( \
344  ApproximateControl const& actrl, /* Approximation style */ \
345  UndersampleStyle const undersampleStyle=THROW_EXCEPTION, /* Behaviour if there are too few points */\
346  T = 0 /* disambiguate */ \
347  ) const v;
348 
351 #endif
352 private:
353  Background(Background const&);
354  Background& operator=(Background const&);
355  void _setCenOrigSize(int const width, int const height, int const nxSample, int const nySample);
356 };
357 
382 class BackgroundMI : public Background {
383 public:
384  template<typename ImageT>
385  explicit BackgroundMI(ImageT const& img,
386  BackgroundControl const& bgCtrl);
387  explicit BackgroundMI(geom::Box2I const imageDimensions,
388  image::MaskedImage<InternalPixelT> const& statsImage);
389 
390  virtual void operator+=(float const delta);
391  virtual void operator-=(float const delta);
392 
393 
394  double getPixel(Interpolate::Style const style, int const x, int const y) const;
402  double getPixel(int const x, int const y) const {
403  return getPixel(_bctrl.getInterpStyle(), x, y);
404  }
409  return _statsImage;
410  }
411 
412 private:
413  lsst::afw::image::MaskedImage<InternalPixelT> _statsImage; // statistical properties for the grid of subimages
414  mutable std::vector<std::vector<double> > _gridColumns; // interpolated columns for the bicubic spline
415 
416  void _setGridColumns(Interpolate::Style const interpStyle,
417  UndersampleStyle const undersampleStyle,
418  int const iX, std::vector<int> const& ypix) const;
419 
420 #if !defined(SWIG) && defined(LSST_makeBackground_getImage)
423 #if 0 // keep for use in Background instantiations
424 #undef LSST_makeBackground_getImage_types
425 #undef LSST_makeBackground_getApproximate_types
426 #endif
427 #undef LSST_makeBackground_getImage
428 #undef LSST_makeBackground_getApproximate
429 #endif
430  // Here's the worker function for _getImage (non-virtual; it's templated in BackgroundMI, not Background)
431  template<typename PixelT>
432  PTR(image::Image<PixelT>) doGetImage(geom::Box2I const& bbox,
433  Interpolate::Style const interpStyle_,
434  UndersampleStyle const undersampleStyle) const;
435  // and for _getApproximate
436  template<typename PixelT>
437  PTR(Approximate<PixelT>) doGetApproximate(ApproximateControl const& actrl,
438  UndersampleStyle const undersampleStyle) const;
439 };
445 template<typename ImageT>
446 PTR(Background) makeBackground(ImageT const& img, BackgroundControl const& bgCtrl) {
447  return PTR(Background)(new BackgroundMI(img, bgCtrl));
448 }
449 
450 }}}
451 
452 #endif // LSST_AFW_MATH_BACKGROUND_H
int y
Interpolate::Style getInterpStyle() const
Definition: Background.h:185
UndersampleStyle stringToUndersampleStyle(std::string const &style)
Conversion function to switch a string to an UndersampleStyle.
Definition: Background.cc:148
boost::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:446
std::vector< int > _ysize
y size ...
Definition: Background.h:320
Interpolate::Style stringToInterpStyle(std::string const &style)
Conversion function to switch a string to an Interpolate::Style.
Definition: Interpolate.cc:264
#define CONST_PTR(...)
Definition: base.h:47
Interpolate values for a set of x,y vector&lt;&gt;s.
Definition: Interpolate.h:37
void setInterpStyle(Interpolate::Style const style)
Definition: Background.h:171
std::vector< int > _xorig
x origin pix coords of sub images
Definition: Background.h:317
estimate sample N-sigma clipped mean (N set in StatisticsControl, default=3)
Definition: Statistics.h:72
void setUndersampleStyle(std::string const &undersampleStyle)
Definition: Background.h:179
void setNxSample(int nxSample)
Definition: Background.h:156
double getPixel(int const x, int const y) const
Return the background value at a point.
Definition: Background.h:402
UndersampleStyle getUndersampleStyle() const
Definition: Background.h:193
#define LSST_makeBackground_getApproximate_types
Definition: Background.h:333
boost::shared_ptr< StatisticsControl > getStatisticsControl()
Definition: Background.h:194
Extent< double, N > & operator+=(Extent< double, N > &lhs, Extent< int, N > const &rhs)
Definition: Extent.h:429
void setStatisticsProperty(Property prop)
Definition: Background.h:198
std::vector< double > _ycen
y center ...
Definition: Background.h:316
Extent< double, N > & operator-=(Extent< double, N > &lhs, Extent< int, N > const &rhs)
Definition: Extent.h:439
#define PTR(...)
Definition: base.h:41
UndersampleStyle _undersampleStyle
Definition: Background.h:205
Pass parameters to a Background object.
Definition: Background.h:62
Approximate values for a MaskedImage.
Definition: Approximate.h:38
A virtual base class to evaluate image background levels.
Definition: Background.h:214
An integer coordinate rectangle.
Definition: Box.h:53
float InternalPixelT
type used for any internal images, and returned by getApproximate
Definition: Background.h:223
Control how to make an approximation.
Definition: Approximate.h:47
Pass parameters to a Statistics objectA class to pass parameters which control how the stats are calc...
Definition: Statistics.h:92
BackgroundControl(int const nxSample, int const nySample, StatisticsControl const &sctrl, std::string const &prop)
Definition: Background.h:86
BackgroundControl _bctrl
control info set by user.
Definition: Background.h:311
void setUndersampleStyle(UndersampleStyle const undersampleStyle)
Definition: Background.h:175
std::vector< int > _xsize
x size of sub images
Definition: Background.h:319
void setStatisticsProperty(std::string prop)
Definition: Background.h:199
Property getStatisticsProperty() const
Definition: Background.h:197
UndersampleStyle _asUsedUndersampleStyle
the undersampleStyle we actually used
Definition: Background.h:313
geom::Box2I _imgBBox
size and origin of input image
Definition: Background.h:310
lsst::afw::image::MaskedImage< InternalPixelT > _statsImage
Definition: Background.h:413
void setNySample(int nySample)
Definition: Background.h:163
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")
Definition: Background.h:134
#define LSST_makeBackground_getImage_types
Definition: Background.h:332
UndersampleStyle getAsUsedUndersampleStyle() const
Definition: Background.h:291
#define LSST_makeBackground_getImage(m, v, T)
Definition: Background.h:334
int x
lsst::afw::image::MaskedImage< InternalPixelT > getStatsImage() const
Return the image of statistical quantities extracted from the image.
Definition: Background.h:408
virtual ~Background()
dtor
Definition: Background.h:221
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
std::vector< double > _xcen
x center pix coords of sub images
Definition: Background.h:315
Compute Image Statistics.
Property stringToStatisticsProperty(std::string const property)
Conversion function to switch a string to a Property (see Statistics.h)
Definition: Statistics.cc:658
#define LSST_makeBackground_getApproximate(m, v, T)
Definition: Background.h:342
A class to evaluate image background levels.
Definition: Background.h:382
void setInterpStyle(std::string const &style)
Definition: Background.h:173
std::vector< std::vector< double > > _gridColumns
Definition: Background.h:414
Property
control what is calculated
Definition: Statistics.h:63
std::vector< int > _yorig
y origin ...
Definition: Background.h:318
geom::Box2I getImageBBox() const
Definition: Background.h:307
boost::shared_ptr< StatisticsControl > _sctrl
Definition: Background.h:206
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)
Definition: Background.h:108
Interpolate::Style _asUsedInterpStyle
the style we actually used
Definition: Background.h:312
Include files required for standard LSST Exception handling.
BackgroundControl(int const nxSample, int const nySample, StatisticsControl const sctrl=StatisticsControl(), Property const prop=MEANCLIP)
Definition: Background.h:64
Interpolate::Style getAsUsedInterpStyle() const
Definition: Background.h:285