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
KernelCandidateDetection.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
12 #include "lsst/afw/geom.h"
13 #include "lsst/afw/image.h"
14 #include "lsst/afw/detection.h"
16 #include "lsst/pex/policy/Policy.h"
17 #include "lsst/pex/logging/Trace.h"
18 
21 
22 namespace afwGeom = lsst::afw::geom;
23 namespace afwImage = lsst::afw::image;
24 namespace afwDetect = lsst::afw::detection;
25 namespace pexLog = lsst::pex::logging;
26 namespace pexExcept = lsst::pex::exceptions;
27 
28 namespace lsst {
29 namespace ip {
30 namespace diffim {
31 
32 
33  template <typename PixelT>
35  lsst::pex::policy::Policy const& policy
36  ) :
37  _policy(policy),
38  _badBitMask(0),
39  _footprints(std::vector<lsst::afw::detection::Footprint::Ptr>()) {
40 
41  std::vector<std::string> detBadMaskPlanes = _policy.getStringArray("badMaskPlanes");
42  for (std::vector<std::string>::iterator mi = detBadMaskPlanes.begin();
43  mi != detBadMaskPlanes.end(); ++mi){
44  try {
46  } catch (pexExcept::Exception& e) {
47  pexLog::TTrace<6>("lsst.ip.diffim.KernelCandidateDetection",
48  "Cannot update bad bit mask with %s", (*mi).c_str());
49  pexLog::TTrace<7>("lsst.ip.diffim.KernelCandidateDetection",
50  e.what());
51  }
52  }
53  pexLog::TTrace<4>("lsst.ip.diffim.KernelCandidateDetection",
54  "Using bad bit mask %d", _badBitMask);
55  }
56 
57 
58 
77  template <typename PixelT>
79  MaskedImagePtr const& templateMaskedImage,
80  MaskedImagePtr const& scienceMaskedImage
81  ) {
82 
83  // Parse the Policy
84  int fpNpixMin = _policy.getInt("fpNpixMin");
85  int fpGrowPix = _policy.getInt("fpGrowPix");
86 
87  bool detOnTemplate = _policy.getBool("detOnTemplate");
88  double detThreshold = _policy.getDouble("detThreshold");
89  std::string detThresholdType = _policy.getString("detThresholdType");
90 
91  /* reset private variables */
92  _footprints.clear();
93 
94  // List of Footprints
95  boost::shared_ptr<std::vector<afwDetect::Footprint::Ptr> > footprintListInPtr;
96 
97  // Find detections
98  afwDetect::Threshold threshold =
99  afwDetect::createThreshold(detThreshold, detThresholdType);
100 
101  if (detOnTemplate == true) {
102  afwDetect::FootprintSet footprintSet(
103  *(templateMaskedImage),
104  threshold,
105  "",
106  fpNpixMin);
107  // Get the associated footprints
108 
109  footprintListInPtr = footprintSet.getFootprints();
110  pexLog::TTrace<4>("lsst.ip.diffim.KernelCandidateDetection.apply",
111  "Found %d total footprints in template above %.3f %s",
112  footprintListInPtr->size(), detThreshold, detThresholdType.c_str());
113  }
114  else {
115  afwDetect::FootprintSet footprintSet(
116  *(scienceMaskedImage),
117  threshold,
118  "",
119  fpNpixMin);
120 
121  footprintListInPtr = footprintSet.getFootprints();
122  pexLog::TTrace<4>("lsst.ip.diffim.KernelCandidateDetection.apply",
123  "Found %d total footprints in science image above %.3f %s",
124  footprintListInPtr->size(), detThreshold, detThresholdType.c_str());
125  }
126 
127  // Iterate over footprints, look for "good" ones
128  for (std::vector<afwDetect::Footprint::Ptr>::iterator i = footprintListInPtr->begin();
129  i != footprintListInPtr->end(); ++i) {
130 
131  pexLog::TTrace<6>("lsst.ip.diffim.KernelCandidateDetection.apply",
132  "Processing footprint %d", (*i)->getId());
133  growCandidate((*i), fpGrowPix, templateMaskedImage, scienceMaskedImage);
134  }
135 
136  if (_footprints.size() == 0) {
138  "Unable to find any footprints for Psf matching");
139  }
140 
141  pexLog::TTrace<1>("lsst.ip.diffim.KernelCandidateDetection.apply",
142  "Found %d clean footprints above threshold %.3f",
143  _footprints.size(), detThreshold);
144 
145  }
146 
147  template <typename PixelT>
150  int fpGrowPix,
151  MaskedImagePtr const& templateMaskedImage,
152  MaskedImagePtr const& scienceMaskedImage
153  ) {
154  int fpNpixMax = _policy.getInt("fpNpixMax");
155 
156  /* Functor to search through the images for masked pixels within *
157  * candidate footprints. Might want to consider changing the default
158  * mask planes it looks through.
159  */
161 
162  afwGeom::Box2I fpBBox = fp->getBBox();
163  /* Failure Condition 1)
164  *
165  * Footprint has too many pixels off the bat. We don't want to throw
166  * away these guys, they have alot of signal! Lets just use the core of
167  * it.
168  *
169  */
170  if (fp->getNpix() > fpNpixMax) {
171  pexLog::TTrace<6>("lsst.ip.diffim.KernelCandidateDetection.apply",
172  "Footprint has too many pix: %d (max =%d)",
173  fp->getNpix(), fpNpixMax);
174 
175  int xc = int(0.5 * (fpBBox.getMinX() + fpBBox.getMaxX()));
176  int yc = int(0.5 * (fpBBox.getMinY() + fpBBox.getMaxY()));
179  );
180  return growCandidate(fpCore, fpGrowPix, templateMaskedImage, scienceMaskedImage);
181  }
182 
183  pexLog::TTrace<8>("lsst.ip.diffim.KernelCandidateDetection.apply",
184  "Original footprint in parent : %d,%d -> %d,%d -> %d,%d",
185  fpBBox.getMinX(), fpBBox.getMinY(),
186  int(0.5 * (fpBBox.getMinX() + fpBBox.getMaxX())),
187  int(0.5 * (fpBBox.getMinY() + fpBBox.getMaxY())),
188  fpBBox.getMaxX(), fpBBox.getMaxY());
189 
190  /* Grow the footprint
191  * flag true = isotropic grow = slow
192  * flag false = 'manhattan grow' = fast
193  *
194  * The manhattan masks are rotated 45 degree w.r.t. the coordinate
195  * system. They intersect the vertices of the rectangle that would
196  * connect pixels (X0,Y0) (X1,Y0), (X0,Y1), (X1,Y1).
197  *
198  * The isotropic masks do take considerably longer to grow and are
199  * basically elliptical. X0, X1, Y0, Y1 delimit the extent of the
200  * ellipse.
201  *
202  * In both cases, since the masks aren't rectangles oriented with
203  * the image coordinate system, when we DO extract such rectangles
204  * as subimages for kernel fitting, some corner pixels can be found
205  * in multiple subimages.
206  *
207  */
208  afwDetect::Footprint::Ptr fpGrow =
209  afwDetect::growFootprint(fp, fpGrowPix, false);
210 
211  /* Next we look at the image within this Footprint.
212  */
213  afwGeom::Box2I fpGrowBBox = fpGrow->getBBox();
214  pexLog::TTrace<8>("lsst.ip.diffim.KernelCandidateDetection.apply",
215  "Grown footprint in parent : %d,%d -> %d,%d -> %d,%d",
216  fpGrowBBox.getMinX(), fpGrowBBox.getMinY(),
217  int(0.5 * (fpGrowBBox.getMinX() + fpGrowBBox.getMaxX())),
218  int(0.5 * (fpGrowBBox.getMinY() + fpGrowBBox.getMaxY())),
219  fpGrowBBox.getMaxX(), fpGrowBBox.getMaxY());
220 
221  /* Failure Condition 2)
222  * Grown off the image
223  */
224  if (!(templateMaskedImage->getBBox().contains(fpGrowBBox))) {
225  pexLog::TTrace<6>("lsst.ip.diffim.KernelCandidateDetection.apply",
226  "Footprint grown off image");
227  return false;
228  }
229 
230  /* Grab subimages; report any exception */
231  bool subimageHasFailed = false;
232  try {
233  afwImage::MaskedImage<PixelT> templateSubimage(*templateMaskedImage, fpGrowBBox);
234  afwImage::MaskedImage<PixelT> scienceSubimage(*scienceMaskedImage, fpGrowBBox);
235 
236  // Search for any masked pixels within the footprint
237  fsb.apply(*(templateSubimage.getMask()));
238  if (fsb.getBits() & _badBitMask) {
239  pexLog::TTrace<6>("lsst.ip.diffim.KernelCandidateDetection.apply",
240  "Footprint has masked pix (vals=%d) in image to convolve",
241  fsb.getBits());
242  subimageHasFailed = true;
243  }
244 
245  fsb.apply(*(scienceSubimage.getMask()));
246  if (fsb.getBits() & _badBitMask) {
247  pexLog::TTrace<6>("lsst.ip.diffim.KernelCandidateDetection.apply",
248  "Footprint has masked pix (vals=%d) in image not to convolve",
249  fsb.getBits());
250  subimageHasFailed = true;
251  }
252 
253  } catch (pexExcept::Exception& e) {
254  pexLog::TTrace<6>("lsst.ip.diffim.KernelCandidateDetection.apply",
255  "Exception caught extracting Footprint");
256  pexLog::TTrace<7>("lsst.ip.diffim.KernelCandidateDetection.apply",
257  e.what());
258  subimageHasFailed = true;
259  }
260  if (subimageHasFailed) {
261  return false;
262  } else {
263  /* We have a good candidate */
264  _footprints.push_back(fpGrow);
265  return true;
266  }
267  }
268 
269 /***********************************************************************************************************/
270 //
271 // Explicit instantiations
272 //
273  typedef float PixelT;
274  template class KernelCandidateDetection<PixelT>;
275 
276 }}} // end of namespace lsst::ip::diffim
277 
Image Subtraction helper functions.
int getMaxY() const
Definition: Box.h:129
An include file to include the header files for lsst::afw::geom.
boost::shared_ptr< Footprint > Ptr
Definition: Footprint.h:67
boost::shared_ptr< FootprintList > getFootprints()
Definition: FootprintSet.h:146
a container for holding hierarchical configuration data in memory.
Definition: Policy.h:169
definition of the Trace messaging facilities
StringArray getStringArray(const std::string &name) const
Definition: Policy.h:1023
void apply(MaskT const &mask)
Definition: FindSetBits.h:68
void apply(MaskedImagePtr const &templateMaskedImage, MaskedImagePtr const &scienceMaskedImage)
Runs Detection on a single image for significant peaks, and checks returned Footprints for Masked pix...
A Threshold is used to pass a threshold value to detection algorithms.
Definition: Threshold.h:44
Detect candidates for kernels within 2 images.
KernelCandidateDetection(lsst::pex::policy::Policy const &policy)
Class to accumulate Mask bits.
Definition: FindSetBits.h:53
An integer coordinate rectangle.
Definition: Box.h:53
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
MaskT::Pixel getBits() const
Definition: FindSetBits.h:65
An include file to include the header files for lsst::afw::image.
boost::shared_ptr< Footprint > growFootprint(Footprint const &foot, int nGrow, bool isotropic=true)
An include file to include the header files for lsst::afw::detection.
int getMinY() const
Definition: Box.h:125
MaskPtr getMask(bool const noThrow=false) const
Return a (Ptr to) the MaskedImage&#39;s mask.
Definition: MaskedImage.h:879
Search through images for Footprints with no masked pixels.
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:77
int getMinX() const
Definition: Box.h:124
bool growCandidate(lsst::afw::detection::Footprint::Ptr fp, int fpGrowPix, MaskedImagePtr const &templateMaskedImage, MaskedImagePtr const &scienceMaskedImage)
static MaskPixelT getPlaneBitMask(const std::vector< std::string > &names)
Return the bitmask corresponding to a vector of plane names OR&#39;d together.
Definition: Mask.cc:860
A set of pixels in an Image.
Definition: Footprint.h:62
Threshold createThreshold(const double value, const std::string type="value", const bool polarity=true)
Factory method for creating Threshold objects.
Definition: Threshold.cc:138
virtual char const * what(void) const
Definition: Exception.cc:112
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
A set of Footprints, associated with a MaskedImage.
Definition: FootprintSet.h:53
lsst::afw::detection::Footprint Footprint
Definition: Source.h:61
int getMaxX() const
Definition: Box.h:128
boost::shared_ptr< KernelCandidateDetection > Ptr
boost::shared_ptr< lsst::afw::image::MaskedImage< PixelT > > MaskedImagePtr