LSSTApplications  21.0.0+75b29a8a7f,21.0.0+e70536a077,21.0.0-1-ga51b5d4+62c747d40b,21.0.0-11-ga6ea59e8e+47cba9fc36,21.0.0-2-g103fe59+914993bf7c,21.0.0-2-g1367e85+e2614ded12,21.0.0-2-g45278ab+e70536a077,21.0.0-2-g4bc9b9f+7b2b5f8678,21.0.0-2-g5242d73+e2614ded12,21.0.0-2-g54e2caa+6403186824,21.0.0-2-g7f82c8f+3ac4acbffc,21.0.0-2-g8dde007+04a6aea1af,21.0.0-2-g8f08a60+9402881886,21.0.0-2-ga326454+3ac4acbffc,21.0.0-2-ga63a54e+81dd751046,21.0.0-2-gc738bc1+5f65c6e7a9,21.0.0-2-gde069b7+26c92b3210,21.0.0-2-gecfae73+0993ddc9bd,21.0.0-2-gfc62afb+e2614ded12,21.0.0-21-gba890a8+5a4f502a26,21.0.0-23-g9966ff26+03098d1af8,21.0.0-3-g357aad2+8ad216c477,21.0.0-3-g4be5c26+e2614ded12,21.0.0-3-g6d51c4a+4d2fe0280d,21.0.0-3-g7d9da8d+75b29a8a7f,21.0.0-3-gaa929c8+522e0f12c2,21.0.0-3-ge02ed75+4d2fe0280d,21.0.0-4-g3300ddd+e70536a077,21.0.0-4-gc004bbf+eac6615e82,21.0.0-4-gccdca77+f94adcd104,21.0.0-4-gd1c1571+18b81799f9,21.0.0-5-g7b47fff+4d2fe0280d,21.0.0-5-gb155db7+d2632f662b,21.0.0-5-gdf36809+637e4641ee,21.0.0-6-g722ad07+28c848f42a,21.0.0-7-g959bb79+522e0f12c2,21.0.0-7-gfd72ab2+cf01990774,21.0.0-9-g87fb7b8d+e2ab11cdd6,w.2021.04
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 
26 #include "lsst/geom/Box.h"
27 #include "lsst/pex/exceptions.h"
30 #include "lsst/afw/image/Image.h"
31 
32 namespace lsst {
33 namespace meas {
34 namespace algorithms {
35 
36 namespace {
37 
38 inline double min4(double a, double b, double c, double d) {
39  return std::min(std::min(a, b), std::min(c, d));
40 }
41 
42 inline double max4(double a, double b, double c, double d) {
43  return std::max(std::max(a, b), std::max(c, d));
44 }
45 
46 // TODO: make this routine externally callable and more generic using templates
47 // (also useful in e.g. math/offsetImage.cc)
48 PTR(afw::detection::Psf::Image) zeroPadImage(afw::detection::Psf::Image const &im, int xPad, int yPad) {
49  int nx = im.getWidth();
50  int ny = im.getHeight();
51 
53  out = std::make_shared<afw::detection::Psf::Image>(nx + 2 * xPad, ny + 2 * yPad);
54  out->setXY0(im.getX0() - xPad, im.getY0() - yPad);
55 
56  geom::Box2I box(geom::Point2I(xPad, yPad), geom::Extent2I(nx, ny));
57  out->assign(im, box, afw::image::LOCAL);
58 
59  return out;
60 }
61 
62 geom::Box2I computeBBoxFromTransform(geom::Box2I const bbox, geom::AffineTransform const &t) {
63  static const int dst_padding = 0;
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(pex::exceptions::RangeError, "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  geom::Point2D c00 = t(geom::Point2D(in_xlo, in_ylo));
83  geom::Point2D c01 = t(geom::Point2D(in_xlo, in_yhi));
84  geom::Point2D c10 = t(geom::Point2D(in_xhi, in_ylo));
85  geom::Point2D c11 = t(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  geom::Box2I ret = geom::Box2I(geom::Point2I(out_xlo, out_ylo),
96  geom::Extent2I(out_xhi - out_xlo + 1, out_yhi - out_ylo + 1));
97  return ret;
98 }
99 
114 warpAffine(afw::detection::Psf::Image const &im, geom::AffineTransform const &srcToDest,
115  afw::math::WarpingControl const &wc) {
117  afw::geom::makeTransform(srcToDest);
118 
119  afw::math::SeparableKernel const &kernel = *wc.getWarpingKernel();
120  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  geom::Box2I bbox = computeBBoxFromTransform(im.getBBox(), srcToDest);
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, *srcToDestTransform, wc, 0.0);
133  return ret;
134 }
135 
136 } // namespace
137 
139  PTR(afw::geom::TransformPoint2ToPoint2 const) distortion,
141  : ImagePsf(false),
142  _undistortedPsf(undistortedPsf),
143  _distortion(distortion),
144  _warpingControl(control) {
145  _init();
146 }
147 
149  PTR(afw::geom::TransformPoint2ToPoint2 const) distortion, std::string const &kernelName,
150  unsigned int cache)
151  : ImagePsf(false),
152  _undistortedPsf(undistortedPsf),
153  _distortion(distortion),
154  _warpingControl(new afw::math::WarpingControl(kernelName, "", cache)) {
155  _init();
156 }
157 
158 void WarpedPsf::_init() {
159  if (!_undistortedPsf) {
161  "Undistorted Psf passed to WarpedPsf must not be None/NULL");
162  }
163  if (!_distortion) {
164  throw LSST_EXCEPT(pex::exceptions::LogicError, "Transform passed to WarpedPsf must not be None/NULL");
165  }
166  if (!_warpingControl) {
168  "WarpingControl passed to WarpedPsf must not be None/NULL");
169  }
170 }
171 
173  return _distortion->applyForward(_undistortedPsf->getAveragePosition());
174 }
175 
177  return std::make_shared<WarpedPsf>(_undistortedPsf->clone(), _distortion, _warpingControl);
178 }
179 
180 PTR(afw::detection::Psf) WarpedPsf::resized(int width, int height) const {
181  // For a given set of requested dimensions and distortion, it is not guaranteed that a
182  // _undistortedPsf would exist to manifest those dimensions after distortion
183  // Not possible to implement with member data currently in WarpedPsf
184  throw LSST_EXCEPT(pex::exceptions::LogicError, "Not Implemented");
185 }
186 
188 WarpedPsf::doComputeKernelImage(geom::Point2D const &position, afw::image::Color const &color) const {
189  geom::AffineTransform t = afw::geom::linearizeTransform(*_distortion->inverted(), position);
190  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  auto srcToDest = geom::AffineTransform(t.inverted().getLinear());
196  PTR(afw::detection::Psf::Psf::Image) ret = warpAffine(*im, srcToDest, *_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 geom::Box2I WarpedPsf::doComputeBBox(geom::Point2D const &position, afw::image::Color const &color) const {
219  geom::Point2D tp = t(position);
220  geom::Box2I bboxUndistorted = _undistortedPsf->computeBBox(tp, color);
221  geom::Box2I ret =
222  computeBBoxFromTransform(bboxUndistorted, geom::AffineTransform(t.inverted().getLinear()));
223  return ret;
224 }
225 
226 } // namespace algorithms
227 } // namespace meas
228 } // namespace lsst
y
int y
Definition: SpanSet.cc:49
lsst::afw::image
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
Definition: imageAlgorithm.dox:1
lsst::afw::math::WarpingControl
Parameters to control convolution.
Definition: warpExposure.h:250
lsst::afw::image::LOCAL
@ LOCAL
Definition: ImageBase.h:94
std::string
STL class.
std::shared_ptr
STL class.
lsst::afw::geom::Transform
Transform LSST spatial data, such as lsst::geom::Point2D and lsst::geom::SpherePoint,...
Definition: Transform.h:68
lsst::geom::Box2I::getHeight
int getHeight() const noexcept
Definition: Box.h:188
AffineTransform.h
lsst::afw
Definition: imageAlgorithm.dox:1
exceptions.h
lsst::afw::geom::linearizeTransform
lsst::geom::AffineTransform linearizeTransform(TransformPoint2ToPoint2 const &original, lsst::geom::Point2D const &inPoint)
Approximate a Transform by its local linearization.
Definition: transformFactory.cc:132
lsst::geom::AffineTransform::inverted
AffineTransform const inverted() const
Return the inverse transform.
Definition: AffineTransform.cc:53
lsst::meas::algorithms::WarpedPsf::getAveragePosition
virtual geom::Point2D getAveragePosition() const
Return the average of the positions of the stars that went into this Psf.
Definition: WarpedPsf.cc:172
lsst::meas::algorithms::WarpedPsf::clone
virtual boost::shared_ptr< afw::detection::Psf > clone() const
Polymorphic deep copy. Usually unnecessary, as Psfs are immutable.
Definition: WarpedPsf.cc:176
lsst::geom::Point2D
Point< double, 2 > Point2D
Definition: Point.h:324
Image.h
lsst::geom::AffineTransform
An affine coordinate transformation consisting of a linear transformation and an offset.
Definition: AffineTransform.h:75
lsst::afw::detection::Psf::Image
image::Image< Pixel > Image
Image type returned by computeImage.
Definition: Psf.h:83
lsst::meas::algorithms::WarpedPsf::_undistortedPsf
boost::shared_ptr< afw::detection::Psf const > _undistortedPsf
Definition: WarpedPsf.h:83
lsst::geom::AffineTransform::getLinear
LinearTransform const & getLinear() const noexcept
Definition: AffineTransform.h:155
lsst.pex::exceptions::LogicError
Reports errors in the logical structure of the program.
Definition: Runtime.h:46
PTR
#define PTR(...)
Definition: base.h:41
lsst::meas::algorithms::WarpedPsf
A Psf class that maps an arbitrary Psf through a coordinate transformation.
Definition: WarpedPsf.h:50
lsst::geom::LinearTransform::getMatrix
Matrix const & getMatrix() const noexcept
Definition: LinearTransform.h:151
lsst::afw::image::ImageBase< Pixel >::x_iterator
_view_t::x_iterator x_iterator
An iterator for traversing the pixels in a row.
Definition: ImageBase.h:133
b
table::Key< int > b
Definition: TransmissionCurve.cc:467
lsst::meas::algorithms::WarpedPsf::WarpedPsf
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:138
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
LSST_EXCEPT
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
lsst::afw::math::warpImage
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()))
Warp an Image or MaskedImage to a new Wcs.
Definition: warpExposure.cc:283
std::min
T min(T... args)
lsst::geom
Definition: AffineTransform.h:36
lsst.pex::exceptions::InvalidParameterError
Reports invalid arguments.
Definition: Runtime.h:66
a
table::Key< int > a
Definition: TransmissionCurve.cc:466
lsst::meas::algorithms::WarpedPsf::resized
virtual boost::shared_ptr< afw::detection::Psf > resized(int width, int height) const
Return a clone with specified kernel dimensions.
Definition: WarpedPsf.cc:180
lsst::geom::Point< int, 2 >
lsst::geom::Box2I
An integer coordinate rectangle.
Definition: Box.h:55
lsst::afw::detection::Psf
A polymorphic base class for representing an image's Point Spread Function.
Definition: Psf.h:76
std::max
T max(T... args)
lsst::afw::image::Image< Pixel >
lsst::geom::floor
Extent< int, N > floor(Extent< double, N > const &input) noexcept
Return the component-wise floor (round towards more negative).
Definition: Extent.cc:109
lsst::geom::ceil
Extent< int, N > ceil(Extent< double, N > const &input) noexcept
Return the component-wise ceil (round towards more positive).
Definition: Extent.cc:118
lsst::afw::geom::makeTransform
std::shared_ptr< TransformPoint2ToPoint2 > makeTransform(lsst::geom::AffineTransform const &affine)
Wrap an lsst::geom::AffineTransform as a Transform.
Definition: transformFactory.cc:154
lsst::geom::Extent< int, 2 >
Box.h
CONST_PTR
#define CONST_PTR(...)
A shared pointer to a const object.
Definition: base.h:47
warpExposure.h
bbox
AmpInfoBoxKey bbox
Definition: Amplifier.cc:117
lsst::afw::image::Color
Describe the colour of a source.
Definition: Color.h:26
lsst::meas::algorithms::ImagePsf
An intermediate base class for Psfs that use an image representation.
Definition: ImagePsf.h:40
lsst::meas::algorithms::WarpedPsf::_distortion
boost::shared_ptr< afw::geom::TransformPoint2ToPoint2 const > _distortion
Definition: WarpedPsf.h:84
WarpedPsf.h