lsst.meas.algorithms  13.0-24-g22030a45+6
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"
27 #include "lsst/afw/math/warpExposure.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 
57 afw::geom::Box2I computeBBoxFromTransform
58  (afw::geom::Box2I const bbox,
59  afw::geom::AffineTransform const &t
60 ) {
61  static const int dst_padding = 0;
62 
63  afw::geom::AffineXYTransform xyTransform(t);
64 
65  // This is the maximum scaling I can imagine we'd want - it's approximately what you'd
66  // get from trying to coadd 2"-pixel data (e.g. 2MASS) onto a 0.01"-pixel grid (e.g.
67  // from JWST). Anything beyond that is probably a bug elsewhere in the code, and
68  // catching that can save us from segfaults and catastrophic memory consumption.
69  static const double maxTransformCoeff = 200.0;
70 
71  if (t.getLinear().getMatrix().lpNorm<Eigen::Infinity>() > maxTransformCoeff) {
72  throw LSST_EXCEPT(
73  pex::exceptions::RangeError,
74  "Unexpectedly large transform passed to WarpedPsf");
75  }
76 
77  // min/max coordinate values in input image
78  int in_xlo = bbox.getMinX();
79  int in_xhi = bbox.getMinX() + bbox.getWidth() - 1;
80  int in_ylo = bbox.getMinY();
81  int in_yhi = bbox.getMinY() + bbox.getHeight() - 1;
82 
83  // corners of output image
84  afw::geom::Point2D c00 = t(afw::geom::Point2D(in_xlo, in_ylo));
85  afw::geom::Point2D c01 = t(afw::geom::Point2D(in_xlo, in_yhi));
86  afw::geom::Point2D c10 = t(afw::geom::Point2D(in_xhi, in_ylo));
87  afw::geom::Point2D c11 = t(afw::geom::Point2D(in_xhi, in_yhi));
88 
89  //
90  // bounding box for output image
91  //
92  int out_xlo = floor(min4(c00.getX(), c01.getX(), c10.getX(), c11.getX())) - dst_padding;
93  int out_ylo = floor(min4(c00.getY(), c01.getY(), c10.getY(), c11.getY())) - dst_padding;
94  int out_xhi = ceil(max4(c00.getX(), c01.getX(), c10.getX(), c11.getX())) + dst_padding;
95  int out_yhi = ceil(max4(c00.getY(), c01.getY(), c10.getY(), c11.getY())) + dst_padding;
96 
97  afw::geom::Box2I ret = afw::geom::Box2I(afw::geom::Point2I(out_xlo, out_ylo),
98  afw::geom::Extent2I(out_xhi - out_xlo + 1, out_yhi-out_ylo + 1));
99  return ret;
100 }
101 
113 PTR(afw::detection::Psf::Image) warpAffine(
114  afw::detection::Psf::Image const &im, afw::geom::AffineTransform const &t,
115  afw::math::WarpingControl const &wc
116 ) {
117  afw::geom::AffineXYTransform xyTransform(t);
118 
119  afw::math::SeparableKernel const& kernel = *wc.getWarpingKernel();
120  afw::geom::Point2I const& center = kernel.getCtr();
121  int const xPad = std::max(center.getX(), kernel.getWidth() - center.getX());
122  int const yPad = std::max(center.getY(), kernel.getHeight() - center.getY());
123 
124  // allocate output image
125  afw::geom::Box2I bbox = computeBBoxFromTransform(im.getBBox(), t);
126  PTR(afw::detection::Psf::Image) ret = std::make_shared<afw::detection::Psf::Image>(bbox);
127 
128  // zero-pad input image
129  PTR(afw::detection::Psf::Image) im_padded = zeroPadImage(im, xPad, yPad);
130 
131  // warp it!
132  afw::math::warpImage(*ret, *im_padded, xyTransform, wc, 0.0);
133  return ret;
134 }
135 
136 } // anonymous
137 
139  PTR(afw::detection::Psf const) undistortedPsf,
140  PTR(afw::geom::XYTransform const) distortion,
141  CONST_PTR(afw::math::WarpingControl) control
142  ) :
143  ImagePsf(false),
144  _undistortedPsf(undistortedPsf),
145  _distortion(distortion),
146  _warpingControl(control)
147 {
148  _init();
149 }
150 
152  PTR(afw::detection::Psf const) undistortedPsf,
153  PTR(afw::geom::XYTransform const) distortion,
154  std::string const& kernelName,
155  unsigned int cache
156  ) :
157  ImagePsf(false),
158  _undistortedPsf(undistortedPsf),
159  _distortion(distortion),
160  _warpingControl(new afw::math::WarpingControl(kernelName, "", cache))
161 {
162  _init();
163 }
164 
165 void WarpedPsf::_init()
166 {
167  if (!_undistortedPsf) {
168  throw LSST_EXCEPT(
169  pex::exceptions::LogicError,
170  "Undistorted Psf passed to WarpedPsf must not be None/NULL"
171  );
172  }
173  if (!_distortion) {
174  throw LSST_EXCEPT(
175  pex::exceptions::LogicError,
176  "XYTransform passed to WarpedPsf must not be None/NULL"
177  );
178  }
179  if (!_warpingControl) {
180  throw LSST_EXCEPT(
181  pex::exceptions::LogicError,
182  "WarpingControl passed to WarpedPsf must not be None/NULL"
183  );
184  }
185 }
186 
187 afw::geom::Point2D WarpedPsf::getAveragePosition() const {
188  return _distortion->forwardTransform(_undistortedPsf->getAveragePosition());
189 }
190 
191 PTR(afw::detection::Psf) WarpedPsf::clone() const {
192  return std::make_shared<WarpedPsf>(_undistortedPsf->clone(), _distortion->clone(), _warpingControl);
193 }
194 
195 PTR(afw::detection::Psf) WarpedPsf::resized(int width, int height) const {
196  // For a given set of requested dimensions and distortion, it is not guaranteed that a
197  // _undistortedPsf would exist to manifest those dimensions after distortion
198  // Not possible to implement with member data currently in WarpedPsf
199  throw LSST_EXCEPT(pex::exceptions::LogicError, "Not Implemented");
200  }
201 
202 PTR(afw::detection::Psf::Image) WarpedPsf::doComputeKernelImage(
203  afw::geom::Point2D const & position, afw::image::Color const & color
204 ) const {
205  afw::geom::AffineTransform t = _distortion->linearizeReverseTransform(position);
206  afw::geom::Point2D tp = t(position);
207 
208  PTR(Image) im = _undistortedPsf->computeKernelImage(tp, color);
209 
210  // Go to the warped coordinate system with 'p' at the origin
211  PTR(afw::detection::Psf::Psf::Image) ret
212  = warpAffine(*im, afw::geom::AffineTransform(t.invert().getLinear()), *_warpingControl);
213 
214  double normFactor = 1.0;
215  //
216  // Normalize the output image to sum 1
217  // FIXME defining a member function Image::getSum() would be convenient here and in other places
218  //
219  normFactor = 0.0;
220  for (int y = 0; y != ret->getHeight(); ++y) {
221  afw::detection::Psf::Image::x_iterator imEnd = ret->row_end(y);
222  for (afw::detection::Psf::Image::x_iterator imPtr = ret->row_begin(y); imPtr != imEnd; imPtr++) {
223  normFactor += *imPtr;
224  }
225  }
226  if (normFactor == 0.0) {
227  throw LSST_EXCEPT(pex::exceptions::InvalidParameterError, "psf image has sum 0");
228  }
229  *ret /= normFactor;
230  return ret;
231 }
232 
233 afw::geom::Box2I WarpedPsf::doComputeBBox(
234  afw::geom::Point2D const & position, afw::image::Color const & color
235 ) const {
236  afw::geom::AffineTransform t = _distortion->linearizeReverseTransform(position);
237  afw::geom::Point2D tp = t(position);
238  afw::geom::Box2I bboxUndistorted = _undistortedPsf->computeBBox(tp, color);
239  afw::geom::Box2I ret = computeBBoxFromTransform(bboxUndistorted,
240  afw::geom::AffineTransform(t.invert().getLinear()));
241  return ret;
242 }
243 
244 }}} // namepace lsst::meas::algorithms
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:138
virtual afw::geom::Point2D getAveragePosition() const
Return the average of the positions of the stars that went into this Psf.
Definition: WarpedPsf.cc:187
virtual boost::shared_ptr< afw::detection::Psf::Image > doComputeKernelImage(afw::geom::Point2D const &position, afw::image::Color const &color) const
Definition: WarpedPsf.cc:202
Point< int, 2 > Point2I
Definition: PSF.h:39
virtual boost::shared_ptr< afw::detection::Psf > resized(int width, int height) const
Return a clone with specified kernel dimensions.
Definition: WarpedPsf.cc:195
virtual boost::shared_ptr< afw::detection::Psf > clone() const
Polymorphic deep copy. Usually unnecessary, as Psfs are immutable.
Definition: WarpedPsf.cc:191
afw::table::Key< double > b
An intermediate base class for Psfs that use an image representation.
Definition: ImagePsf.h:37
boost::shared_ptr< afw::detection::Psf const > _undistortedPsf
Definition: WarpedPsf.h:90
boost::shared_ptr< afw::geom::XYTransform const > _distortion
Definition: WarpedPsf.h:91