LSST Applications g02d81e74bb+86cf3d8bc9,g180d380827+7a4e862ed4,g2079a07aa2+86d27d4dc4,g2305ad1205+e1ca1c66fa,g29320951ab+012e1474a1,g295015adf3+341ea1ce94,g2bbee38e9b+0e5473021a,g337abbeb29+0e5473021a,g33d1c0ed96+0e5473021a,g3a166c0a6a+0e5473021a,g3ddfee87b4+c429d67c83,g48712c4677+f88676dd22,g487adcacf7+27e1e21933,g50ff169b8f+96c6868917,g52b1c1532d+585e252eca,g591dd9f2cf+b41db86c35,g5a732f18d5+53520f316c,g64a986408d+86cf3d8bc9,g858d7b2824+86cf3d8bc9,g8a8a8dda67+585e252eca,g99cad8db69+84912a7fdc,g9ddcbc5298+9a081db1e4,ga1e77700b3+15fc3df1f7,ga8c6da7877+a2b54eae19,gb0e22166c9+60f28cb32d,gba4ed39666+c2a2e4ac27,gbb8dafda3b+6681f309db,gc120e1dc64+f0fcc2f6d8,gc28159a63d+0e5473021a,gcf0d15dbbd+c429d67c83,gdaeeff99f8+f9a426f77a,ge6526c86ff+0433e6603d,ge79ae78c31+0e5473021a,gee10cc3b42+585e252eca,gff1a9f87cc+86cf3d8bc9,w.2024.17
LSST Data Management Base Package
Loading...
Searching...
No Matches
offsetImage.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/*
26 * Offset an Image (or Mask or MaskedImage) by a constant vector (dx, dy)
27 */
28#include <iterator>
29#include "lsst/geom.h"
33
34namespace afwImage = lsst::afw::image;
35
36namespace lsst {
37namespace afw {
38namespace math {
39
40template <typename ImageT>
41std::shared_ptr<ImageT> offsetImage(ImageT const& inImage, float dx, float dy,
42 std::string const& algorithmName, unsigned int buffer
43
44) {
45 std::shared_ptr<SeparableKernel> offsetKernel = makeWarpingKernel(algorithmName);
46
48 if (buffer > 0) {
49 // Paste input image into buffered image
50 lsst::geom::Extent2I const& dims = inImage.getDimensions();
51 std::shared_ptr<ImageT> buffered(new ImageT(dims.getX() + 2 * buffer, dims.getY() + 2 * buffer));
52 buffImage = buffered;
53 lsst::geom::Box2I box(lsst::geom::Point2I(buffer, buffer), dims);
54 buffImage->assign(inImage, box);
55 } else {
56 buffImage = std::make_shared<ImageT>(inImage);
57 }
58
59 if (offsetKernel->getWidth() > buffImage->getWidth() ||
60 offsetKernel->getHeight() > buffImage->getHeight()) {
62 (boost::format("Image of size %dx%d is too small to offset using a %s kernel"
63 "(minimum %dx%d)") %
64 buffImage->getWidth() % buffImage->getHeight() % algorithmName %
65 offsetKernel->getWidth() % offsetKernel->getHeight())
66 .str());
67 }
68
69 // std::shared_ptr<ImageT> convImage(new ImageT(buffImage, true)); // output image, a deep copy
70 std::shared_ptr<ImageT> convImage(new ImageT(buffImage->getDimensions())); // Convolved image
71
72 int dOrigX, dOrigY;
73 double fracX, fracY;
74 // If the offset in both axes is in (-1, 1) use it as is, and don't shift the origin
75 if (dx > -1 && dx < 1 && dy > -1 && dy < 1) {
76 dOrigX = 0;
77 dOrigY = 0;
78 fracX = dx;
79 fracY = dy;
80 } else {
81 dOrigX = static_cast<int>(std::floor(dx + 0.5));
82 dOrigY = static_cast<int>(std::floor(dy + 0.5));
83 fracX = dx - dOrigX;
84 fracY = dy - dOrigY;
85 }
86
87 // We seem to have to pass -fracX, -fracY to setKernelParameters, for reasons RHL doesn't understand
88 double dKerX = -fracX;
89 double dKerY = -fracY;
90
91 //
92 // If the shift is -ve, the generated shift kernel (e.g. Lanczos5) is quite asymmetric, with the
93 // largest coefficients to the left of centre. We therefore move the centre of calculated shift kernel
94 // one to the right to center up the largest coefficients
95 //
96 if (dKerX < 0) {
97 offsetKernel->setCtr(offsetKernel->getCtr() + lsst::geom::Extent2I(1, 0));
98 }
99 if (dKerY < 0) {
100 offsetKernel->setCtr(offsetKernel->getCtr() + lsst::geom::Extent2I(0, 1));
101 }
102
103 offsetKernel->setKernelParameters(std::make_pair(dKerX, dKerY));
104
105 ConvolutionControl convolutionControl;
106 convolutionControl.setDoNormalize(true);
107 convolutionControl.setDoCopyEdge(true);
108 convolve(*convImage, *buffImage, *offsetKernel, convolutionControl);
109
111 if (buffer > 0) {
112 lsst::geom::Box2I box(lsst::geom::Point2I(buffer, buffer), inImage.getDimensions());
113 std::shared_ptr<ImageT> out(new ImageT(*convImage, box, afwImage::LOCAL, true));
114 outImage = out;
115 } else {
116 outImage = convImage;
117 }
118
119 // adjust the origin; do this after convolution since convolution also sets XY0
120 outImage->setXY0(lsst::geom::Point2I(inImage.getX0() + dOrigX, inImage.getY0() + dOrigY));
121
122 return outImage;
123}
124
125//
126// Explicit instantiations
127//
129#define INSTANTIATE(TYPE) \
130 template std::shared_ptr<afwImage::Image<TYPE>> offsetImage(afwImage::Image<TYPE> const&, float, float, \
131 std::string const&, unsigned int); \
132 template std::shared_ptr<afwImage::MaskedImage<TYPE>> offsetImage( \
133 afwImage::MaskedImage<TYPE> const&, float, float, std::string const&, unsigned int);
134
135INSTANTIATE(double)
136INSTANTIATE(float)
137INSTANTIATE(int)
139} // namespace math
140} // namespace afw
141} // namespace lsst
#define INSTANTIATE(FROMSYS, TOSYS)
Definition Detector.cc:509
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition Exception.h:48
Parameters to control convolution.
void setDoCopyEdge(bool doCopyEdge)
void setDoNormalize(bool doNormalize)
An integer coordinate rectangle.
Definition Box.h:55
Reports attempts to exceed implementation-defined length limits for some classes.
Definition Runtime.h:76
T floor(T... args)
T make_pair(T... args)
void convolve(OutImageT &convolvedImage, InImageT const &inImage, KernelT const &kernel, ConvolutionControl const &convolutionControl=ConvolutionControl())
Convolve an Image or MaskedImage with a Kernel, setting pixels of an existing output image.
std::shared_ptr< SeparableKernel > makeWarpingKernel(std::string name)
Return a warping kernel given its name.
std::shared_ptr< ImageT > offsetImage(ImageT const &image, float dx, float dy, std::string const &algorithmName="lanczos5", unsigned int buffer=0)
Return an image offset by (dx, dy) using the specified algorithm.
Extent< int, 2 > Extent2I
Definition Extent.h:397