43 namespace ex = pex::exceptions;
53 void cullNan(std::vector<double>
const &values, std::vector<double>
const &refs,
54 std::vector<double> &culledValues, std::vector<double> &culledRefs,
55 double const defaultValue=std::numeric_limits<double>::quiet_NaN()
57 if (culledValues.capacity() == 0) {
58 culledValues.reserve(refs.size());
62 if (culledRefs.capacity() == 0) {
63 culledRefs.reserve(refs.size());
68 bool const haveDefault = !std::isnan(defaultValue);
70 for (std::vector<double>::const_iterator pVal = values.begin(), pRef = refs.begin();
71 pRef != refs.end(); ++pRef, ++pVal) {
72 if (!std::isnan(*pRef)) {
73 culledValues.push_back(*pVal);
74 culledRefs.push_back(*pRef);
75 }
else if(haveDefault) {
76 culledValues.push_back(*pVal);
77 culledRefs.push_back(defaultValue);
107 template<
typename ImageT>
111 Background(img, bgCtrl), _statsImage(
image::MaskedImage<InternalPixelT>())
123 for (
int iX = 0; iX < nxSample; ++iX) {
124 for (
int iY = 0; iY < nySample; ++iY) {
130 im(iX, iY) = res.first;
131 var(iX, iY) = res.second;
141 Background(imageBBox, statsImage.getWidth(), statsImage.getHeight()),
142 _statsImage(statsImage)
148 int const iX, std::vector<int>
const& ypix)
const
162 std::vector<double> ycenTmp, gridTmp;
163 cullNan(
_ycen, _grid, ycenTmp, gridTmp);
168 }
catch(pex::exceptions::OutOfRangeError &e) {
169 switch (undersampleStyle) {
175 if (gridTmp.empty()) {
177 ycenTmp.push_back(0);
178 gridTmp.push_back(std::numeric_limits<double>::quiet_NaN());
187 LSST_EXCEPT_ADD(e,
"The BackgroundControl UndersampleStyle INCREASE_NXNYSAMPLE is not supported.");
191 "UndersampleStyle %d is not defined.") % undersampleStyle));
199 for (
int iY = 0; iY <
height; ++iY) {
235 (void)getImage<InternalPixelT>(interpStyle);
239 std::vector<double> bg_x(nxSample);
240 for (
int iX = 0; iX < nxSample; iX++) {
243 std::vector<double> xcenTmp, bgTmp;
244 cullNan(
_xcen, bg_x, xcenTmp, bgTmp);
248 return static_cast<double>(intobj->interpolate(x));
257 template<
typename PixelT>
259 geom::Box2I const& bbox,
264 if (!_imgBBox.contains(bbox)) {
266 str(
boost::format(
"BBox (%d:%d,%d:%d) out of range (%d:%d,%d:%d)") %
267 bbox.getMinX() % bbox.getMaxX() % bbox.getMinY() % bbox.getMaxY() %
268 _imgBBox.getMinX() % _imgBBox.getMaxX() %
269 _imgBBox.getMinY() % _imgBBox.getMaxY()));
271 int const nxSample = _statsImage.getWidth();
272 int const nySample = _statsImage.getHeight();
282 _asUsedInterpStyle = interpStyle;
283 _asUsedUndersampleStyle = undersampleStyle;
291 switch (undersampleStyle) {
293 if (isXundersampled && isYundersampled) {
295 "nxSample and nySample have too few points for requested interpolation style.");
296 }
else if (isXundersampled) {
298 "nxSample has too few points for requested interpolation style.");
299 }
else if (isYundersampled) {
301 "nySample has too few points for requested interpolation style.");
305 if (isXundersampled || isYundersampled) {
308 interpStyle = (nxSample < nySample) ? xStyle : yStyle;
309 _asUsedInterpStyle = interpStyle;
313 if (isXundersampled || isYundersampled) {
315 "The BackgroundControl UndersampleStyle INCREASE_NXNYSAMPLE is not supported.");
321 "UndersampleStyle %d is not defined.") % undersampleStyle));
326 return doGetApproximate<PixelT>(*_bctrl->getApproximateControl(), _asUsedUndersampleStyle)->getImage();
332 int const width = _imgBBox.getWidth();
333 int const height = _imgBBox.getHeight();
334 auto const bboxOff = bbox.getMin() - _imgBBox.getMin();
336 std::vector<int> ypix(height);
337 for (
int iY = 0; iY <
height; ++iY) {
341 _gridColumns.resize(width);
342 for (
int iX = 0; iX < nxSample; ++iX) {
343 _setGridColumns(interpStyle, undersampleStyle, iX, ypix);
354 std::vector<double> xcenTmp, bgTmp;
360 double defaultValue = std::numeric_limits<double>::quiet_NaN();
362 for (
int y = 0, iY = bboxOff.getY();
y < bbox.
getHeight(); ++
y, ++iY) {
364 std::vector<double> bg_x(nxSample);
365 for (
int iX = 0; iX < nxSample; iX++) {
366 bg_x[iX] =
static_cast<double>(_gridColumns[iX][iY]);
368 cullNan(_xcen, bg_x, xcenTmp, bgTmp, defaultValue);
373 }
catch(pex::exceptions::OutOfRangeError &e) {
374 switch (undersampleStyle) {
381 xcenTmp.push_back(0);
382 bgTmp.push_back(defaultValue);
392 LSST_EXCEPT_ADD(e,
"The BackgroundControl UndersampleStyle INCREASE_NXNYSAMPLE is not supported.");
396 "UndersampleStyle %d is not defined.") % undersampleStyle));
405 for (
int iX = bboxOff.getX(),
x = 0;
x < bbox.
getWidth(); ++iX, ++
x) {
406 (*bg)(
x,
y) = static_cast<PixelT>(intobj->interpolate(iX));
409 bg->setXY0(bbox.getMin());
416 template<
typename PixelT>
431 #define CREATE_BACKGROUND(m, v, TYPE) \
432 template BackgroundMI::BackgroundMI(image::Image<TYPE> const& img, \
433 BackgroundControl const& bgCtrl); \
434 template BackgroundMI::BackgroundMI(image::MaskedImage<TYPE> const& img, \
435 BackgroundControl const& bgCtrl); \
436 PTR(image::Image<TYPE>) \
437 BackgroundMI::_getImage( \
438 geom::Box2I const& bbox, \
439 Interpolate::Style const interpStyle, \
440 UndersampleStyle const undersampleStyle, \
444 return BackgroundMI::doGetImage<TYPE>(bbox, interpStyle, undersampleStyle); \
447 #define CREATE_getApproximate(m, v, TYPE) \
448 PTR(Approximate<TYPE>) BackgroundMI::_getApproximate( \
449 ApproximateControl const& actrl, \
450 UndersampleStyle const undersampleStyle, \
454 return BackgroundMI::doGetApproximate<TYPE>(actrl, undersampleStyle); \
lsst::afw::image::MaskedImage< InternalPixelT > _statsImage
std::vector< int > _xsize
x size of sub images
virtual void operator-=(float const delta)
Subtract a scalar from the Background (equivalent to subtracting a constant from the original image) ...
std::vector< double > _xcen
x center pix coords of sub images
Interpolate values for a set of x,y vector<>s.
std::vector< int > _yorig
y origin ...
std::vector< std::vector< double > > _gridColumns
int getHeight() const
Return the number of rows in the image.
BackgroundMI(ImageT const &img, BackgroundControl const &bgCtrl)
Constructor for BackgroundMI.
virtual void operator+=(float const delta)
Add a scalar to the Background (equivalent to adding a constant to the original image) ...
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(bool const noThrow=false) const
Return a (Ptr to) the MaskedImage's image.
boost::shared_ptr< Interpolate > makeInterpolate(std::vector< double > const &x, std::vector< double > const &y, Interpolate::Style const style)
A factory function to make Interpolate objects.
Include errors of requested quantities.
Pass parameters to a Background object.
Approximate values for a MaskedImage.
A virtual base class to evaluate image background levels.
An integer coordinate rectangle.
Property getStatisticsProperty() const
VariancePtr getVariance(bool const noThrow=false) const
Return a (Ptr to) the MaskedImage's variance.
table::Key< table::Array< Kernel::Pixel > > image
Control how to make an approximation.
void ImageT ImageT int float saturatedPixelValue int const width
void _setGridColumns(Interpolate::Style const interpStyle, UndersampleStyle const undersampleStyle, int const iX, std::vector< int > const &ypix) const
geom::Extent2I getDimensions() const
Return the image's size; useful for passing to constructors.
int getHeight() const
Return the number of rows in the image.
y_iterator col_end(int x) const
Return an y_iterator to the end of the y'th row.
boost::shared_ptr< StatisticsControl > getStatisticsControl()
#define LSST_makeBackground_getImage_types
void ImageT ImageT int float saturatedPixelValue int const height
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. ...
#define LSST_EXCEPT(type,...)
Create an exception with a given type and message and optionally other arguments (dependent on the ty...
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
boost::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)
A factory function to make Approximate objects.
int getWidth() const
Return the number of columns in the image.
Statistics makeStatistics(afwImage::Mask< afwImage::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl)
Specialization to handle Masks.
std::vector< int > _ysize
y size ...
Compute Image Statistics.
Implementation of the Class MaskedImage.
A class to evaluate image background levels.
int getWidth() const
Return the number of columns in the image.
#define LSST_EXCEPT_ADD(e, m)
Add the current location and a message to an existing exception before rethrowing it...