LSST Applications g04e9c324dd+8c5ae1fdc5,g134cb467dc+b203dec576,g18429d2f64+358861cd2c,g199a45376c+0ba108daf9,g1fd858c14a+dd066899e3,g262e1987ae+ebfced1d55,g29ae962dfc+72fd90588e,g2cef7863aa+aef1011c0b,g35bb328faa+8c5ae1fdc5,g3fd5ace14f+b668f15bc5,g4595892280+3897dae354,g47891489e3+abcf9c3559,g4d44eb3520+fb4ddce128,g53246c7159+8c5ae1fdc5,g67b6fd64d1+abcf9c3559,g67fd3c3899+1f72b5a9f7,g74acd417e5+cb6b47f07b,g786e29fd12+668abc6043,g87389fa792+8856018cbb,g89139ef638+abcf9c3559,g8d7436a09f+bcf525d20c,g8ea07a8fe4+9f5ccc88ac,g90f42f885a+6054cc57f1,g97be763408+06f794da49,g9dd6db0277+1f72b5a9f7,ga681d05dcb+7e36ad54cd,gabf8522325+735880ea63,gac2eed3f23+abcf9c3559,gb89ab40317+abcf9c3559,gbf99507273+8c5ae1fdc5,gd8ff7fe66e+1f72b5a9f7,gdab6d2f7ff+cb6b47f07b,gdc713202bf+1f72b5a9f7,gdfd2d52018+8225f2b331,ge365c994fd+375fc21c71,ge410e46f29+abcf9c3559,geaed405ab2+562b3308c0,gf9a733ac38+8c5ae1fdc5,w.2025.35
LSST Data Management Base Package
Loading...
Searching...
No Matches
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 "ndarray.h"
33#include "lsst/pex/exceptions.h"
34#include "lsst/geom/Box.h"
38
39namespace lsst {
40namespace afw {
41namespace 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//
53
58public:
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) {
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) {
104 str(boost::format("You must specify at least one point, not %dx%d") % nxSample %
105 nySample));
106 }
107 }
108
119 BackgroundControl(Interpolate::Style const style, int const nxSample = 10, int const nySample = 10,
120 UndersampleStyle const undersampleStyle = THROW_EXCEPTION,
121 StatisticsControl const sctrl = StatisticsControl(), Property const prop = MEANCLIP,
123
124 )
125 : _style(style),
126 _nxSample(nxSample),
127 _nySample(nySample),
128 _undersampleStyle(undersampleStyle),
129 _sctrl(new StatisticsControl(sctrl)),
130 _prop(prop),
131 _actrl(new ApproximateControl(actrl)) {
132 if (nxSample <= 0 || nySample <= 0) {
134 str(boost::format("You must specify at least one point, not %dx%d") % nxSample %
135 nySample));
136 }
137 }
138
150 BackgroundControl(std::string const& style, int const nxSample = 10, int const nySample = 10,
151 std::string const& undersampleStyle = "THROW_EXCEPTION",
152 StatisticsControl const sctrl = StatisticsControl(),
153 std::string const& prop = "MEANCLIP",
155 : _style(math::stringToInterpStyle(style)),
156 _nxSample(nxSample),
157 _nySample(nySample),
158 _undersampleStyle(math::stringToUndersampleStyle(undersampleStyle)),
159 _sctrl(new StatisticsControl(sctrl)),
160 _prop(stringToStatisticsProperty(prop)),
161 _actrl(new ApproximateControl(actrl)) {
162 if (nxSample <= 0 || nySample <= 0) {
164 str(boost::format("You must specify at least one point, not %dx%d") % nxSample %
165 nySample));
166 }
167 }
168
173
175 void setNxSample(int nxSample) {
176 if (nxSample <= 0) {
178 str(boost::format("nxSample must be position, not %d") % nxSample));
179 }
180 _nxSample = nxSample;
181 }
182 void setNySample(int nySample) {
183 if (nySample <= 0) {
185 str(boost::format("nySample must be position, not %d") % nySample));
186 }
187 _nySample = nySample;
188 }
189
190 void setInterpStyle(Interpolate::Style const style) { _style = style; }
191 // overload to take a string
192 void setInterpStyle(std::string const& style) { _style = math::stringToInterpStyle(style); }
193
194 void setUndersampleStyle(UndersampleStyle const undersampleStyle) {
195 _undersampleStyle = undersampleStyle;
196 }
197 // overload to take a string
198 void setUndersampleStyle(std::string const& undersampleStyle) {
199 _undersampleStyle = math::stringToUndersampleStyle(undersampleStyle);
200 }
201
202 int getNxSample() const { return _nxSample; }
203 int getNySample() const { return _nySample; }
205 if (_style < 0 || _style >= Interpolate::NUM_STYLES) {
207 str(boost::format("Style %d is invalid") % _style));
208 }
209 return _style;
210 }
211 UndersampleStyle getUndersampleStyle() const { return _undersampleStyle; }
214
215 Property getStatisticsProperty() const { return _prop; }
216 void setStatisticsProperty(Property prop) { _prop = prop; }
218
222
223private:
224 Interpolate::Style _style; // style of interpolation to use
225 int _nxSample; // number of grid squares to divide image into to sample in x
226 int _nySample; // number of grid squares to divide image into to sample in y
227 UndersampleStyle _undersampleStyle; // what to do when nx,ny are too small for the requested interp style
228 std::shared_ptr<StatisticsControl> _sctrl; // statistics control object
229 Property _prop; // statistics Property
230 std::shared_ptr<ApproximateControl> _actrl; // approximate control object
231};
232
237protected:
247 template <typename ImageT>
248 explicit Background(ImageT const& img, BackgroundControl const& bgCtrl);
249
260 explicit Background(lsst::geom::Box2I const imageBBox, int const nx, int const ny);
261
262public:
263 using InternalPixelT = float;
264
265 Background(Background const&) = delete;
268 virtual ~Background() = default;
269 Background& operator=(Background const&) = delete;
271
273 virtual Background& operator+=(float const delta) = 0;
275 virtual Background& operator-=(float const delta) = 0;
283 template <typename PixelT>
285 Interpolate::Style const interpStyle,
286 UndersampleStyle const undersampleStyle = THROW_EXCEPTION) const {
287 return getImage<PixelT>(_imgBBox, interpStyle, undersampleStyle);
288 }
289
296 template <typename PixelT>
298 std::string const& interpStyle, std::string const& undersampleStyle = "THROW_EXCEPTION") const {
300 stringToUndersampleStyle(undersampleStyle));
301 }
302
307 template <typename PixelT>
309 lsst::geom::Box2I const& bbox, Interpolate::Style const interpStyle,
310 UndersampleStyle const undersampleStyle = THROW_EXCEPTION) const {
311 return _getImage(bbox, interpStyle, undersampleStyle, static_cast<PixelT>(0));
312 }
313
318 template <typename PixelT>
320 lsst::geom::Box2I const& bbox, std::string const& interpStyle,
321 std::string const& undersampleStyle = "THROW_EXCEPTION") const {
322 return _getImage(bbox, math::stringToInterpStyle(interpStyle),
323 stringToUndersampleStyle(undersampleStyle), static_cast<PixelT>(0));
324 }
325
329 template <typename PixelT>
331 return getImage<PixelT>(_bctrl->getInterpStyle(), _bctrl->getUndersampleStyle());
332 }
333
350 ApproximateControl const& actrl,
351 UndersampleStyle const undersampleStyle = THROW_EXCEPTION) const {
352 InternalPixelT disambiguate = 0;
353 return _getApproximate(actrl, undersampleStyle, disambiguate);
354 }
355
359
362
363protected:
368
375 /*
376 * We want getImage to be present in the base class, but a templated virtual function
377 * is impossible. So we'll solve the dilemma with a hack: explicitly defined
378 * virtual functions for the image types we need
379 */
380// We'll evaluate LSST_makeBackground_get{Approximation,Image} for each type in
381// LSST_makeBackground_get{Approximation,Image}_types,
382// setting v to the second arg (i.e. "= 0" for the first invocation). The first agument, m, is ignores
383
384// Desired types
385#define LSST_makeBackground_getImage_types (Background::InternalPixelT)
386#define LSST_makeBackground_getApproximate_types (Background::InternalPixelT)
387#define LSST_makeBackground_getImage(m, v, T) \
388 virtual std::shared_ptr<lsst::afw::image::Image<T>> _getImage( \
389 lsst::geom::Box2I const& bbox, \
390 Interpolate::Style const interpStyle, /* Style of the interpolation */ \
391 UndersampleStyle const undersampleStyle = \
392 THROW_EXCEPTION, /* Behaviour if there are too few points */ \
393 T = 0 /* disambiguate */ \
394 ) const v;
395
396#define LSST_makeBackground_getApproximate(m, v, T) \
397 virtual std::shared_ptr<Approximate<T>> _getApproximate( \
398 ApproximateControl const& actrl, /* Approximation style */ \
399 UndersampleStyle const undersampleStyle = \
400 THROW_EXCEPTION, /* Behaviour if there are too few points */ \
401 T = 0 /* disambiguate */ \
402 ) const v;
403
406private:
411 void _setCenOrigSize(int const width, int const height, int const nxSample, int const nySample);
412};
413
435class BackgroundMI : public Background {
436public:
437 template <typename ImageT>
459 explicit BackgroundMI(ImageT const& img, BackgroundControl const& bgCtrl);
466 explicit BackgroundMI(lsst::geom::Box2I const imageDimensions,
467 image::MaskedImage<InternalPixelT> const& statsImage);
468
469 BackgroundMI(BackgroundMI const&) = delete;
473 ~BackgroundMI() override = default;
474
480 BackgroundMI& operator+=(float const delta) override;
486 BackgroundMI& operator-=(float const delta) override;
487
492
494 ndarray::Array<double, 1, 1> getBinCentersX() const;
495
497 ndarray::Array<double, 1, 1> getBinCentersY() const;
498
499private:
501 _statsImage; // statistical properties for the grid of subimages
502 mutable std::vector<std::vector<double>> _gridColumns; // interpolated columns for the bicubic spline
503
504 void _setGridColumns(Interpolate::Style const interpStyle, UndersampleStyle const undersampleStyle,
505 int const iX, std::vector<int> const& ypix) const;
506
507#if defined(LSST_makeBackground_getImage)
509 BOOST_PP_SEQ_FOR_EACH(LSST_makeBackground_getApproximate, override,
511#undef LSST_makeBackground_getImage
512#undef LSST_makeBackground_getApproximate
513#endif
514 // Here's the worker function for _getImage (non-virtual; it's templated in BackgroundMI, not Background)
518 template <typename PixelT>
520 Interpolate::Style const interpStyle_,
521 UndersampleStyle const undersampleStyle) const;
522 // and for _getApproximate
523 template <typename PixelT>
524 std::shared_ptr<Approximate<PixelT>> doGetApproximate(ApproximateControl const& actrl,
525 UndersampleStyle const undersampleStyle) const;
526};
527
532template <typename ImageT>
534 return std::shared_ptr<Background>(new BackgroundMI(img, bgCtrl));
535}
536} // namespace math
537} // namespace afw
538} // namespace lsst
539
540#endif // LSST_AFW_MATH_BACKGROUND_H
#define LSST_makeBackground_getImage(m, v, T)
Definition Background.h:387
#define LSST_makeBackground_getApproximate(m, v, T)
Definition Background.h:396
#define LSST_makeBackground_getApproximate_types
Definition Background.h:386
#define LSST_makeBackground_getImage_types
Definition Background.h:385
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition Exception.h:48
A class to manipulate images, masks, and variance as a single object.
Definition MaskedImage.h:74
Control how to make an approximation.
Definition Approximate.h:48
Pass parameters to a Background object.
Definition Background.h:57
void setApproximateControl(std::shared_ptr< ApproximateControl > actrl)
Definition Background.h:219
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:150
std::shared_ptr< ApproximateControl const > getApproximateControl() const
Definition Background.h:221
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))
Overload constructor to provide interp style.
Definition Background.h:119
BackgroundControl(BackgroundControl const &)=default
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
std::shared_ptr< StatisticsControl > getStatisticsControl()
Definition Background.h:212
BackgroundControl & operator=(BackgroundControl const &)=default
BackgroundControl(BackgroundControl &&)=default
std::shared_ptr< ApproximateControl > getApproximateControl()
Definition Background.h:220
Interpolate::Style getInterpStyle() const
Definition Background.h:204
BackgroundControl & operator=(BackgroundControl &&)=default
void setInterpStyle(Interpolate::Style const style)
Definition Background.h:190
std::shared_ptr< StatisticsControl const > getStatisticsControl() const
Definition Background.h:213
void setUndersampleStyle(UndersampleStyle const undersampleStyle)
Definition Background.h:194
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(Property prop)
Definition Background.h:216
void setStatisticsProperty(std::string prop)
Definition Background.h:217
UndersampleStyle getUndersampleStyle() const
Definition Background.h:211
void setUndersampleStyle(std::string const &undersampleStyle)
Definition Background.h:198
Property getStatisticsProperty() const
Definition Background.h:215
void setInterpStyle(std::string const &style)
Definition Background.h:192
std::vector< double > _ycen
y center ...
Definition Background.h:370
std::shared_ptr< lsst::afw::image::Image< PixelT > > getImage(lsst::geom::Box2I const &bbox, std::string const &interpStyle, std::string const &undersampleStyle="THROW_EXCEPTION") const
Definition Background.h:319
std::vector< double > _xcen
x center pix coords of sub images
Definition Background.h:369
UndersampleStyle getAsUsedUndersampleStyle() const
Return the UndersampleStyle that we actually used in the last call to getImage()
Definition Background.h:342
Background & operator=(Background &&)=delete
virtual Background & operator-=(float const delta)=0
Subtract a constant level from a background.
std::shared_ptr< lsst::afw::image::Image< PixelT > > getImage(lsst::geom::Box2I const &bbox, Interpolate::Style const interpStyle, UndersampleStyle const undersampleStyle=THROW_EXCEPTION) const
Definition Background.h:308
Interpolate::Style getAsUsedInterpStyle() const
Return the Interpolate::Style that we actually used in the last call to getImage()
Definition Background.h:338
Background(Background &&)=delete
virtual Background & operator+=(float const delta)=0
Add a constant level to a background.
Background(Background const &)=delete
lsst::geom::Box2I getImageBBox() const
Return the input image's (PARENT) bounding box.
Definition Background.h:358
Background(ImageT const &img, BackgroundControl const &bgCtrl)
Constructor for Background.
Definition Background.cc:44
std::vector< int > _xsize
x size of sub images
Definition Background.h:373
lsst::geom::Box2I _imgBBox
size and origin of input image
Definition Background.h:364
std::vector< int > _xorig
x origin pix coords of sub images
Definition Background.h:371
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:297
std::shared_ptr< BackgroundControl > getBackgroundControl()
Definition Background.h:360
UndersampleStyle _asUsedUndersampleStyle
the undersampleStyle we actually used
Definition Background.h:367
std::shared_ptr< lsst::afw::image::Image< PixelT > > getImage() const
Method to interpolate and return the background for entire image.
Definition Background.h:330
std::vector< int > _ysize
y size ...
Definition Background.h:374
Background & operator=(Background const &)=delete
virtual ~Background()=default
dtor
float InternalPixelT
type used for any internal images, and returned by getApproximate
Definition Background.h:263
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:349
std::shared_ptr< BackgroundControl const > getBackgroundControl() const
Definition Background.h:361
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:284
std::vector< int > _yorig
y origin ...
Definition Background.h:372
Interpolate::Style _asUsedInterpStyle
the style we actually used
Definition Background.h:366
std::shared_ptr< BackgroundControl > _bctrl
control info set by user.
Definition Background.h:365
A class to evaluate image background levels.
Definition Background.h:435
lsst::afw::image::MaskedImage< InternalPixelT > getStatsImage() const
Return the image of statistical quantities extracted from the image.
Definition Background.h:491
BackgroundMI(BackgroundMI &&)=delete
ndarray::Array< double, 1, 1 > getBinCentersY() const
Return the y-coordinate centers of the bins.
BackgroundMI & operator=(BackgroundMI const &)=delete
~BackgroundMI() override=default
BackgroundMI & operator-=(float const delta) override
Subtract a scalar from the Background (equivalent to subtracting a constant from the original image)
ndarray::Array< double, 1, 1 > getBinCentersX() const
Return the x-coordinate centers of the bins.
BackgroundMI(BackgroundMI const &)=delete
BackgroundMI & operator+=(float const delta) override
Add a scalar to the Background (equivalent to adding a constant to the original image)
BackgroundMI & operator=(BackgroundMI &&)=delete
BackgroundMI(ImageT const &img, BackgroundControl const &bgCtrl)
Constructor for BackgroundMI.
Pass parameters to a Statistics object.
Definition Statistics.h:83
An integer coordinate rectangle.
Definition Box.h:55
Reports invalid arguments.
Definition Runtime.h:66
Reports attempts to exceed implementation-defined length limits for some classes.
Definition Runtime.h:76
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:533
Property
control what is calculated
Definition Statistics.h:53
@ MEANCLIP
estimate sample N-sigma clipped mean (N set in StatisticsControl, default=3)
Definition Statistics.h:62
Property stringToStatisticsProperty(std::string const property)
Conversion function to switch a string to a Property (see Statistics.h)
Interpolate::Style stringToInterpStyle(std::string const &style)
Conversion function to switch a string to an Interpolate::Style.
UndersampleStyle stringToUndersampleStyle(std::string const &style)
Conversion function to switch a string to an UndersampleStyle.