LSST Applications  21.0.0-172-gfb10e10a+18fedfabac,22.0.0+297cba6710,22.0.0+80564b0ff1,22.0.0+8d77f4f51a,22.0.0+a28f4c53b1,22.0.0+dcf3732eb2,22.0.1-1-g7d6de66+2a20fdde0d,22.0.1-1-g8e32f31+297cba6710,22.0.1-1-geca5380+7fa3b7d9b6,22.0.1-12-g44dc1dc+2a20fdde0d,22.0.1-15-g6a90155+515f58c32b,22.0.1-16-g9282f48+790f5f2caa,22.0.1-2-g92698f7+dcf3732eb2,22.0.1-2-ga9b0f51+7fa3b7d9b6,22.0.1-2-gd1925c9+bf4f0e694f,22.0.1-24-g1ad7a390+a9625a72a8,22.0.1-25-g5bf6245+3ad8ecd50b,22.0.1-25-gb120d7b+8b5510f75f,22.0.1-27-g97737f7+2a20fdde0d,22.0.1-32-gf62ce7b1+aa4237961e,22.0.1-4-g0b3f228+2a20fdde0d,22.0.1-4-g243d05b+871c1b8305,22.0.1-4-g3a563be+32dcf1063f,22.0.1-4-g44f2e3d+9e4ab0f4fa,22.0.1-42-gca6935d93+ba5e5ca3eb,22.0.1-5-g15c806e+85460ae5f3,22.0.1-5-g58711c4+611d128589,22.0.1-5-g75bb458+99c117b92f,22.0.1-6-g1c63a23+7fa3b7d9b6,22.0.1-6-g50866e6+84ff5a128b,22.0.1-6-g8d3140d+720564cf76,22.0.1-6-gd805d02+cc5644f571,22.0.1-8-ge5750ce+85460ae5f3,master-g6e05de7fdc+babf819c66,master-g99da0e417a+8d77f4f51a,w.2021.48
LSST Data Management Base Package
ConvolveWithInterpolation.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  * Definition of convolveWithInterpolation and helper functions declared in detail/ConvolveImage.h
27  */
28 #include <sstream>
29 
30 #include "lsst/pex/exceptions.h"
31 #include "lsst/log/Log.h"
32 #include "lsst/geom.h"
34 #include "lsst/afw/math/Kernel.h"
36 
38 
39 namespace lsst {
40 namespace afw {
41 namespace math {
42 namespace detail {
43 
44 template <typename OutImageT, typename InImageT>
45 void convolveWithInterpolation(OutImageT &outImage, InImageT const &inImage, math::Kernel const &kernel,
46  math::ConvolutionControl const &convolutionControl) {
47  if (outImage.getDimensions() != inImage.getDimensions()) {
49  os << "outImage dimensions = ( " << outImage.getWidth() << ", " << outImage.getHeight() << ") != ("
50  << inImage.getWidth() << ", " << inImage.getHeight() << ") = inImage dimensions";
52  }
53 
54  // compute region covering good area of output image
56  lsst::geom::Point2I(0, 0), lsst::geom::Extent2I(outImage.getWidth(), outImage.getHeight()));
57  lsst::geom::Box2I goodBBox = kernel.shrinkBBox(fullBBox);
58  KernelImagesForRegion goodRegion(KernelImagesForRegion(kernel.clone(), goodBBox, inImage.getXY0(),
59  convolutionControl.getDoNormalize()));
60  LOGL_DEBUG("TRACE5.lsst.afw.math.convolve.convolveWithInterpolation",
61  "convolveWithInterpolation: full bbox minimum=(%d, %d), extent=(%d, %d)", fullBBox.getMinX(),
62  fullBBox.getMinY(), fullBBox.getWidth(), fullBBox.getHeight());
63  LOGL_DEBUG("TRACE5.lsst.afw.math.convolve.convolveWithInterpolation",
64  "convolveWithInterpolation: goodRegion bbox minimum=(%d, %d), extent=(%d, %d)",
65  goodRegion.getBBox().getMinX(), goodRegion.getBBox().getMinY(),
66  goodRegion.getBBox().getWidth(), goodRegion.getBBox().getHeight());
67 
68  // divide good region into subregions small enough to interpolate over
69  int nx = 1 + (goodBBox.getWidth() / convolutionControl.getMaxInterpolationDistance());
70  int ny = 1 + (goodBBox.getHeight() / convolutionControl.getMaxInterpolationDistance());
71  LOGL_DEBUG("TRACE3.lsst.afw.math.convolve.convolveWithInterpolation",
72  "convolveWithInterpolation: divide into %d x %d subregions", nx, ny);
73 
75  RowOfKernelImagesForRegion regionRow(nx, ny);
76  while (goodRegion.computeNextRow(regionRow)) {
77  for (auto const &rgnIter : regionRow) {
78  LOGL_DEBUG("TRACE5.lsst.afw.math.convolve.convolveWithInterpolation",
79  "convolveWithInterpolation: bbox minimum=(%d, %d), extent=(%d, %d)",
80  rgnIter->getBBox().getMinX(), rgnIter->getBBox().getMinY(),
81  rgnIter->getBBox().getWidth(), rgnIter->getBBox().getHeight());
82  convolveRegionWithInterpolation(outImage, inImage, *rgnIter, workingImages);
83  }
84  }
85 }
86 
87 template <typename OutImageT, typename InImageT>
88 void convolveRegionWithInterpolation(OutImageT &outImage, InImageT const &inImage,
89  KernelImagesForRegion const &region,
91  using OutLocator = typename OutImageT::xy_locator;
92  using InConstLocator = typename InImageT::const_xy_locator;
93  using KernelImage = KernelImagesForRegion::Image;
94  using KernelConstLocator = KernelImage::const_xy_locator;
95 
96  std::shared_ptr<Kernel const> kernelPtr = region.getKernel();
97  lsst::geom::Extent2I const kernelDimensions(kernelPtr->getDimensions());
100  workingImages.kernelImage.assign(workingImages.leftImage);
101 
102  lsst::geom::Box2I const goodBBox = region.getBBox();
103  lsst::geom::Box2I const fullBBox = kernelPtr->growBBox(goodBBox);
104 
105  // top and right images are computed one beyond bbox boundary,
106  // so the distance between edge images is bbox width/height pixels
107  double xfrac = 1.0 / static_cast<double>(goodBBox.getWidth());
108  double yfrac = 1.0 / static_cast<double>(goodBBox.getHeight());
110  -yfrac, workingImages.leftImage);
112  -yfrac, workingImages.rightImage);
113 
114  KernelConstLocator const kernelLocator = workingImages.kernelImage.xy_at(0, 0);
115 
116  // The loop is a bit odd for efficiency: the initial value of workingImages.kernelImage
117  // and related kernel images are set when they are allocated,
118  // so they are not computed in the loop until after the convolution; to save cpu cycles
119  // they are not computed at all for the last iteration.
120  InConstLocator inLocator = inImage.xy_at(fullBBox.getMinX(), fullBBox.getMinY());
121  OutLocator outLocator = outImage.xy_at(goodBBox.getMinX(), goodBBox.getMinY());
122  for (int j = 0;;) {
123  auto inLocatorInitialPosition = inLocator;
124  auto outLocatorInitialPosition = outLocator;
125  math::scaledPlus(workingImages.deltaImage, xfrac, workingImages.rightImage, -xfrac,
126  workingImages.leftImage);
127  for (int i = 0;;) {
128  *outLocator = math::convolveAtAPoint<OutImageT, InImageT>(
129  inLocator, kernelLocator, kernelDimensions.getX(), kernelDimensions.getY());
130  ++outLocator.x();
131  ++inLocator.x();
132  ++i;
133  if (i >= goodBBox.getWidth()) {
134  break;
135  }
136  workingImages.kernelImage += workingImages.deltaImage;
137  }
138 
139  ++j;
140  if (j >= goodBBox.getHeight()) {
141  break;
142  }
143  workingImages.leftImage += workingImages.leftDeltaImage;
144  workingImages.rightImage += workingImages.rightDeltaImage;
145  workingImages.kernelImage.assign(workingImages.leftImage);
146 
147  // Workaround for DM-5822
148  // Boost GIL locator won't decrement in x-dimension for some strange and still
149  // not understood reason. So instead store position at start of previous row,
150  // reset and move down.
151  inLocator = inLocatorInitialPosition;
152  outLocator = outLocatorInitialPosition;
153  ++inLocator.y();
154  ++outLocator.y();
155  }
156 }
157 
158 /*
159  * Explicit instantiation
160  */
162 #define IMAGE(PIXTYPE) image::Image<PIXTYPE>
163 #define MASKEDIMAGE(PIXTYPE) image::MaskedImage<PIXTYPE, image::MaskPixel, image::VariancePixel>
164 #define NL /* */
165 // Instantiate Image or MaskedImage versions
166 #define INSTANTIATE_IM_OR_MI(IMGMACRO, OUTPIXTYPE, INPIXTYPE) \
167  template void convolveWithInterpolation(IMGMACRO(OUTPIXTYPE) &, IMGMACRO(INPIXTYPE) const &, \
168  math::Kernel const &, math::ConvolutionControl const &); \
169  NL template void convolveRegionWithInterpolation(IMGMACRO(OUTPIXTYPE) &, IMGMACRO(INPIXTYPE) const &, \
170  KernelImagesForRegion const &, \
171  ConvolveWithInterpolationWorkingImages &);
172 // Instantiate both Image and MaskedImage versions
173 #define INSTANTIATE(OUTPIXTYPE, INPIXTYPE) \
174  INSTANTIATE_IM_OR_MI(IMAGE, OUTPIXTYPE, INPIXTYPE) \
175  INSTANTIATE_IM_OR_MI(MASKEDIMAGE, OUTPIXTYPE, INPIXTYPE)
176 
177 INSTANTIATE(double, double)
178 INSTANTIATE(double, float)
179 INSTANTIATE(double, int)
180 INSTANTIATE(double, std::uint16_t)
181 INSTANTIATE(float, float)
182 INSTANTIATE(float, int)
184 INSTANTIATE(int, int)
187 } // namespace detail
188 } // namespace math
189 } // namespace afw
190 } // namespace lsst
#define INSTANTIATE(FROMSYS, TOSYS)
Definition: Detector.cc:484
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
LSST DM logging module built on log4cxx.
#define LOGL_DEBUG(logger, message...)
Log a debug-level message using a varargs/printf style interface.
Definition: Log.h:515
std::ostream * os
Definition: Schema.cc:557
xy_locator xy_at(int x, int y) const
Return an xy_locator at the point (x, y) in the image.
Definition: ImageBase.h:425
void assign(ImageBase const &rhs, lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT)
Copy pixels from another image to a specified subregion of this image.
Definition: Image.cc:153
Parameters to control convolution.
Definition: ConvolveImage.h:50
Kernels are used for convolution with MaskedImages and (eventually) Images.
Definition: Kernel.h:110
lsst::geom::Extent2I const getDimensions() const
Return the Kernel's dimensions (width, height)
Definition: Kernel.h:212
virtual std::shared_ptr< Kernel > clone() const =0
Return a pointer to a deep copy of this kernel.
lsst::geom::Box2I shrinkBBox(lsst::geom::Box2I const &bbox) const
Given a bounding box for an image one wishes to convolve with this kernel, return the bounding box fo...
Definition: Kernel.cc:173
A collection of Kernel images for special locations on a rectangular region of an image.
Definition: Convolve.h:172
KernelConstPtr getKernel() const
Get the kernel (as a shared pointer to const)
Definition: Convolve.h:254
bool computeNextRow(RowOfKernelImagesForRegion &regionRow) const
Compute next row of subregions.
ImagePtr getImage(Location location) const
Return the image and sum at the specified location.
lsst::geom::Box2I getBBox() const
Get the bounding box for the region.
Definition: Convolve.h:234
lsst::afw::image::Image< lsst::afw::math::Kernel::Pixel > Image
Definition: Convolve.h:175
An integer coordinate rectangle.
Definition: Box.h:55
int getMinY() const noexcept
Definition: Box.h:158
int getHeight() const noexcept
Definition: Box.h:188
int getMinX() const noexcept
Definition: Box.h:157
int getWidth() const noexcept
Definition: Box.h:187
Reports invalid arguments.
Definition: Runtime.h:66
void convolveRegionWithInterpolation(OutImageT &outImage, InImageT const &inImage, KernelImagesForRegion const &region, ConvolveWithInterpolationWorkingImages &workingImages)
Convolve a region of an Image or MaskedImage with a spatially varying Kernel using interpolation.
void convolveWithInterpolation(OutImageT &outImage, InImageT const &inImage, lsst::afw::math::Kernel const &kernel, ConvolutionControl const &convolutionControl)
Convolve an Image or MaskedImage with a spatially varying Kernel using linear interpolation.
void scaledPlus(OutImageT &outImage, double c1, InImageT const &inImage1, double c2, InImageT const &inImage2)
Compute the scaled sum of two images.
A base class for image defects.
kernel images used by convolveRegionWithInterpolation
Definition: Convolve.h:418