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
WarpedPsf.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 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 <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 #include "lsst/pex/exceptions.h"
28 #include "lsst/afw/image/Image.h"
29 
30 namespace lsst { namespace meas { namespace algorithms {
31 
32 namespace {
33 
34 inline double min4(double a, double b, double c, double d) {
35  return std::min(std::min(a,b), std::min(c,d));
36 }
37 
38 inline double max4(double a, double b, double c, double d) {
39  return std::max(std::max(a,b), std::max(c,d));
40 }
41 
42 // TODO: make this routine externally callable and more generic using templates
43 // (also useful in e.g. math/offsetImage.cc)
44 PTR(afw::detection::Psf::Image) zeroPadImage(afw::detection::Psf::Image const &im, int xPad, int yPad) {
45  int nx = im.getWidth();
46  int ny = im.getHeight();
47 
48  PTR(afw::detection::Psf::Image) out = std::make_shared<afw::detection::Psf::Image>(nx+2*xPad, ny+2*yPad);
49  out->setXY0(im.getX0()-xPad, im.getY0()-yPad);
50 
51  afw::geom::Box2I box(afw::geom::Point2I(xPad,yPad), afw::geom::Extent2I(nx,ny));
52  out->assign(im, box, afw::image::LOCAL);
53 
54  return out;
55 }
56 
68 PTR(afw::detection::Psf::Image) warpAffine(
69  afw::detection::Psf::Image const &im, afw::geom::AffineTransform const &t,
70  afw::math::WarpingControl const &wc
71 ) {
72  static const int dst_padding = 0;
73 
74  afw::geom::AffineXYTransform xyTransform(t);
75 
76  afw::math::SeparableKernel const& kernel = *wc.getWarpingKernel();
77  afw::geom::Point2I const& center = kernel.getCtr();
78  int const xPad = std::max(center.getX(), kernel.getWidth() - center.getX());
79  int const yPad = std::min(center.getY(), kernel.getHeight() - center.getY());
80 
81  // This is the maximum scaling I can imagine we'd want - it's approximately what you'd
82  // get from trying to coadd 2"-pixel data (e.g. 2MASS) onto a 0.01"-pixel grid (e.g.
83  // from JWST). Anything beyond that is probably a bug elsewhere in the code, and
84  // catching that can save us from segfaults and catastrophic memory consumption.
85  static const double maxTransformCoeff = 200.0;
86 
87  if (t.getLinear().getMatrix().lpNorm<Eigen::Infinity>() > maxTransformCoeff) {
88  throw LSST_EXCEPT(
89  pex::exceptions::RangeError,
90  "Unexpectedly large transform passed to WarpedPsf"
91  );
92  }
93 
94  // min/max coordinate values in input image
95  int in_xlo = im.getX0();
96  int in_xhi = im.getX0() + im.getWidth() - 1;
97  int in_ylo = im.getY0();
98  int in_yhi = im.getY0() + im.getHeight() - 1;
99 
100  // corners of output image
101  afw::geom::Point2D c00 = t(afw::geom::Point2D(in_xlo,in_ylo));
102  afw::geom::Point2D c01 = t(afw::geom::Point2D(in_xlo,in_yhi));
103  afw::geom::Point2D c10 = t(afw::geom::Point2D(in_xhi,in_ylo));
104  afw::geom::Point2D c11 = t(afw::geom::Point2D(in_xhi,in_yhi));
105 
106  //
107  // bounding box for output image
108  //
109  int out_xlo = floor(min4(c00.getX(),c01.getX(),c10.getX(),c11.getX())) - dst_padding;
110  int out_ylo = floor(min4(c00.getY(),c01.getY(),c10.getY(),c11.getY())) - dst_padding;
111  int out_xhi = ceil(max4(c00.getX(),c01.getX(),c10.getX(),c11.getX())) + dst_padding;
112  int out_yhi = ceil(max4(c00.getY(),c01.getY(),c10.getY(),c11.getY())) + dst_padding;
113 
114  // allocate output image
116  = std::make_shared<afw::detection::Psf::Image>(out_xhi-out_xlo+1, out_yhi-out_ylo+1);
117  ret->setXY0(afw::geom::Point2I(out_xlo,out_ylo));
118 
119  // zero-pad input image
120  PTR(afw::detection::Psf::Image) im_padded = zeroPadImage(im, xPad, yPad);
121 
122  // warp it!
123  afw::math::warpImage(*ret, *im_padded, xyTransform, wc, 0.0);
124  return ret;
125 }
126 
127 } // anonymous
128 
130  PTR(afw::detection::Psf const) undistortedPsf,
131  PTR(afw::geom::XYTransform const) distortion,
132  CONST_PTR(afw::math::WarpingControl) control
133  ) :
134  ImagePsf(false),
135  _undistortedPsf(undistortedPsf),
136  _distortion(distortion),
137  _warpingControl(control)
138 {
139  _init();
140 }
141 
143  PTR(afw::detection::Psf const) undistortedPsf,
144  PTR(afw::geom::XYTransform const) distortion,
145  std::string const& kernelName,
146  unsigned int cache
147  ) :
148  ImagePsf(false),
149  _undistortedPsf(undistortedPsf),
150  _distortion(distortion),
151  _warpingControl(new afw::math::WarpingControl(kernelName, "", cache))
152 {
153  _init();
154 }
155 
157 {
158  if (!_undistortedPsf) {
159  throw LSST_EXCEPT(
160  pex::exceptions::LogicError,
161  "Undistorted Psf passed to WarpedPsf must not be None/NULL"
162  );
163  }
164  if (!_distortion) {
165  throw LSST_EXCEPT(
166  pex::exceptions::LogicError,
167  "XYTransform passed to WarpedPsf must not be None/NULL"
168  );
169  }
170  if (!_warpingControl) {
171  throw LSST_EXCEPT(
172  pex::exceptions::LogicError,
173  "WarpingControl passed to WarpedPsf must not be None/NULL"
174  );
175  }
176 }
177 
179  return _distortion->forwardTransform(_undistortedPsf->getAveragePosition());
180 }
181 
183  return std::make_shared<WarpedPsf>(_undistortedPsf->clone(), _distortion->clone(), _warpingControl);
184 }
185 
186 PTR(afw::detection::Psf::Image) WarpedPsf::doComputeKernelImage(
187  afw::geom::Point2D const & position, afw::image::Color const & color
188 ) const {
189  afw::geom::AffineTransform t = _distortion->linearizeReverseTransform(position);
190  afw::geom::Point2D tp = t(position);
191 
192  PTR(Image) im = _undistortedPsf->computeKernelImage(tp, color);
193 
194  // Go to the warped coordinate system with 'p' at the origin
195  PTR(afw::detection::Psf::Psf::Image) ret
196  = warpAffine(*im, afw::geom::AffineTransform(t.invert().getLinear()), *_warpingControl);
197 
198  double normFactor = 1.0;
199  //
200  // Normalize the output image to sum 1
201  // FIXME defining a member function Image::getSum() would be convenient here and in other places
202  //
203  normFactor = 0.0;
204  for (int y = 0; y != ret->getHeight(); ++y) {
205  afw::detection::Psf::Image::x_iterator imEnd = ret->row_end(y);
206  for (afw::detection::Psf::Image::x_iterator imPtr = ret->row_begin(y); imPtr != imEnd; imPtr++) {
207  normFactor += *imPtr;
208  }
209  }
210  if (normFactor == 0.0) {
211  throw LSST_EXCEPT(pex::exceptions::InvalidParameterError, "psf image has sum 0");
212  }
213  *ret /= normFactor;
214  return ret;
215 }
216 
217 }}} // namepace lsst::meas::algorithms
int y
virtual afw::geom::Point2D getAveragePosition() const
Return the average of the positions of the stars that went into this Psf.
Definition: WarpedPsf.cc:178
boost::shared_ptr< afw::math::WarpingControl const > _warpingControl
Definition: WarpedPsf.h:92
AffineTransform const invert() const
_view_t::x_iterator x_iterator
An iterator for traversing the pixels in a row.
Definition: Image.h:151
Point< double, 2 > Point2D
Definition: Point.h:288
Point< int, 2 > Point2I
Definition: PSF.h:39
#define CONST_PTR(...)
Definition: base.h:47
Extent< int, 2 > Extent2I
Definition: Extent.h:358
Extent< int, N > ceil(Extent< double, N > const &input)
int warpImage(DestImageT &destImage, lsst::afw::image::Wcs const &destWcs, SrcImageT const &srcImage, lsst::afw::image::Wcs const &srcWcs, WarpingControl const &control, typename DestImageT::SinglePixel padValue=lsst::afw::math::edgePixel< DestImageT >(typename lsst::afw::image::detail::image_traits< DestImageT >::image_category()))
Warp an Image or MaskedImage to a new Wcs. See also convenience function warpExposure() to warp an Ex...
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
An affine coordinate transformation consisting of a linear transformation and an offset.
Support for 2-D images.
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
Extent< int, N > floor(Extent< double, N > const &input)
Support for warping an image to a new WCS.
WarpedPsf(boost::shared_ptr< afw::detection::Psf const > undistortedPsf, boost::shared_ptr< afw::geom::XYTransform const > distortion, boost::shared_ptr< afw::math::WarpingControl const > control)
Construct WarpedPsf from unwarped psf and distortion.
Definition: WarpedPsf.cc:129
Virtual base class for 2D transforms.
Definition: XYTransform.h:48
#define PTR(...)
Definition: base.h:41
afw::table::Key< double > b
An intermediate base class for Psfs that use an image representation.
Definition: ImagePsf.h:37
LinearTransform const & getLinear() const
Include files required for standard LSST Exception handling.
A polymorphic base class for representing an image&#39;s Point Spread Function.
Definition: Psf.h:68
boost::shared_ptr< afw::detection::Psf const > _undistortedPsf
Definition: WarpedPsf.h:87
image::Image< Pixel > Image
Image type returned by computeImage.
Definition: Psf.h:79
A Psf class that maps an arbitrary Psf through a coordinate transformation.
Definition: WarpedPsf.h:49
boost::shared_ptr< afw::geom::XYTransform const > _distortion
Definition: WarpedPsf.h:88