LSSTApplications  16.0-10-g0ee56ad+5,16.0-11-ga33d1f2+5,16.0-12-g3ef5c14+3,16.0-12-g71e5ef5+18,16.0-12-gbdf3636+3,16.0-13-g118c103+3,16.0-13-g8f68b0a+3,16.0-15-gbf5c1cb+4,16.0-16-gfd17674+3,16.0-17-g7c01f5c+3,16.0-18-g0a50484+1,16.0-20-ga20f992+8,16.0-21-g0e05fd4+6,16.0-21-g15e2d33+4,16.0-22-g62d8060+4,16.0-22-g847a80f+4,16.0-25-gf00d9b8+1,16.0-28-g3990c221+4,16.0-3-gf928089+3,16.0-32-g88a4f23+5,16.0-34-gd7987ad+3,16.0-37-gc7333cb+2,16.0-4-g10fc685+2,16.0-4-g18f3627+26,16.0-4-g5f3a788+26,16.0-5-gaf5c3d7+4,16.0-5-gcc1f4bb+1,16.0-6-g3b92700+4,16.0-6-g4412fcd+3,16.0-6-g7235603+4,16.0-69-g2562ce1b+2,16.0-8-g14ebd58+4,16.0-8-g2df868b+1,16.0-8-g4cec79c+6,16.0-8-gadf6c7a+1,16.0-8-gfc7ad86,16.0-82-g59ec2a54a+1,16.0-9-g5400cdc+2,16.0-9-ge6233d7+5,master-g2880f2d8cf+3,v17.0.rc1
LSSTDataManagementBasePackage
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 
34 namespace afwImage = lsst::afw::image;
35 
36 namespace lsst {
37 namespace afw {
38 namespace math {
39 
40 template <typename ImageT>
41 std::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 
47  std::shared_ptr<ImageT> buffImage;
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->setCtrX(offsetKernel->getCtrX() + 1);
98  }
99  if (dKerY < 0) {
100  offsetKernel->setCtrY(offsetKernel->getCtrY() + 1);
101  }
102 
103  offsetKernel->setKernelParameters(std::make_pair(dKerX, dKerY));
104 
105  convolve(*convImage, *buffImage, *offsetKernel, true, true);
106 
107  std::shared_ptr<ImageT> outImage;
108  if (buffer > 0) {
109  lsst::geom::Box2I box(lsst::geom::Point2I(buffer, buffer), inImage.getDimensions());
110  std::shared_ptr<ImageT> out(new ImageT(*convImage, box, afwImage::LOCAL, true));
111  outImage = out;
112  } else {
113  outImage = convImage;
114  }
115 
116  // adjust the origin; do this after convolution since convolution also sets XY0
117  outImage->setXY0(lsst::geom::Point2I(inImage.getX0() + dOrigX, inImage.getY0() + dOrigY));
118 
119  return outImage;
120 }
121 
122 //
123 // Explicit instantiations
124 //
126 #define INSTANTIATE(TYPE) \
127  template std::shared_ptr<afwImage::Image<TYPE>> offsetImage(afwImage::Image<TYPE> const&, float, float, \
128  std::string const&, unsigned int); \
129  template std::shared_ptr<afwImage::MaskedImage<TYPE>> offsetImage( \
130  afwImage::MaskedImage<TYPE> const&, float, float, std::string const&, unsigned int);
131 
132 INSTANTIATE(double)
133 INSTANTIATE(float)
134 INSTANTIATE(int)
136 } // namespace math
137 } // namespace afw
138 } // namespace lsst
Reports attempts to exceed implementation-defined length limits for some classes. ...
Definition: Runtime.h:76
T floor(T... args)
std::shared_ptr< SeparableKernel > makeWarpingKernel(std::string name)
Return a warping kernel given its name.
STL class.
A base class for image defects.
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:168
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...
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
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.
Definition: offsetImage.cc:41
afw::table::Key< afw::table::Array< ImagePixelT > > image
An integer coordinate rectangle.
Definition: Box.h:54
#define INSTANTIATE(FROMSYS, TOSYS)
Definition: Detector.cc:312