12 #include "lsst/afw/geom.h" 13 #include "lsst/afw/image.h" 14 #include "lsst/afw/detection.h" 15 #include "lsst/log/Log.h" 16 #include "lsst/pex/exceptions/Exception.h" 17 #include "lsst/pex/policy/Policy.h" 22 namespace afwGeom = lsst::afw::geom;
23 namespace afwImage = lsst::afw::image;
24 namespace afwDetect = lsst::afw::detection;
25 namespace pexExcept = lsst::pex::exceptions;
32 template <
typename PixelT>
34 lsst::pex::policy::Policy
const& policy
38 _footprints(
std::vector<
std::shared_ptr<
lsst::afw::detection::Footprint>>()) {
40 std::vector<std::string> detBadMaskPlanes =
_policy.getStringArray(
"badMaskPlanes");
41 for (std::vector<std::string>::iterator mi = detBadMaskPlanes.begin();
42 mi != detBadMaskPlanes.end(); ++mi){
44 _badBitMask |= afwImage::Mask<afwImage::MaskPixel>::getPlaneBitMask(*mi);
45 }
catch (pexExcept::Exception& e) {
46 LOGL_DEBUG(
"TRACE3.ip.diffim.KernelCandidateDetection",
47 "Cannot update bad bit mask with %s", (*mi).c_str());
48 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidateDetection",
52 LOGL_DEBUG(
"TRACE2.ip.diffim.KernelCandidateDetection",
76 template <
typename PixelT>
83 int fpNpixMin =
_policy.getInt(
"fpNpixMin");
84 int fpGrowPix =
_policy.getInt(
"fpGrowPix");
86 bool detOnTemplate =
_policy.getBool(
"detOnTemplate");
87 double detThreshold =
_policy.getDouble(
"detThreshold");
88 std::string detThresholdType =
_policy.getString(
"detThresholdType");
94 std::shared_ptr<std::vector<std::shared_ptr<afwDetect::Footprint>>> footprintListInPtr;
97 afwDetect::Threshold threshold =
98 afwDetect::createThreshold(detThreshold, detThresholdType);
100 if (detOnTemplate ==
true) {
101 afwDetect::FootprintSet footprintSet(
102 *(templateMaskedImage),
108 footprintListInPtr = footprintSet.getFootprints();
109 LOGL_DEBUG(
"TRACE2.ip.diffim.KernelCandidateDetection.apply",
110 "Found %d total footprints in template above %.3f %s",
111 footprintListInPtr->size(), detThreshold, detThresholdType.c_str());
114 afwDetect::FootprintSet footprintSet(
115 *(scienceMaskedImage),
120 footprintListInPtr = footprintSet.getFootprints();
121 LOGL_DEBUG(
"TRACE2.ip.diffim.KernelCandidateDetection.apply",
122 "Found %d total footprints in science image above %.3f %s",
123 footprintListInPtr->size(), detThreshold, detThresholdType.c_str());
127 for (std::vector<std::shared_ptr<afwDetect::Footprint>>::iterator i = footprintListInPtr->begin();
128 i != footprintListInPtr->end(); ++i) {
130 LOGL_DEBUG(
"TRACE3.ip.diffim.KernelCandidateDetection.apply",
131 "Processing footprint %d", (*i)->getId());
132 growCandidate((*i), fpGrowPix, templateMaskedImage, scienceMaskedImage);
136 throw LSST_EXCEPT(pexExcept::Exception,
137 "Unable to find any footprints for Psf matching");
140 LOGL_DEBUG(
"TRACE1.ip.diffim.KernelCandidateDetection.apply",
141 "Found %d clean footprints above threshold %.3f",
146 template <
typename PixelT>
148 std::shared_ptr<lsst::afw::detection::Footprint> fp,
153 int fpNpixMax =
_policy.getInt(
"fpNpixMax");
161 afwGeom::Box2I fpBBox = fp->getBBox();
169 if (fp->getArea() >
static_cast<std::size_t
>(fpNpixMax)) {
170 LOGL_DEBUG(
"TRACE3.ip.diffim.KernelCandidateDetection.apply",
171 "Footprint has too many pix: %d (max =%d)",
172 fp->getArea(), fpNpixMax);
174 int xc = int(0.5 * (fpBBox.getMinX() + fpBBox.getMaxX()));
175 int yc = int(0.5 * (fpBBox.getMinY() + fpBBox.getMaxY()));
176 std::shared_ptr<afwDetect::Footprint> fpCore(
177 new afwDetect::Footprint(
178 std::make_shared<afwGeom::SpanSet>(afwGeom::Box2I(afwGeom::Point2I(xc, yc),
179 afwGeom::Extent2I(1,1))))
181 return growCandidate(fpCore, fpGrowPix, templateMaskedImage, scienceMaskedImage);
184 LOGL_DEBUG(
"TRACE5.ip.diffim.KernelCandidateDetection.apply",
185 "Original footprint in parent : %d,%d -> %d,%d -> %d,%d",
186 fpBBox.getMinX(), fpBBox.getMinY(),
187 int(0.5 * (fpBBox.getMinX() + fpBBox.getMaxX())),
188 int(0.5 * (fpBBox.getMinY() + fpBBox.getMaxY())),
189 fpBBox.getMaxX(), fpBBox.getMaxY());
209 std::shared_ptr<afwDetect::Footprint> fpGrow = std::make_shared<afwDetect::Footprint>(
210 fp->getSpans()->dilated(fpGrowPix, afwGeom::Stencil::MANHATTAN)
215 afwGeom::Box2I fpGrowBBox = fpGrow->getBBox();
216 LOGL_DEBUG(
"TRACE5.ip.diffim.KernelCandidateDetection.apply",
217 "Grown footprint in parent : %d,%d -> %d,%d -> %d,%d",
218 fpGrowBBox.getMinX(), fpGrowBBox.getMinY(),
219 int(0.5 * (fpGrowBBox.getMinX() + fpGrowBBox.getMaxX())),
220 int(0.5 * (fpGrowBBox.getMinY() + fpGrowBBox.getMaxY())),
221 fpGrowBBox.getMaxX(), fpGrowBBox.getMaxY());
226 if (!(templateMaskedImage->getBBox().contains(fpGrowBBox))) {
227 LOGL_DEBUG(
"TRACE3.ip.diffim.KernelCandidateDetection.apply",
228 "Footprint grown off image");
233 bool subimageHasFailed =
false;
235 afwImage::MaskedImage<PixelT> templateSubimage(*templateMaskedImage, fpGrowBBox);
236 afwImage::MaskedImage<PixelT> scienceSubimage(*scienceMaskedImage, fpGrowBBox);
239 fsb.
apply(*(templateSubimage.getMask()));
241 LOGL_DEBUG(
"TRACE3.ip.diffim.KernelCandidateDetection.apply",
242 "Footprint has masked pix (vals=%d) in image to convolve",
244 subimageHasFailed =
true;
247 fsb.
apply(*(scienceSubimage.getMask()));
249 LOGL_DEBUG(
"TRACE3.ip.diffim.KernelCandidateDetection.apply",
250 "Footprint has masked pix (vals=%d) in image not to convolve",
252 subimageHasFailed =
true;
255 }
catch (pexExcept::Exception& e) {
256 LOGL_DEBUG(
"TRACE3.ip.diffim.KernelCandidateDetection.apply",
257 "Exception caught extracting Footprint");
258 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidateDetection.apply",
260 subimageHasFailed =
true;
262 if (subimageHasFailed) {
Image Subtraction helper functions.
lsst::pex::policy::Policy _policy
std::shared_ptr< lsst::afw::image::MaskedImage< PixelT > > MaskedImagePtr
void apply(MaskT const &mask)
void apply(MaskedImagePtr const &templateMaskedImage, MaskedImagePtr const &scienceMaskedImage)
Runs Detection on a single image for significant peaks, and checks returned Footprints for Masked pix...
Detect candidates for kernels within 2 images.
KernelCandidateDetection(lsst::pex::policy::Policy const &policy)
lsst::afw::image::MaskPixel _badBitMask
Class to accumulate Mask bits.
Search through images for Footprints with no masked pixels.
std::vector< std::shared_ptr< lsst::afw::detection::Footprint > > _footprints
bool growCandidate(std::shared_ptr< lsst::afw::detection::Footprint > fp, int fpGrowPix, MaskedImagePtr const &templateMaskedImage, MaskedImagePtr const &scienceMaskedImage)
MaskT::Pixel getBits() const