39 namespace ex = pex::exceptions;
49 void cullNan(std::vector<double>
const& values, std::vector<double>
const& refs,
50 std::vector<double>& culledValues, std::vector<double>& culledRefs,
51 double const defaultValue = std::numeric_limits<double>::quiet_NaN()) {
52 if (culledValues.capacity() == 0) {
53 culledValues.reserve(refs.size());
57 if (culledRefs.capacity() == 0) {
58 culledRefs.reserve(refs.size());
63 bool const haveDefault = !std::isnan(defaultValue);
65 for (std::vector<double>::const_iterator pVal = values.begin(), pRef = refs.begin(); pRef != refs.end();
67 if (!std::isnan(*pRef)) {
68 culledValues.push_back(*pVal);
69 culledRefs.push_back(*pRef);
70 }
else if (haveDefault) {
71 culledValues.push_back(*pVal);
72 culledRefs.push_back(defaultValue);
80 template <
typename ImageT>
82 :
Background(img, bgCtrl), _statsImage(
image::MaskedImage<InternalPixelT>()) {
93 for (
int iX = 0; iX < nxSample; ++iX) {
94 for (
int iY = 0; iY < nySample; ++iY) {
102 im(iX, iY) = res.first;
103 var(iX, iY) = res.second;
108 :
Background(imageBBox, statsImage.getWidth(), statsImage.getHeight()), _statsImage(statsImage) {}
112 std::vector<int>
const& ypix)
const {
116 _gridColumns[iX].resize(height);
119 std::vector<double> _grid(_statsImage.
getHeight());
125 std::vector<double> ycenTmp, gridTmp;
126 cullNan(
_ycen, _grid, ycenTmp, gridTmp);
128 std::shared_ptr<Interpolate> intobj;
131 }
catch (pex::exceptions::OutOfRangeError& e) {
132 switch (undersampleStyle) {
134 LSST_EXCEPT_ADD(e,
"setting _gridcolumns");
137 if (gridTmp.empty()) {
139 ycenTmp.push_back(0);
140 gridTmp.push_back(std::numeric_limits<double>::quiet_NaN());
150 e,
"The BackgroundControl UndersampleStyle INCREASE_NXNYSAMPLE is not supported.");
153 LSST_EXCEPT_ADD(e, str(boost::format(
"The selected BackgroundControl "
154 "UndersampleStyle %d is not defined.") %
158 }
catch (ex::Exception& e) {
159 LSST_EXCEPT_ADD(e,
"setting _gridcolumns");
163 for (
int iY = 0; iY < height; ++iY) {
164 _gridColumns[iX][iY] = intobj->interpolate(ypix[iY]);
169 _statsImage += delta;
174 _statsImage -= delta;
179 (void)getImage<InternalPixelT>(interpStyle);
182 int const nxSample = _statsImage.
getWidth();
183 std::vector<double> bg_x(nxSample);
184 for (
int iX = 0; iX < nxSample; iX++) {
185 bg_x[iX] = _gridColumns[iX][y];
187 std::vector<double> xcenTmp, bgTmp;
188 cullNan(
_xcen, bg_x, xcenTmp, bgTmp);
191 std::shared_ptr<Interpolate> intobj =
makeInterpolate(xcenTmp, bgTmp, interpStyle);
192 return static_cast<double>(intobj->interpolate(x));
193 }
catch (ex::Exception& e) {
194 LSST_EXCEPT_ADD(e,
"in getPixel()");
198 template <
typename PixelT>
199 std::shared_ptr<image::Image<PixelT>> BackgroundMI::doGetImage(
207 str(boost::format(
"BBox (%d:%d,%d:%d) out of range (%d:%d,%d:%d)") % bbox.
getMinX() %
211 int const nxSample = _statsImage.
getWidth();
212 int const nySample = _statsImage.
getHeight();
231 switch (undersampleStyle) {
233 if (isXundersampled && isYundersampled) {
235 ex::InvalidParameterError,
236 "nxSample and nySample have too few points for requested interpolation style.");
237 }
else if (isXundersampled) {
238 throw LSST_EXCEPT(ex::InvalidParameterError,
239 "nxSample has too few points for requested interpolation style.");
240 }
else if (isYundersampled) {
241 throw LSST_EXCEPT(ex::InvalidParameterError,
242 "nySample has too few points for requested interpolation style.");
246 if (isXundersampled || isYundersampled) {
249 interpStyle = (nxSample < nySample) ? xStyle : yStyle;
254 if (isXundersampled || isYundersampled) {
256 ex::InvalidParameterError,
257 "The BackgroundControl UndersampleStyle INCREASE_NXNYSAMPLE is not supported.");
261 throw LSST_EXCEPT(ex::InvalidParameterError,
262 str(boost::format(
"The selected BackgroundControl "
263 "UndersampleStyle %d is not defined.") %
280 std::vector<int> ypix(height);
281 for (
int iY = 0; iY < height; ++iY) {
285 _gridColumns.resize(width);
286 for (
int iX = 0; iX < nxSample; ++iX) {
287 _setGridColumns(interpStyle, undersampleStyle, iX, ypix);
292 std::shared_ptr<image::Image<PixelT>> bg =
298 std::vector<double> xcenTmp, bgTmp;
304 double defaultValue = std::numeric_limits<double>::quiet_NaN();
306 for (
int y = 0, iY = bboxOff.getY(); y < bbox.
getHeight(); ++y, ++iY) {
308 std::vector<double> bg_x(nxSample);
309 for (
int iX = 0; iX < nxSample; iX++) {
310 bg_x[iX] =
static_cast<double>(_gridColumns[iX][iY]);
312 cullNan(
_xcen, bg_x, xcenTmp, bgTmp, defaultValue);
314 std::shared_ptr<Interpolate> intobj;
317 }
catch (pex::exceptions::OutOfRangeError& e) {
318 switch (undersampleStyle) {
320 LSST_EXCEPT_ADD(e, str(boost::format(
"Interpolating in y (iY = %d)") % iY));
324 xcenTmp.push_back(0);
325 bgTmp.push_back(defaultValue);
336 "The BackgroundControl UndersampleStyle INCREASE_NXNYSAMPLE is not supported.");
339 LSST_EXCEPT_ADD(e, str(boost::format(
"The selected BackgroundControl "
340 "UndersampleStyle %d is not defined.") %
344 }
catch (ex::Exception& e) {
345 LSST_EXCEPT_ADD(e, str(boost::format(
"Interpolating in y (iY = %d)") % iY));
350 for (
int iX = bboxOff.getX(),
x = 0;
x < bbox.
getWidth(); ++iX, ++
x) {
351 (*bg)(
x, y) = static_cast<PixelT>(intobj->interpolate(iX));
354 bg->setXY0(bbox.
getMin());
359 template <
typename PixelT>
360 std::shared_ptr<Approximate<PixelT>> BackgroundMI::doGetApproximate(
361 ApproximateControl
const& actrl,
373 #define CREATE_BACKGROUND(m, v, TYPE) \
374 template BackgroundMI::BackgroundMI(image::Image<TYPE> const& img, BackgroundControl const& bgCtrl); \
375 template BackgroundMI::BackgroundMI(image::MaskedImage<TYPE> const& img, \
376 BackgroundControl const& bgCtrl); \
377 std::shared_ptr<image::Image<TYPE>> BackgroundMI::_getImage( \
378 geom::Box2I const& bbox, Interpolate::Style const interpStyle, \
379 UndersampleStyle const undersampleStyle, \
382 return BackgroundMI::doGetImage<TYPE>(bbox, interpStyle, undersampleStyle); \
385 #define CREATE_getApproximate(m, v, TYPE) \
386 std::shared_ptr<Approximate<TYPE>> BackgroundMI::_getApproximate( \
387 ApproximateControl const& actrl, \
388 UndersampleStyle const undersampleStyle, \
391 return BackgroundMI::doGetApproximate<TYPE>(actrl, undersampleStyle); \
std::vector< int > _xsize
x size of sub images
std::vector< double > _xcen
x center pix coords of sub images
Extent2I const getDimensions() const
std::vector< int > _yorig
y origin ...
bool contains(Point2I const &point) const
Return true if the box contains the point.
VariancePtr getVariance() const
Return a (shared_ptr to) the MaskedImage's variance.
int getHeight() const
Return the number of rows in the image.
BackgroundMI(ImageT const &img, BackgroundControl const &bgCtrl)
Constructor for BackgroundMI.
std::shared_ptr< Interpolate > makeInterpolate(std::vector< double > const &x, std::vector< double > const &y, Interpolate::Style const style=Interpolate::AKIMA_SPLINE)
A factory function to make Interpolate objects.
geom::Box2I _imgBBox
size and origin of input image
double getPixel(Interpolate::Style const style, int const x, int const y) const
Method to retrieve the background level at a pixel coord.
#define LSST_makeBackground_getApproximate_types
std::vector< double > _ycen
y center ...
ImagePtr getImage() const
Return a (shared_ptr to) the MaskedImage's image.
std::shared_ptr< Approximate< PixelT > > makeApproximate(std::vector< double > const &x, std::vector< double > const &y, image::MaskedImage< PixelT > const &im, geom::Box2I const &bbox, ApproximateControl const &ctrl)
Construct a new Approximate object, inferring the type from the type of the given MaskedImage...
Include errors of requested quantities.
Pass parameters to a Background object.
A virtual base class to evaluate image background levels.
An integer coordinate rectangle.
Property getStatisticsProperty() const
Interpolate::Style _asUsedInterpStyle
the style we actually used
std::shared_ptr< StatisticsControl > getStatisticsControl()
Statistics makeStatistics(lsst::afw::image::Image< Pixel > const &img, lsst::afw::image::Mask< image::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl=StatisticsControl())
Handle a watered-down front-end to the constructor (no variance)
y_iterator col_end(int x) const
Return an y_iterator to the start of the y'th row.
#define LSST_makeBackground_getImage_types
Interpolate::Style lookupMaxInterpStyle(int const n)
Get the highest order Interpolation::Style available for 'n' points.
int lookupMinInterpPoints(Interpolate::Style const style)
Get the minimum number of points needed to use the requested interpolation style. ...
y_iterator col_begin(int x) const
Return an y_iterator to the start of the y'th row.
std::vector< int > _xorig
x origin pix coords of sub images
std::shared_ptr< BackgroundControl > _bctrl
control info set by user.
int getWidth() const
Return the number of columns in the image.
std::vector< int > _ysize
y size ...
UndersampleStyle _asUsedUndersampleStyle
the undersampleStyle we actually used
virtual BackgroundMI & operator+=(float const delta)
Add a scalar to the Background (equivalent to adding a constant to the original image) ...
A class to evaluate image background levels.
std::shared_ptr< lsst::afw::image::Image< PixelT > > getImage() const
Method to interpolate and return the background for entire image.
Point2I const getMin() const
virtual BackgroundMI & operator-=(float const delta)
Subtract a scalar from the Background (equivalent to subtracting a constant from the original image) ...
A class to represent a 2-dimensional array of pixels.