LSSTApplications  11.0-13-gbb96280,12.1.rc1,12.1.rc1+1,12.1.rc1+2,12.1.rc1+5,12.1.rc1+8,12.1.rc1-1-g06d7636+1,12.1.rc1-1-g253890b+5,12.1.rc1-1-g3d31b68+7,12.1.rc1-1-g3db6b75+1,12.1.rc1-1-g5c1385a+3,12.1.rc1-1-g83b2247,12.1.rc1-1-g90cb4cf+6,12.1.rc1-1-g91da24b+3,12.1.rc1-2-g3521f8a,12.1.rc1-2-g39433dd+4,12.1.rc1-2-g486411b+2,12.1.rc1-2-g4c2be76,12.1.rc1-2-gc9c0491,12.1.rc1-2-gda2cd4f+6,12.1.rc1-3-g3391c73+2,12.1.rc1-3-g8c1bd6c+1,12.1.rc1-3-gcf4b6cb+2,12.1.rc1-4-g057223e+1,12.1.rc1-4-g19ed13b+2,12.1.rc1-4-g30492a7
LSSTDataManagementBasePackage
Background.cc
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008-2015 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 <https://www.lsstcorp.org/LegalNotices/>.
23  */
24 
32 #include <iostream>
33 #include <limits>
34 #include <cmath>
35 #include <vector>
40 
41 namespace lsst {
42 namespace ex = pex::exceptions;
43 
44 namespace afw {
45 namespace math {
46 
57 template<typename ImageT>
58 Background::Background(ImageT const& img,
59  BackgroundControl const& bgCtrl
60  ) :
61  lsst::daf::base::Citizen(typeid(this)),
62  _imgBBox(img.getBBox()),
63  _bctrl(new BackgroundControl(bgCtrl)),
64  _asUsedInterpStyle(Interpolate::UNKNOWN),
65  _asUsedUndersampleStyle(THROW_EXCEPTION),
66  _xcen(0), _ycen(0), _xorig(0), _yorig(0), _xsize(0), _ysize(0)
67 {
68  if (_imgBBox.isEmpty()) {
69  throw LSST_EXCEPT(ex::InvalidParameterError, "Image contains no pixels");
70  }
71 
72  // Check that an int's large enough to hold the number of pixels
73  if (_imgBBox.getWidth()*static_cast<double>(_imgBBox.getHeight()) > std::numeric_limits<int>::max()) {
74  throw LSST_EXCEPT(lsst::pex::exceptions::OverflowError,
75  str(boost::format("Image %dx%d has more pixels than fit in an int (%d)")
76  % _imgBBox.getWidth() % _imgBBox.getHeight() % std::numeric_limits<int>::max()));
77  }
78 
80 }
81 
82 /************************************************************************************************************/
90  int const nx,
91  int const ny
92  ) :
93  lsst::daf::base::Citizen(typeid(this)),
94  _imgBBox(imageBBox),
95  _bctrl(new BackgroundControl(nx, ny)),
96  _asUsedInterpStyle(Interpolate::UNKNOWN),
97  _asUsedUndersampleStyle(THROW_EXCEPTION),
98  _xcen(0), _ycen(0), _xorig(0), _yorig(0), _xsize(0), _ysize(0)
99 {
100  if (_imgBBox.isEmpty()) {
101  throw LSST_EXCEPT(ex::InvalidParameterError, "Image contains no pixels");
102  }
103 
104  // Check that an int's large enough to hold the number of pixels
105  if (_imgBBox.getWidth()*static_cast<double>(_imgBBox.getHeight()) > std::numeric_limits<int>::max()) {
106  throw LSST_EXCEPT(lsst::pex::exceptions::OverflowError,
107  str(boost::format("Image %dx%d has more pixels than fit in an int (%d)")
108  % _imgBBox.getWidth() % _imgBBox.getHeight() % std::numeric_limits<int>::max()));
109  }
110 
112 }
113 
114 /************************************************************************************************************/
119 void
120 Background::_setCenOrigSize(int const width, int const height,
121  int const nxSample, int const nySample)
122 {
123  _xcen.resize( nxSample); _ycen.resize(nySample);
124  _xorig.resize(nxSample); _yorig.resize(nySample);
125  _xsize.resize(nxSample), _ysize.resize(nySample);
126 
127  // Compute the centers and origins for the cells
128  for (int iX = 0; iX < nxSample; ++iX) {
129  const int endx = std::min(((iX+1)*width + nxSample/2)/nxSample, width);
130  _xorig[iX] = (iX == 0) ? 0 : _xorig[iX-1] + _xsize[iX-1];
131  _xsize[iX] = endx - _xorig[iX];
132  _xcen [iX] = _xorig[iX] + (0.5 * _xsize[iX]) - 0.5;
133  }
134 
135  for (int iY = 0; iY < nySample; ++iY) {
136  const int endy = std::min(((iY+1)*height + nySample/2)/nySample, height);
137  _yorig[iY] = (iY == 0) ? 0 : _yorig[iY-1] + _ysize[iY-1];
138  _ysize[iY] = endy - _yorig[iY];
139  _ycen [iY] = _yorig[iY] + (0.5 * _ysize[iY]) - 0.5;
140  }
141 }
142 
143 /************************************************************************************************************/
147 UndersampleStyle stringToUndersampleStyle(std::string const &style) {
148  static std::map<std::string, UndersampleStyle> undersampleStrings;
149  if (undersampleStrings.size() == 0) {
150  undersampleStrings["THROW_EXCEPTION"] = THROW_EXCEPTION;
151  undersampleStrings["REDUCE_INTERP_ORDER"] = REDUCE_INTERP_ORDER;
152  undersampleStrings["INCREASE_NXNYSAMPLE"] = INCREASE_NXNYSAMPLE;
153  }
154 
155  if (undersampleStrings.find(style) == undersampleStrings.end()) {
156  throw LSST_EXCEPT(ex::InvalidParameterError, "Understample style not defined: "+style);
157  }
158  return undersampleStrings[style];
159 }
161 /*
162  * Explicit instantiations
163  *
164  */
165 #define INSTANTIATE_BACKGROUND(TYPE) \
166  template Background::Background(image::Image<TYPE> const& img, \
167  BackgroundControl const& bgCtrl); \
168  template Background::Background(image::MaskedImage<TYPE> const& img, \
169  BackgroundControl const& bgCtrl); \
170  template PTR(image::Image<TYPE>) Background::getImage<TYPE>(Interpolate::Style const, \
171  UndersampleStyle const) const;
172 
173 
174 INSTANTIATE_BACKGROUND(float)
175 
176 }}} // lsst::afw::math
std::vector< int > _xsize
x size of sub images
Definition: Background.h:339
UndersampleStyle stringToUndersampleStyle(std::string const &style)
Conversion function to switch a string to an UndersampleStyle.
Definition: Background.cc:147
std::vector< double > _xcen
x center pix coords of sub images
Definition: Background.h:335
Interpolate values for a set of x,y vector&lt;&gt;s.
Definition: Interpolate.h:38
std::vector< int > _yorig
y origin ...
Definition: Background.h:338
Background(ImageT const &img, BackgroundControl const &bgCtrl)
Constructor for Background.
Definition: Background.cc:58
geom::Box2I _imgBBox
size and origin of input image
Definition: Background.h:330
std::vector< double > _ycen
y center ...
Definition: Background.h:336
Pass parameters to a Background object.
Definition: Background.h:61
An integer coordinate rectangle.
Definition: Box.h:53
bool isEmpty() const
Return true if the box contains no points.
Definition: Box.h:166
int getWidth() const
Definition: Box.h:154
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
int getHeight() const
Definition: Box.h:155
std::vector< int > _xorig
x origin pix coords of sub images
Definition: Background.h:337
std::vector< int > _ysize
y size ...
Definition: Background.h:340
Compute Image Statistics.
Implementation of the Class MaskedImage.
void _setCenOrigSize(int const width, int const height, int const nxSample, int const nySample)
Definition: Background.cc:120