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