lsst.afw  14.0-25-g0e8fc950e+5
ConvolveImage.h
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 #ifndef LSST_AFW_MATH_CONVOLVEIMAGE_H
26 #define LSST_AFW_MATH_CONVOLVEIMAGE_H
27 /*
28  * Convolve and convolveAtAPoint functions for Image and Kernel
29  *
30  * @todo Consider adding a flag to convolve indicating which specialized version of basicConvolve was used.
31  * This would only be used for unit testing and trace messages suffice (barely), so not a high priority.
32  */
33 #include <limits>
34 #include <sstream>
35 
36 #include "lsst/pex/exceptions.h"
37 #include "lsst/afw/geom.h"
38 #include "lsst/afw/image/Image.h"
40 #include "lsst/afw/math/Kernel.h"
41 
42 namespace lsst {
43 namespace afw {
44 namespace math {
45 
52 public:
53  ConvolutionControl(bool doNormalize = true,
54  bool doCopyEdge = false,
55  int maxInterpolationDistance = 10
57  )
59  : _doNormalize(doNormalize),
60  _doCopyEdge(doCopyEdge),
61  _maxInterpolationDistance(maxInterpolationDistance) {}
62 
63  bool getDoNormalize() const { return _doNormalize; }
64  bool getDoCopyEdge() const { return _doCopyEdge; }
65  int getMaxInterpolationDistance() const { return _maxInterpolationDistance; };
66 
67  void setDoNormalize(bool doNormalize) { _doNormalize = doNormalize; }
68  void setDoCopyEdge(bool doCopyEdge) { _doCopyEdge = doCopyEdge; }
69  void setMaxInterpolationDistance(int maxInterpolationDistance) {
70  _maxInterpolationDistance = maxInterpolationDistance;
71  }
72 
73 private:
74  bool _doNormalize;
75  bool _doCopyEdge;
76  int _maxInterpolationDistance;
78 };
80 
103 template <typename OutImageT, typename InImageT>
104 void scaledPlus(OutImageT& outImage, double c1, InImageT const& inImage1, double c2,
105  InImageT const& inImage2);
106 
107 template <typename OutImageT, typename InImageT>
108 inline typename OutImageT::SinglePixel convolveAtAPoint(
109  typename InImageT::const_xy_locator inImageLocator,
111  int kWidth, int kHeight);
112 
113 template <typename OutImageT, typename InImageT>
114 inline typename OutImageT::SinglePixel convolveAtAPoint(
115  typename InImageT::const_xy_locator inImageLocator,
116  std::vector<lsst::afw::math::Kernel::Pixel> const& kernelColList,
117  std::vector<lsst::afw::math::Kernel::Pixel> const& kernelRowList);
118 
191 template <typename OutImageT, typename InImageT, typename KernelT>
192 void convolve(OutImageT& convolvedImage, InImageT const& inImage, KernelT const& kernel,
193  ConvolutionControl const& convolutionControl = ConvolutionControl());
194 
207 template <typename OutImageT, typename InImageT, typename KernelT>
208 void convolve(OutImageT& convolvedImage, InImageT const& inImage, KernelT const& kernel, bool doNormalize,
209  bool doCopyEdge = false);
210 
216 template <typename ImageT>
217 typename ImageT::SinglePixel edgePixel(lsst::afw::image::detail::Image_tag
219  ) {
220  typedef typename ImageT::SinglePixel SinglePixelT;
223  : 0);
224 }
225 
234 template <typename MaskedImageT>
235 typename MaskedImageT::SinglePixel edgePixel(
238  ) {
239  typedef typename MaskedImageT::Image::Pixel ImagePixelT;
240  typedef typename MaskedImageT::Variance::Pixel VariancePixelT;
241 
242  return typename MaskedImageT::SinglePixel(
244  : 0,
245  MaskedImageT::Mask::getPlaneBitMask("NO_DATA"), std::numeric_limits<VariancePixelT>::infinity());
246 }
247 
248 /*
249  * Define inline functions
250  */
251 
268 template <typename OutImageT, typename InImageT>
269 inline typename OutImageT::SinglePixel convolveAtAPoint(
270  typename InImageT::const_xy_locator inImageLocator,
271  image::Image<Kernel::Pixel>::const_xy_locator kernelLocator, int kWidth, int kHeight) {
272  typename OutImageT::SinglePixel outValue = 0;
273  for (int kRow = 0; kRow != kHeight; ++kRow) {
275  kEnd = kernelLocator + image::detail::difference_type(kWidth, 0);
276  kernelLocator != kEnd; ++inImageLocator.x(), ++kernelLocator.x()) {
277  typename Kernel::Pixel const kVal = kernelLocator[0];
278  if (kVal != 0) {
279  outValue += *inImageLocator * kVal;
280  }
281  }
282 
283  inImageLocator += image::detail::difference_type(-kWidth, 1);
284  kernelLocator += image::detail::difference_type(-kWidth, 1);
285  }
286 
287  return outValue;
288 }
289 
304 template <typename OutImageT, typename InImageT>
305 inline typename OutImageT::SinglePixel convolveAtAPoint(typename InImageT::const_xy_locator inImageLocator,
306  std::vector<Kernel::Pixel> const& kernelXList,
307  std::vector<Kernel::Pixel> const& kernelYList) {
308  typedef typename std::vector<Kernel::Pixel>::const_iterator k_iter;
309 
310  typedef typename OutImageT::SinglePixel OutT;
311  OutT outValue = 0;
312  for (k_iter kernelYIter = kernelYList.begin(), yEnd = kernelYList.end(); kernelYIter != yEnd;
313  ++kernelYIter) {
314  OutT outValueY = 0;
315  for (k_iter kernelXIter = kernelXList.begin(), xEnd = kernelXList.end(); kernelXIter != xEnd;
316  ++kernelXIter, ++inImageLocator.x()) {
317  typename Kernel::Pixel const kValX = *kernelXIter;
318  if (kValX != 0) {
319  outValueY += *inImageLocator * kValX;
320  }
321  }
322 
323  double const kValY = *kernelYIter;
324  if (kValY != 0) {
325  outValue += outValueY * kValY;
326  }
327 
328  inImageLocator += image::detail::difference_type(-kernelXList.size(), 1);
329  }
330 
331  return outValue;
332 }
333 }
334 }
335 } // lsst::afw::math
336 
337 #endif // !defined(LSST_AFW_MATH_CONVOLVEIMAGE_H)
void scaledPlus(OutImageT &outImage, double c1, InImageT const &inImage1, double c2, InImageT const &inImage2)
Compute the scaled sum of two images.
Parameters to control convolution.
Definition: ConvolveImage.h:51
void setDoCopyEdge(bool doCopyEdge)
Definition: ConvolveImage.h:68
T end(T... args)
void setDoNormalize(bool doNormalize)
Definition: ConvolveImage.h:67
OutImageT::SinglePixel convolveAtAPoint(typename InImageT::const_xy_locator inImageLocator, typename lsst::afw::image::Image< lsst::afw::math::Kernel::Pixel >::const_xy_locator kernelLocator, int kWidth, int kHeight)
Apply convolution kernel to an image at one point.
A base class for image defects.
ConvolutionControl(bool doNormalize=true, bool doCopyEdge=false, int maxInterpolationDistance=10)
Definition: ConvolveImage.h:53
A traits class for MaskedImage.
Definition: MaskedImage.h:53
void setMaxInterpolationDistance(int maxInterpolationDistance)
Definition: ConvolveImage.h:69
ImageT::SinglePixel edgePixel(lsst::afw::image::detail::Image_tag)
Return an off-the-edge pixel appropriate for a given Image type.
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...
_view_t::xy_locator::const_t const_xy_locator
A const_xy_locator.
Definition: ImageBase.h:127
T size(T... args)
STL class.
T begin(T... args)