lsst.ip.diffim
g174f8ebed5+0e497d1560
Toggle main menu visibility
Loading...
Searching...
No Matches
src
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/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/exceptions/Runtime.h
"
46
47
#include "
lsst/ip/diffim.h
"
48
49
namespace
afwImage
=
lsst::afw::image
;
50
namespace
afwMath
=
lsst::afw::math
;
51
namespace
pexExcept
=
lsst::pex::exceptions
;
52
namespace
dafBase
=
lsst::daf::base
;
53
54
namespace
lsst
{
55
namespace
ip
{
56
namespace
diffim
{
57
61
template
<
typename
PixelT>
62
Eigen::MatrixXd
imageToEigenMatrix
(
63
lsst::afw::image::Image<PixelT>
const
&img
64
) {
65
unsigned
int
rows = img.
getHeight
();
66
unsigned
int
cols = img.
getWidth
();
67
Eigen::MatrixXd M = Eigen::MatrixXd::Zero(rows, cols);
68
for
(
int
y = 0; y != img.
getHeight
(); ++y) {
69
int
x = 0;
70
for
(
typename
afwImage::Image<PixelT>::x_iterator
ptr = img.
row_begin
(y);
71
ptr != img.
row_end
(y); ++ptr, ++x) {
72
// M is addressed row, col. Need to invert y-axis.
73
// WARNING : CHECK UNIT TESTS BEFORE YOU COMMIT THIS (-y-1) INVERSION
74
M(rows-y-1,x) = *ptr;
75
}
76
}
77
return
M;
78
}
79
80
Eigen::MatrixXi
maskToEigenMatrix
(
81
lsst::afw::image::Mask<lsst::afw::image::MaskPixel>
const
&
mask
82
) {
83
unsigned
int
rows =
mask
.getHeight();
84
unsigned
int
cols =
mask
.getWidth();
85
Eigen::MatrixXi M = Eigen::MatrixXi::Zero(rows, cols);
86
for
(
int
y = 0; y !=
mask
.getHeight(); ++y) {
87
int
x = 0;
88
for
(
afwImage::Mask<lsst::afw::image::MaskPixel>::x_iterator
ptr =
mask
.row_begin(y);
89
ptr !=
mask
.row_end(y); ++ptr, ++x) {
90
// M is addressed row, col. Need to invert y-axis.
91
// WARNING : CHECK UNIT TESTS BEFORE YOU COMMIT THIS (-y-1) INVERSION
92
M(rows-y-1,x) = *ptr;
93
}
94
}
95
return
M;
96
}
97
98
114
template
<
typename
PixelT,
typename
BackgroundT>
115
afwImage::MaskedImage<PixelT>
convolveAndSubtract
(
116
lsst::afw::image::MaskedImage<PixelT>
const
&templateImage,
117
lsst::afw::image::MaskedImage<PixelT>
const
&scienceMaskedImage,
118
lsst::afw::math::Kernel
const
&convolutionKernel,
119
BackgroundT
background
,
120
bool
invert
121
) {
122
123
boost::timer::cpu_timer t;
124
125
afwImage::MaskedImage<PixelT>
convolvedMaskedImage(templateImage.
getDimensions
());
126
afwMath::ConvolutionControl
convolutionControl =
afwMath::ConvolutionControl
();
127
convolutionControl.
setDoNormalize
(
false
);
128
afwMath::convolve
(convolvedMaskedImage, templateImage,
129
convolutionKernel, convolutionControl);
130
131
/* Add in background */
132
*(convolvedMaskedImage.
getImage
()) +=
background
;
133
134
/* Do actual subtraction */
135
convolvedMaskedImage -= scienceMaskedImage;
136
137
/* Invert */
138
if
(invert) {
139
convolvedMaskedImage *= -1.0;
140
}
141
142
t.stop();
143
double
time = 1e-9 * t.elapsed().wall;
144
LOGL_DEBUG
(
"TRACE4.ip.diffim.convolveAndSubtract"
,
145
"Total compute time to convolve and subtract : %.2f s"
, time);
146
147
return
convolvedMaskedImage;
148
}
149
165
template
<
typename
PixelT,
typename
BackgroundT>
166
afwImage::MaskedImage<PixelT>
convolveAndSubtract
(
167
lsst::afw::image::Image<PixelT>
const
&templateImage,
168
lsst::afw::image::MaskedImage<PixelT>
const
&scienceMaskedImage,
169
lsst::afw::math::Kernel
const
&convolutionKernel,
170
BackgroundT
background
,
171
bool
invert
172
) {
173
174
boost::timer::cpu_timer t;
175
176
afwImage::MaskedImage<PixelT>
convolvedMaskedImage(templateImage.
getDimensions
());
177
afwMath::ConvolutionControl
convolutionControl =
afwMath::ConvolutionControl
();
178
convolutionControl.
setDoNormalize
(
false
);
179
afwMath::convolve
(*convolvedMaskedImage.
getImage
(), templateImage,
180
convolutionKernel, convolutionControl);
181
182
/* Add in background */
183
*(convolvedMaskedImage.
getImage
()) +=
background
;
184
185
/* Do actual subtraction */
186
*convolvedMaskedImage.
getImage
() -= *scienceMaskedImage.
getImage
();
187
188
/* Invert */
189
if
(invert) {
190
*convolvedMaskedImage.
getImage
() *= -1.0;
191
}
192
convolvedMaskedImage.
getMask
()->
assign
(*scienceMaskedImage.
getMask
());
193
convolvedMaskedImage.
getVariance
()->
assign
(*scienceMaskedImage.
getVariance
());
194
195
t.stop();
196
double
time = 1e-9 * t.elapsed().wall;
197
LOGL_DEBUG
(
"TRACE4.ip.diffim.convolveAndSubtract"
,
198
"Total compute time to convolve and subtract : %.2f s"
, time);
199
200
return
convolvedMaskedImage;
201
}
202
203
/***********************************************************************************************************/
204
//
205
// Explicit instantiations
206
//
207
208
template
209
Eigen::MatrixXd
imageToEigenMatrix
(
lsst::afw::image::Image<float>
const
&);
210
211
template
212
Eigen::MatrixXd
imageToEigenMatrix
(
lsst::afw::image::Image<double>
const
&);
213
214
template
class
ImageStatistics<float>
;
215
template
class
ImageStatistics<double>
;
216
217
/* */
218
219
#define p_INSTANTIATE_convolveAndSubtract(TEMPLATE_IMAGE_T, TYPE) \
220
template \
221
lsst::afw::image::MaskedImage<TYPE> convolveAndSubtract( \
222
lsst::afw::image::TEMPLATE_IMAGE_T<TYPE> const& templateImage, \
223
lsst::afw::image::MaskedImage<TYPE> const& scienceMaskedImage, \
224
lsst::afw::math::Kernel const& convolutionKernel, \
225
double background, \
226
bool invert); \
227
\
228
template \
229
afwImage::MaskedImage<TYPE> convolveAndSubtract( \
230
lsst::afw::image::TEMPLATE_IMAGE_T<TYPE> const& templateImage, \
231
lsst::afw::image::MaskedImage<TYPE> const& scienceMaskedImage, \
232
lsst::afw::math::Kernel const& convolutionKernel, \
233
lsst::afw::math::Function2<double> const& backgroundFunction, \
234
bool invert); \
235
236
#define INSTANTIATE_convolveAndSubtract(TYPE) \
237
p_INSTANTIATE_convolveAndSubtract(Image, TYPE) \
238
p_INSTANTIATE_convolveAndSubtract(MaskedImage, TYPE)
239
/*
240
* Here are the instantiations.
241
*
242
* Do we need double diffim code? It isn't sufficient to remove it here; you'll have to also remove at
243
* least SpatialModelKernel<double>
244
*/
245
INSTANTIATE_convolveAndSubtract
(
float
);
246
INSTANTIATE_convolveAndSubtract
(
double
);
247
248
}}}
// end of namespace lsst::ip::diffim
Log.h
LOGL_DEBUG
#define LOGL_DEBUG(logger, message...)
image.h
INSTANTIATE_convolveAndSubtract
#define INSTANTIATE_convolveAndSubtract(TYPE)
Definition
ImageSubtract.cc:236
Runtime.h
lsst::afw::image::Image::getWidth
int getWidth() const
lsst::afw::image::Image::getDimensions
lsst::geom::Extent2I getDimensions() const
lsst::afw::image::Mask::assign
void assign(ImageBase const &rhs, lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT)
lsst::afw::image::Image::getHeight
int getHeight() const
lsst::afw::image::Image::row_begin
x_iterator row_begin(int y) const
lsst::afw::image::Image::row_end
x_iterator row_end(int y) const
lsst::afw::image::Image::x_iterator
typename _view_t::x_iterator x_iterator
lsst::afw::image::Image
lsst::afw::image::Mask
lsst::afw::image::MaskedImage
lsst::afw::image::MaskedImage::getDimensions
lsst::geom::Extent2I getDimensions() const
lsst::afw::image::MaskedImage::getVariance
VariancePtr getVariance() const
lsst::afw::image::MaskedImage::getMask
MaskPtr getMask() const
lsst::afw::image::MaskedImage::getImage
ImagePtr getImage() const
lsst::afw::math::ConvolutionControl
lsst::afw::math::ConvolutionControl::setDoNormalize
void setDoNormalize(bool doNormalize)
lsst::afw::math::Kernel
lsst::ip::diffim::ImageStatistics
Class to calculate difference image statistics.
Definition
ImageStatistics.h:59
diffim.h
An include file to include the header files for lsst::ip::diffim.
math.h
lsst::afw::image
lsst::afw::math
lsst::afw::math::convolve
void convolve(OutImageT &convolvedImage, InImageT const &inImage, KernelT const &kernel, ConvolutionControl const &convolutionControl=ConvolutionControl())
lsst::daf::base
lsst::ip::diffim.detectAndMeasure.mask
mask
Definition
detectAndMeasure.py:1015
lsst::ip::diffim.detectAndMeasure.background
background
Definition
detectAndMeasure.py:763
lsst::ip::diffim
Definition
AssessSpatialKernelVisitor.h:22
lsst::ip::diffim::imageToEigenMatrix
Eigen::MatrixXd imageToEigenMatrix(lsst::afw::image::Image< PixelT > const &img)
Turns a 2-d Image into a 2-d Eigen Matrix.
Definition
ImageSubtract.cc:62
lsst::ip::diffim::maskToEigenMatrix
Eigen::MatrixXi maskToEigenMatrix(lsst::afw::image::Mask< lsst::afw::image::MaskPixel > const &mask)
Definition
ImageSubtract.cc:80
lsst::ip::diffim::convolveAndSubtract
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.
Definition
ImageSubtract.cc:115
lsst::ip
Definition
AssessSpatialKernelVisitor.h:21
lsst::pex::exceptions
lsst
Generated on
for lsst.ip.diffim by
1.17.0