25 #include "lsst/pex/exceptions.h" 27 #include "lsst/afw/math/warpExposure.h" 28 #include "lsst/afw/image/Image.h" 30 namespace lsst {
namespace meas {
namespace algorithms {
34 inline double min4(
double a,
double b,
double c,
double d) {
35 return std::min(std::min(a,b), std::min(c,d));
38 inline double max4(
double a,
double b,
double c,
double d) {
39 return std::max(std::max(a,b), std::max(c,d));
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();
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);
52 out->assign(im, box, afw::image::LOCAL);
57 afw::geom::Box2I computeBBoxFromTransform
58 (afw::geom::Box2I
const bbox,
59 afw::geom::AffineTransform
const &t
61 static const int dst_padding = 0;
63 afw::geom::AffineXYTransform xyTransform(t);
69 static const double maxTransformCoeff = 200.0;
71 if (t.getLinear().getMatrix().lpNorm<Eigen::Infinity>() > maxTransformCoeff) {
73 pex::exceptions::RangeError,
74 "Unexpectedly large transform passed to WarpedPsf");
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;
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));
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;
98 afw::geom::Extent2I(out_xhi - out_xlo + 1, out_yhi-out_ylo + 1));
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
117 afw::geom::AffineXYTransform xyTransform(t);
119 afw::math::SeparableKernel
const& kernel = *wc.getWarpingKernel();
121 int const xPad = std::max(center.getX(), kernel.getWidth() - center.getX());
122 int const yPad = std::max(center.getY(), kernel.getHeight() - center.getY());
125 afw::geom::Box2I bbox = computeBBoxFromTransform(im.getBBox(), t);
126 PTR(afw::detection::Psf::Image) ret = std::make_shared<afw::detection::Psf::Image>(bbox);
129 PTR(afw::detection::Psf::Image) im_padded = zeroPadImage(im, xPad, yPad);
132 afw::math::warpImage(*ret, *im_padded, xyTransform, wc, 0.0);
139 PTR(afw::detection::Psf
const) undistortedPsf,
140 PTR(afw::geom::XYTransform
const) distortion,
141 CONST_PTR(afw::math::WarpingControl) control
144 _undistortedPsf(undistortedPsf),
145 _distortion(distortion),
146 _warpingControl(control)
152 PTR(afw::detection::Psf
const) undistortedPsf,
153 PTR(afw::geom::XYTransform
const) distortion,
154 std::string
const& kernelName,
160 _warpingControl(new afw::math::WarpingControl(kernelName,
"", cache))
165 void WarpedPsf::_init()
169 pex::exceptions::LogicError,
170 "Undistorted Psf passed to WarpedPsf must not be None/NULL" 175 pex::exceptions::LogicError,
176 "XYTransform passed to WarpedPsf must not be None/NULL" 179 if (!_warpingControl) {
181 pex::exceptions::LogicError,
182 "WarpingControl passed to WarpedPsf must not be None/NULL" 199 throw LSST_EXCEPT(pex::exceptions::LogicError,
"Not Implemented");
203 afw::geom::Point2D
const & position, afw::image::Color
const & color
205 afw::geom::AffineTransform t =
_distortion->linearizeReverseTransform(position);
206 afw::geom::Point2D tp = t(position);
211 PTR(afw::detection::Psf::Psf::Image) ret
212 = warpAffine(*im, afw::geom::AffineTransform(t.invert().getLinear()), *_warpingControl);
214 double normFactor = 1.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;
226 if (normFactor == 0.0) {
227 throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
"psf image has sum 0");
233 afw::geom::Box2I WarpedPsf::doComputeBBox(
234 afw::geom::Point2D
const & position, afw::image::Color
const & color
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()));
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.
virtual afw::geom::Point2D getAveragePosition() const
Return the average of the positions of the stars that went into this Psf.
virtual boost::shared_ptr< afw::detection::Psf::Image > doComputeKernelImage(afw::geom::Point2D const &position, afw::image::Color const &color) const
virtual boost::shared_ptr< afw::detection::Psf > resized(int width, int height) const
Return a clone with specified kernel dimensions.
virtual boost::shared_ptr< afw::detection::Psf > clone() const
Polymorphic deep copy. Usually unnecessary, as Psfs are immutable.
afw::table::Key< double > b
An intermediate base class for Psfs that use an image representation.
boost::shared_ptr< afw::detection::Psf const > _undistortedPsf
boost::shared_ptr< afw::geom::XYTransform const > _distortion