44 namespace ex = pex::exceptions;
54 void cullNan(std::vector<double>
const &values, std::vector<double>
const &refs,
55 std::vector<double> &culledValues, std::vector<double> &culledRefs,
56 double const defaultValue=std::numeric_limits<double>::quiet_NaN()
58 if (culledValues.capacity() == 0) {
59 culledValues.reserve(refs.size());
63 if (culledRefs.capacity() == 0) {
64 culledRefs.reserve(refs.size());
71 for (std::vector<double>::const_iterator pVal = values.begin(), pRef = refs.begin();
72 pRef != refs.end(); ++pRef, ++pVal) {
74 culledValues.push_back(*pVal);
75 culledRefs.push_back(*pRef);
76 }
else if(haveDefault) {
77 culledValues.push_back(*pVal);
78 culledRefs.push_back(defaultValue);
108 template<
typename ImageT>
112 Background(img, bgCtrl), _statsImage(
image::MaskedImage<InternalPixelT>())
124 for (
int iX = 0; iX < nxSample; ++iX) {
125 for (
int iY = 0; iY < nySample; ++iY) {
131 im(iX, iY) = res.first;
132 var(iX, iY) = res.second;
142 Background(imageBBox, statsImage.getWidth(), statsImage.getHeight()),
143 _statsImage(statsImage)
149 int const iX, std::vector<int>
const& ypix)
const
163 std::vector<double> ycenTmp, gridTmp;
164 cullNan(
_ycen, _grid, ycenTmp, gridTmp);
169 }
catch(pex::exceptions::OutOfRangeError &e) {
170 switch (undersampleStyle) {
176 if (gridTmp.empty()) {
178 ycenTmp.push_back(0);
179 gridTmp.push_back(std::numeric_limits<double>::quiet_NaN());
188 LSST_EXCEPT_ADD(e,
"The BackgroundControl UndersampleStyle INCREASE_NXNYSAMPLE is not supported.");
192 "UndersampleStyle %d is not defined.") % undersampleStyle));
200 for (
int iY = 0; iY <
height; ++iY) {
236 (void)getImage<InternalPixelT>(interpStyle);
240 std::vector<double> bg_x(nxSample);
241 for (
int iX = 0; iX < nxSample; iX++) {
244 std::vector<double> xcenTmp, bgTmp;
245 cullNan(
_xcen, bg_x, xcenTmp, bgTmp);
249 return static_cast<double>(intobj->interpolate(x));
258 template<
typename PixelT>
260 geom::Box2I const& bbox,
265 if (!_imgBBox.contains(bbox)) {
267 str(
boost::format(
"BBox (%d:%d,%d:%d) out of range (%d:%d,%d:%d)") %
268 bbox.getMinX() % bbox.getMaxX() % bbox.getMinY() % bbox.getMaxY() %
269 _imgBBox.getMinX() % _imgBBox.getMaxX() %
270 _imgBBox.getMinY() % _imgBBox.getMaxY()));
272 int const nxSample = _statsImage.getWidth();
273 int const nySample = _statsImage.getHeight();
283 _asUsedInterpStyle = interpStyle;
284 _asUsedUndersampleStyle = undersampleStyle;
292 switch (undersampleStyle) {
294 if (isXundersampled && isYundersampled) {
296 "nxSample and nySample have too few points for requested interpolation style.");
297 }
else if (isXundersampled) {
299 "nxSample has too few points for requested interpolation style.");
300 }
else if (isYundersampled) {
302 "nySample has too few points for requested interpolation style.");
306 if (isXundersampled || isYundersampled) {
309 interpStyle = (nxSample < nySample) ? xStyle : yStyle;
310 _asUsedInterpStyle = interpStyle;
314 if (isXundersampled || isYundersampled) {
316 "The BackgroundControl UndersampleStyle INCREASE_NXNYSAMPLE is not supported.");
322 "UndersampleStyle %d is not defined.") % undersampleStyle));
327 return doGetApproximate<PixelT>(*_bctrl->getApproximateControl(), _asUsedUndersampleStyle)->getImage();
333 int const width = _imgBBox.getWidth();
334 int const height = _imgBBox.getHeight();
335 auto const bboxOff = bbox.getMin() - _imgBBox.getMin();
337 std::vector<int> ypix(height);
338 for (
int iY = 0; iY <
height; ++iY) {
342 _gridColumns.resize(width);
343 for (
int iX = 0; iX < nxSample; ++iX) {
344 _setGridColumns(interpStyle, undersampleStyle, iX, ypix);
355 std::vector<double> xcenTmp, bgTmp;
361 double defaultValue = std::numeric_limits<double>::quiet_NaN();
363 for (
int y = 0, iY = bboxOff.getY();
y < bbox.
getHeight(); ++
y, ++iY) {
365 std::vector<double> bg_x(nxSample);
366 for (
int iX = 0; iX < nxSample; iX++) {
367 bg_x[iX] =
static_cast<double>(_gridColumns[iX][iY]);
369 cullNan(_xcen, bg_x, xcenTmp, bgTmp, defaultValue);
374 }
catch(pex::exceptions::OutOfRangeError &e) {
375 switch (undersampleStyle) {
382 xcenTmp.push_back(0);
383 bgTmp.push_back(defaultValue);
393 LSST_EXCEPT_ADD(e,
"The BackgroundControl UndersampleStyle INCREASE_NXNYSAMPLE is not supported.");
397 "UndersampleStyle %d is not defined.") % undersampleStyle));
406 for (
int iX = bboxOff.getX(),
x = 0;
x < bbox.
getWidth(); ++iX, ++
x) {
407 (*bg)(
x,
y) = static_cast<PixelT>(intobj->interpolate(iX));
410 bg->setXY0(bbox.getMin());
417 template<
typename PixelT>
432 #define CREATE_BACKGROUND(m, v, TYPE) \
433 template BackgroundMI::BackgroundMI(image::Image<TYPE> const& img, \
434 BackgroundControl const& bgCtrl); \
435 template BackgroundMI::BackgroundMI(image::MaskedImage<TYPE> const& img, \
436 BackgroundControl const& bgCtrl); \
437 PTR(image::Image<TYPE>) \
438 BackgroundMI::_getImage( \
439 geom::Box2I const& bbox, \
440 Interpolate::Style const interpStyle, \
441 UndersampleStyle const undersampleStyle, \
445 return BackgroundMI::doGetImage<TYPE>(bbox, interpStyle, undersampleStyle); \
448 #define CREATE_getApproximate(m, v, TYPE) \
449 PTR(Approximate<TYPE>) BackgroundMI::_getApproximate( \
450 ApproximateControl const& actrl, \
451 UndersampleStyle const undersampleStyle, \
455 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.
boost::shared_ptr< Interpolate > makeInterpolate(std::vector< double > const &x, std::vector< double > const &y, Interpolate::Style const style=Interpolate::AKIMA_SPLINE)
SelectEigenView< T >::Type copy(Eigen::EigenBase< T > const &other)
Copy an arbitrary Eigen expression into a new EigenView.
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.
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,...)
y_iterator col_begin(int x) const
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)
Construct a new Approximate object, inferring the type from the type of the given MaskedImage...
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)