lsst.afw  14.0-59-g0c7cc230b
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>
30 #include "lsst/afw/geom/Box.h"
31 #include "lsst/afw/geom/Extent.h"
34 
35 namespace afwImage = lsst::afw::image;
36 namespace afwGeom = lsst::afw::geom;
37 
38 namespace lsst {
39 namespace afw {
40 namespace math {
41 
42 template <typename ImageT>
43 std::shared_ptr<ImageT> offsetImage(ImageT const& inImage, float dx, float dy,
44  std::string const& algorithmName, unsigned int buffer
45 
46  ) {
47  std::shared_ptr<SeparableKernel> offsetKernel = makeWarpingKernel(algorithmName);
48 
49  std::shared_ptr<ImageT> buffImage;
50  if (buffer > 0) {
51  // Paste input image into buffered image
52  afwGeom::Extent2I const& dims = inImage.getDimensions();
53  std::shared_ptr<ImageT> buffered(new ImageT(dims.getX() + 2 * buffer, dims.getY() + 2 * buffer));
54  buffImage = buffered;
55  afwGeom::Box2I box(afwGeom::Point2I(buffer, buffer), dims);
56  buffImage->assign(inImage, box);
57  } else {
58  buffImage = std::make_shared<ImageT>(inImage);
59  }
60 
61  if (offsetKernel->getWidth() > buffImage->getWidth() ||
62  offsetKernel->getHeight() > buffImage->getHeight()) {
64  (boost::format("Image of size %dx%d is too small to offset using a %s kernel"
65  "(minimum %dx%d)") %
66  buffImage->getWidth() % buffImage->getHeight() % algorithmName %
67  offsetKernel->getWidth() % offsetKernel->getHeight())
68  .str());
69  }
70 
71  // std::shared_ptr<ImageT> convImage(new ImageT(buffImage, true)); // output image, a deep copy
72  std::shared_ptr<ImageT> convImage(new ImageT(buffImage->getDimensions())); // Convolved image
73 
74  int dOrigX, dOrigY;
75  double fracX, fracY;
76  // If the offset in both axes is in (-1, 1) use it as is, and don't shift the origin
77  if (dx > -1 && dx < 1 && dy > -1 && dy < 1) {
78  dOrigX = 0;
79  dOrigY = 0;
80  fracX = dx;
81  fracY = dy;
82  } else {
83  dOrigX = static_cast<int>(std::floor(dx + 0.5));
84  dOrigY = static_cast<int>(std::floor(dy + 0.5));
85  fracX = dx - dOrigX;
86  fracY = dy - dOrigY;
87  }
88 
89  // We seem to have to pass -fracX, -fracY to setKernelParameters, for reasons RHL doesn't understand
90  double dKerX = -fracX;
91  double dKerY = -fracY;
92 
93  //
94  // If the shift is -ve, the generated shift kernel (e.g. Lanczos5) is quite asymmetric, with the
95  // largest coefficients to the left of centre. We therefore move the centre of calculated shift kernel
96  // one to the right to center up the largest coefficients
97  //
98  if (dKerX < 0) {
99  offsetKernel->setCtrX(offsetKernel->getCtrX() + 1);
100  }
101  if (dKerY < 0) {
102  offsetKernel->setCtrY(offsetKernel->getCtrY() + 1);
103  }
104 
105  offsetKernel->setKernelParameters(std::make_pair(dKerX, dKerY));
106 
107  convolve(*convImage, *buffImage, *offsetKernel, true, true);
108 
109  std::shared_ptr<ImageT> outImage;
110  if (buffer > 0) {
111  afwGeom::Box2I box(afwGeom::Point2I(buffer, buffer), inImage.getDimensions());
112  std::shared_ptr<ImageT> out(new ImageT(*convImage, box, afwImage::LOCAL, true));
113  outImage = out;
114  } else {
115  outImage = convImage;
116  }
117 
118  // adjust the origin; do this after convolution since convolution also sets XY0
119  outImage->setXY0(geom::Point2I(inImage.getX0() + dOrigX, inImage.getY0() + dOrigY));
120 
121  return outImage;
122 }
123 
124 //
125 // Explicit instantiations
126 //
128 #define INSTANTIATE(TYPE) \
129  template std::shared_ptr<afwImage::Image<TYPE>> offsetImage(afwImage::Image<TYPE> const&, float, float, \
130  std::string const&, unsigned int); \
131  template std::shared_ptr<afwImage::MaskedImage<TYPE>> offsetImage( \
132  afwImage::MaskedImage<TYPE> const&, float, float, std::string const&, unsigned int);
133 
134 INSTANTIATE(double)
135 INSTANTIATE(float)
136 INSTANTIATE(int)
138 }
139 }
140 }
afw::table::Key< afw::table::Array< ImagePixelT > > image
T floor(T... args)
std::shared_ptr< SeparableKernel > makeWarpingKernel(std::string name)
Return a warping kernel given its name.
An integer coordinate rectangle.
Definition: Box.h:55
STL class.
A base class for image defects.
#define INSTANTIATE(FROMSYS, TOSYS)
Definition: Detector.cc:156
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,...)
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:43