lsst.ip.diffim  18.1.0-6-gdda7f3e+11
ImageSubtract.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 
34 #include <iostream>
35 #include <numeric>
36 #include <limits>
37 
38 #include "boost/timer.hpp"
39 
40 #include "Eigen/Core"
41 
42 #include "lsst/afw/image.h"
43 #include "lsst/afw/math.h"
44 #include "lsst/log/Log.h"
45 #include "lsst/pex/policy/Policy.h"
47 
48 #include "lsst/ip/diffim.h"
49 
50 namespace afwImage = lsst::afw::image;
51 namespace afwMath = lsst::afw::math;
53 namespace pexPolicy = lsst::pex::policy;
54 
55 namespace lsst {
56 namespace ip {
57 namespace diffim {
58 
62 template <typename PixelT>
63 Eigen::MatrixXd imageToEigenMatrix(
65  ) {
66  unsigned int rows = img.getHeight();
67  unsigned int cols = img.getWidth();
68  Eigen::MatrixXd M = Eigen::MatrixXd::Zero(rows, cols);
69  for (int y = 0; y != img.getHeight(); ++y) {
70  int x = 0;
71  for (typename afwImage::Image<PixelT>::x_iterator ptr = img.row_begin(y);
72  ptr != img.row_end(y); ++ptr, ++x) {
73  // M is addressed row, col. Need to invert y-axis.
74  // WARNING : CHECK UNIT TESTS BEFORE YOU COMMIT THIS (-y-1) INVERSION
75  M(rows-y-1,x) = *ptr;
76  }
77  }
78  return M;
79 }
80 
81 Eigen::MatrixXi maskToEigenMatrix(
83  ) {
84  unsigned int rows = mask.getHeight();
85  unsigned int cols = mask.getWidth();
86  Eigen::MatrixXi M = Eigen::MatrixXi::Zero(rows, cols);
87  for (int y = 0; y != mask.getHeight(); ++y) {
88  int x = 0;
90  ptr != mask.row_end(y); ++ptr, ++x) {
91  // M is addressed row, col. Need to invert y-axis.
92  // WARNING : CHECK UNIT TESTS BEFORE YOU COMMIT THIS (-y-1) INVERSION
93  M(rows-y-1,x) = *ptr;
94  }
95  }
96  return M;
97 }
98 
99 
115 template <typename PixelT, typename BackgroundT>
117  lsst::afw::image::MaskedImage<PixelT> const &templateImage,
118  lsst::afw::image::MaskedImage<PixelT> const &scienceMaskedImage,
119  lsst::afw::math::Kernel const &convolutionKernel,
120  BackgroundT background,
121  bool invert
122  ) {
123 
124  boost::timer t;
125  t.restart();
126 
127  afwImage::MaskedImage<PixelT> convolvedMaskedImage(templateImage.getDimensions());
129  convolutionControl.setDoNormalize(false);
130  afwMath::convolve(convolvedMaskedImage, templateImage,
131  convolutionKernel, convolutionControl);
132 
133  /* Add in background */
134  *(convolvedMaskedImage.getImage()) += background;
135 
136  /* Do actual subtraction */
137  convolvedMaskedImage -= scienceMaskedImage;
138 
139  /* Invert */
140  if (invert) {
141  convolvedMaskedImage *= -1.0;
142  }
143 
144  double time = t.elapsed();
145  LOGL_DEBUG("TRACE4.ip.diffim.convolveAndSubtract",
146  "Total compute time to convolve and subtract : %.2f s", time);
147 
148  return convolvedMaskedImage;
149 }
150 
166 template <typename PixelT, typename BackgroundT>
168  lsst::afw::image::Image<PixelT> const &templateImage,
169  lsst::afw::image::MaskedImage<PixelT> const &scienceMaskedImage,
170  lsst::afw::math::Kernel const &convolutionKernel,
171  BackgroundT background,
172  bool invert
173  ) {
174 
175  boost::timer t;
176  t.restart();
177 
178  afwImage::MaskedImage<PixelT> convolvedMaskedImage(templateImage.getDimensions());
180  convolutionControl.setDoNormalize(false);
181  afwMath::convolve(*convolvedMaskedImage.getImage(), templateImage,
182  convolutionKernel, convolutionControl);
183 
184  /* Add in background */
185  *(convolvedMaskedImage.getImage()) += background;
186 
187  /* Do actual subtraction */
188  *convolvedMaskedImage.getImage() -= *scienceMaskedImage.getImage();
189 
190  /* Invert */
191  if (invert) {
192  *convolvedMaskedImage.getImage() *= -1.0;
193  }
194  convolvedMaskedImage.getMask()->assign(*scienceMaskedImage.getMask());
195  convolvedMaskedImage.getVariance()->assign(*scienceMaskedImage.getVariance());
196 
197  double time = t.elapsed();
198  LOGL_DEBUG("TRACE4.ip.diffim.convolveAndSubtract",
199  "Total compute time to convolve and subtract : %.2f s", time);
200 
201  return convolvedMaskedImage;
202 }
203 
204 /***********************************************************************************************************/
205 //
206 // Explicit instantiations
207 //
208 
209 template
210 Eigen::MatrixXd imageToEigenMatrix(lsst::afw::image::Image<float> const &);
211 
212 template
213 Eigen::MatrixXd imageToEigenMatrix(lsst::afw::image::Image<double> const &);
214 
215 template class FindSetBits<lsst::afw::image::Mask<> >;
216 template class ImageStatistics<float>;
217 template class ImageStatistics<double>;
218 
219 /* */
220 
221 #define p_INSTANTIATE_convolveAndSubtract(TEMPLATE_IMAGE_T, TYPE) \
222  template \
223  lsst::afw::image::MaskedImage<TYPE> convolveAndSubtract( \
224  lsst::afw::image::TEMPLATE_IMAGE_T<TYPE> const& templateImage, \
225  lsst::afw::image::MaskedImage<TYPE> const& scienceMaskedImage, \
226  lsst::afw::math::Kernel const& convolutionKernel, \
227  double background, \
228  bool invert); \
229  \
230  template \
231  afwImage::MaskedImage<TYPE> convolveAndSubtract( \
232  lsst::afw::image::TEMPLATE_IMAGE_T<TYPE> const& templateImage, \
233  lsst::afw::image::MaskedImage<TYPE> const& scienceMaskedImage, \
234  lsst::afw::math::Kernel const& convolutionKernel, \
235  lsst::afw::math::Function2<double> const& backgroundFunction, \
236  bool invert); \
237 
238 #define INSTANTIATE_convolveAndSubtract(TYPE) \
239 p_INSTANTIATE_convolveAndSubtract(Image, TYPE) \
240 p_INSTANTIATE_convolveAndSubtract(MaskedImage, TYPE)
241 /*
242  * Here are the instantiations.
243  *
244  * Do we need double diffim code? It isn't sufficient to remove it here; you'll have to also remove at
245  * least SpatialModelKernel<double>
246  */
249 
250 }}} // end of namespace lsst::ip::diffim
VariancePtr getVariance() const
An include file to include the header files for lsst::ip::diffim.
Eigen::MatrixXi maskToEigenMatrix(lsst::afw::image::Mask< lsst::afw::image::MaskPixel > const &mask)
lsst::afw::image::MaskedImage< PixelT > convolveAndSubtract(lsst::afw::image::MaskedImage< PixelT > const &templateImage, lsst::afw::image::MaskedImage< PixelT > const &scienceMaskedImage, lsst::afw::math::Kernel const &convolutionKernel, BackgroundT background, bool invert=true)
Execute fundamental task of convolving template and subtracting it from science image.
x_iterator row_begin(int y) const
#define LOGL_DEBUG(logger, message...)
Class to accumulate Mask bits.
Definition: FindSetBits.h:53
lsst::geom::Extent2I getDimensions() const
Eigen::MatrixXd imageToEigenMatrix(lsst::afw::image::Image< PixelT > const &img)
Turns a 2-d Image into a 2-d Eigen Matrix.
double x
Class to calculate difference image statistics.
x_iterator row_end(int y) const
#define INSTANTIATE_convolveAndSubtract(TYPE)
lsst::geom::Extent2I getDimensions() const
int y