LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
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 
39 #include <limits>
40 #include <sstream>
41 
42 #include "lsst/pex/exceptions.h"
43 #include "lsst/afw/geom.h"
44 #include "lsst/afw/image/Image.h"
46 #include "lsst/afw/math/Kernel.h"
48 
49 namespace lsst {
50 namespace afw {
51 namespace math {
52 
59  public:
61  bool doNormalize = true,
62  bool doCopyEdge = false,
63  int maxInterpolationDistance = 10,
67  )
69  :
70  _doNormalize(doNormalize),
71  _doCopyEdge(doCopyEdge),
72  _maxInterpolationDistance(maxInterpolationDistance),
73  _devicePreference(devicePreference)
74  { }
75 
76  bool getDoNormalize() const { return _doNormalize; }
77  bool getDoCopyEdge() const { return _doCopyEdge; }
80 
81  void setDoNormalize(bool doNormalize) {_doNormalize = doNormalize; }
82  void setDoCopyEdge(bool doCopyEdge) { _doCopyEdge = doCopyEdge; }
83  void setMaxInterpolationDistance(int maxInterpolationDistance) {
84  _maxInterpolationDistance = maxInterpolationDistance; }
85  void setDevicePreference(lsst::afw::gpu::DevicePreference devicePreference) { _devicePreference = devicePreference; }
86 
87  private:
88  bool _doNormalize;
89  bool _doCopyEdge;
94  };
95 
96  template <typename OutImageT, typename InImageT>
97  void scaledPlus(
98  OutImageT &outImage,
99  double c1,
100  InImageT const &inImage1,
101  double c2,
102  InImageT const &inImage2);
103 
104  template <typename OutImageT, typename InImageT>
105  inline typename OutImageT::SinglePixel convolveAtAPoint(
106  typename InImageT::const_xy_locator inImageLocator,
108  int kWidth,
109  int kHeight);
110 
111  template <typename OutImageT, typename InImageT>
112  inline typename OutImageT::SinglePixel convolveAtAPoint(
113  typename InImageT::const_xy_locator inImageLocator,
114  std::vector<lsst::afw::math::Kernel::Pixel> const& kernelColList,
115  std::vector<lsst::afw::math::Kernel::Pixel> const& kernelRowList);
116 
117  template <typename OutImageT, typename InImageT, typename KernelT>
118  void convolve(
119  OutImageT& convolvedImage,
120  InImageT const& inImage,
121  KernelT const& kernel,
122  ConvolutionControl const& convolutionControl = ConvolutionControl());
123 
124  template <typename OutImageT, typename InImageT, typename KernelT>
125  void convolve(
126  OutImageT& convolvedImage,
127  InImageT const& inImage,
128  KernelT const& kernel,
129  bool doNormalize,
130  bool doCopyEdge = false);
131 
137  template <typename ImageT>
138  typename ImageT::SinglePixel edgePixel(
141  ) {
142  typedef typename ImageT::SinglePixel SinglePixelT;
143  return SinglePixelT(
144  std::numeric_limits<SinglePixelT>::has_quiet_NaN ?
145  std::numeric_limits<SinglePixelT>::quiet_NaN() : 0);
146  }
147 
156  template <typename MaskedImageT>
157  typename MaskedImageT::SinglePixel edgePixel(
160  ) {
161  typedef typename MaskedImageT::Image::Pixel ImagePixelT;
162  typedef typename MaskedImageT::Variance::Pixel VariancePixelT;
163 
164  return typename MaskedImageT::SinglePixel(
165  std::numeric_limits<ImagePixelT>::has_quiet_NaN ?
166  std::numeric_limits<ImagePixelT>::quiet_NaN() : 0,
167  MaskedImageT::Mask::getPlaneBitMask("NO_DATA"),
168  std::numeric_limits<VariancePixelT>::infinity());
169  }
170 }}} // lsst::afw::math
171 
172 /*
173  * Define inline functions
174  */
175 
185 template <typename OutImageT, typename InImageT>
186 inline typename OutImageT::SinglePixel lsst::afw::math::convolveAtAPoint(
187  typename InImageT::const_xy_locator inImageLocator,
191  int kWidth,
192  int kHeight)
193 {
194  typename OutImageT::SinglePixel outValue = 0;
195  for (int kRow = 0; kRow != kHeight; ++kRow) {
197  kernelLocator + lsst::afw::image::detail::difference_type(kWidth, 0);
198  kernelLocator != kEnd; ++inImageLocator.x(), ++kernelLocator.x()) {
199  typename lsst::afw::math::Kernel::Pixel const kVal = kernelLocator[0];
200  if (kVal != 0) {
201  outValue += *inImageLocator*kVal;
202  }
203  }
204 
205  inImageLocator += lsst::afw::image::detail::difference_type(-kWidth, 1);
206  kernelLocator += lsst::afw::image::detail::difference_type(-kWidth, 1);
207  }
208 
209  return outValue;
210 }
211 
221 template <typename OutImageT, typename InImageT>
222 inline typename OutImageT::SinglePixel lsst::afw::math::convolveAtAPoint(
223  typename InImageT::const_xy_locator inImageLocator,
224  std::vector<lsst::afw::math::Kernel::Pixel> const &kernelXList,
226  std::vector<lsst::afw::math::Kernel::Pixel> const &kernelYList)
227 {
228  typedef typename std::vector<lsst::afw::math::Kernel::Pixel>::const_iterator k_iter;
229 
230  typedef typename OutImageT::SinglePixel OutT;
231  OutT outValue = 0;
232  for (k_iter kernelYIter = kernelYList.begin(), yEnd = kernelYList.end();
233  kernelYIter != yEnd; ++kernelYIter) {
234 
235  OutT outValueY = 0;
236  for (k_iter kernelXIter = kernelXList.begin(), xEnd = kernelXList.end();
237  kernelXIter != xEnd; ++kernelXIter, ++inImageLocator.x()) {
238  typename lsst::afw::math::Kernel::Pixel const kValX = *kernelXIter;
239  if (kValX != 0) {
240  outValueY += *inImageLocator*kValX;
241  }
242  }
243 
244  double const kValY = *kernelYIter;
245  if (kValY != 0) {
246  outValue += outValueY*kValY;
247  }
248 
249  inImageLocator += lsst::afw::image::detail::difference_type(-kernelXList.size(), 1);
250  }
251 
252  return outValue;
253 }
254 
255 
256 #endif // !defined(LSST_AFW_MATH_CONVOLVEIMAGE_H)
void scaledPlus(OutImageT &outImage, double c1, InImageT const &inImage1, double c2, InImageT const &inImage2)
An include file to include the header files for lsst::afw::geom.
Declare the Kernel class and subclasses.
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.
Include files required for standard LSST Exception handling.
Parameters to control convolution.
Definition: ConvolveImage.h:58
bool _doNormalize
normalize the kernel to sum=1?
Definition: ConvolveImage.h:88
void setDoCopyEdge(bool doCopyEdge)
Definition: ConvolveImage.h:82
bool _doCopyEdge
instead of setting them to the standard edge pixel?
Definition: ConvolveImage.h:89
void setDoNormalize(bool doNormalize)
Definition: ConvolveImage.h:81
DevicePreference
A type used to select whether to use CPU or GPU device.
A traits class for MaskedImage.
Definition: MaskedImage.h:53
void setMaxInterpolationDistance(int maxInterpolationDistance)
Definition: ConvolveImage.h:83
Support for 2-D images.
Interface for CPU/GPU device selection.
lsst::afw::gpu::DevicePreference _devicePreference
choose CPU or GPU acceleration
Definition: ConvolveImage.h:93
ImageT::SinglePixel edgePixel(lsst::afw::image::detail::Image_tag)
Return an off-the-edge pixel appropriate for a given Image type.
lsst::afw::gpu::DevicePreference getDevicePreference() const
Definition: ConvolveImage.h:79
void setDevicePreference(lsst::afw::gpu::DevicePreference devicePreference)
Definition: ConvolveImage.h:85
_view_t::xy_locator::const_t const_xy_locator
A const_xy_locator.
Definition: Image.h:141
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...
const DevicePreference DEFAULT_DEVICE_PREFERENCE
Default DevicePreference value.
Implementation of the Class MaskedImage.
ConvolutionControl(bool doNormalize=true, bool doCopyEdge=false, int maxInterpolationDistance=10, lsst::afw::gpu::DevicePreference devicePreference=lsst::afw::gpu::DEFAULT_DEVICE_PREFERENCE)
Definition: ConvolveImage.h:60
int _maxInterpolationDistance
over which to attempt interpolation
Definition: ConvolveImage.h:91