LSSTApplications  11.0-13-gbb96280,12.1+18,12.1+7,12.1-1-g14f38d3+72,12.1-1-g16c0db7+5,12.1-1-g5961e7a+84,12.1-1-ge22e12b+23,12.1-11-g06625e2+4,12.1-11-g0d7f63b+4,12.1-19-gd507bfc,12.1-2-g7dda0ab+38,12.1-2-gc0bc6ab+81,12.1-21-g6ffe579+2,12.1-21-gbdb6c2a+4,12.1-24-g941c398+5,12.1-3-g57f6835+7,12.1-3-gf0736f3,12.1-37-g3ddd237,12.1-4-gf46015e+5,12.1-5-g06c326c+20,12.1-5-g648ee80+3,12.1-5-gc2189d7+4,12.1-6-ga608fc0+1,12.1-7-g3349e2a+5,12.1-7-gfd75620+9,12.1-9-g577b946+5,12.1-9-gc4df26a+10
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"
15 #include "lsst/log/Log.h"
17 #include "lsst/pex/policy/Policy.h"
18 
21 
22 namespace afwGeom = lsst::afw::geom;
23 namespace afwImage = lsst::afw::image;
24 namespace afwDetect = lsst::afw::detection;
25 namespace pexExcept = lsst::pex::exceptions;
26 
27 namespace lsst {
28 namespace ip {
29 namespace diffim {
30 
31 
32  template <typename PixelT>
34  lsst::pex::policy::Policy const& policy
35  ) :
36  _policy(policy),
37  _badBitMask(0),
38  _footprints(std::vector<lsst::afw::detection::Footprint::Ptr>()) {
39 
40  std::vector<std::string> detBadMaskPlanes = _policy.getStringArray("badMaskPlanes");
41  for (std::vector<std::string>::iterator mi = detBadMaskPlanes.begin();
42  mi != detBadMaskPlanes.end(); ++mi){
43  try {
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",
49  e.what());
50  }
51  }
52  LOGL_DEBUG("TRACE2.ip.diffim.KernelCandidateDetection",
53  "Using bad bit mask %d", _badBitMask);
54  }
55 
56 
57 
76  template <typename PixelT>
78  MaskedImagePtr const& templateMaskedImage,
79  MaskedImagePtr const& scienceMaskedImage
80  ) {
81 
82  // Parse the Policy
83  int fpNpixMin = _policy.getInt("fpNpixMin");
84  int fpGrowPix = _policy.getInt("fpGrowPix");
85 
86  bool detOnTemplate = _policy.getBool("detOnTemplate");
87  double detThreshold = _policy.getDouble("detThreshold");
88  std::string detThresholdType = _policy.getString("detThresholdType");
89 
90  /* reset private variables */
91  _footprints.clear();
92 
93  // List of Footprints
94  std::shared_ptr<std::vector<afwDetect::Footprint::Ptr> > footprintListInPtr;
95 
96  // Find detections
97  afwDetect::Threshold threshold =
98  afwDetect::createThreshold(detThreshold, detThresholdType);
99 
100  if (detOnTemplate == true) {
101  afwDetect::FootprintSet footprintSet(
102  *(templateMaskedImage),
103  threshold,
104  "",
105  fpNpixMin);
106  // Get the associated footprints
107 
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());
112  }
113  else {
114  afwDetect::FootprintSet footprintSet(
115  *(scienceMaskedImage),
116  threshold,
117  "",
118  fpNpixMin);
119 
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());
124  }
125 
126  // Iterate over footprints, look for "good" ones
127  for (std::vector<afwDetect::Footprint::Ptr>::iterator i = footprintListInPtr->begin();
128  i != footprintListInPtr->end(); ++i) {
129 
130  LOGL_DEBUG("TRACE3.ip.diffim.KernelCandidateDetection.apply",
131  "Processing footprint %d", (*i)->getId());
132  growCandidate((*i), fpGrowPix, templateMaskedImage, scienceMaskedImage);
133  }
134 
135  if (_footprints.size() == 0) {
137  "Unable to find any footprints for Psf matching");
138  }
139 
140  LOGL_DEBUG("TRACE1.ip.diffim.KernelCandidateDetection.apply",
141  "Found %d clean footprints above threshold %.3f",
142  _footprints.size(), detThreshold);
143 
144  }
145 
146  template <typename PixelT>
149  int fpGrowPix,
150  MaskedImagePtr const& templateMaskedImage,
151  MaskedImagePtr const& scienceMaskedImage
152  ) {
153  int fpNpixMax = _policy.getInt("fpNpixMax");
154 
155  /* Functor to search through the images for masked pixels within *
156  * candidate footprints. Might want to consider changing the default
157  * mask planes it looks through.
158  */
160 
161  afwGeom::Box2I fpBBox = fp->getBBox();
162  /* Failure Condition 1)
163  *
164  * Footprint has too many pixels off the bat. We don't want to throw
165  * away these guys, they have alot of signal! Lets just use the core of
166  * it.
167  *
168  */
169  if (fp->getNpix() > 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->getNpix(), fpNpixMax);
173 
174  int xc = int(0.5 * (fpBBox.getMinX() + fpBBox.getMaxX()));
175  int yc = int(0.5 * (fpBBox.getMinY() + fpBBox.getMaxY()));
178  );
179  return growCandidate(fpCore, fpGrowPix, templateMaskedImage, scienceMaskedImage);
180  }
181 
182  LOGL_DEBUG("TRACE5.ip.diffim.KernelCandidateDetection.apply",
183  "Original footprint in parent : %d,%d -> %d,%d -> %d,%d",
184  fpBBox.getMinX(), fpBBox.getMinY(),
185  int(0.5 * (fpBBox.getMinX() + fpBBox.getMaxX())),
186  int(0.5 * (fpBBox.getMinY() + fpBBox.getMaxY())),
187  fpBBox.getMaxX(), fpBBox.getMaxY());
188 
189  /* Grow the footprint
190  * flag true = isotropic grow = slow
191  * flag false = 'manhattan grow' = fast
192  *
193  * The manhattan masks are rotated 45 degree w.r.t. the coordinate
194  * system. They intersect the vertices of the rectangle that would
195  * connect pixels (X0,Y0) (X1,Y0), (X0,Y1), (X1,Y1).
196  *
197  * The isotropic masks do take considerably longer to grow and are
198  * basically elliptical. X0, X1, Y0, Y1 delimit the extent of the
199  * ellipse.
200  *
201  * In both cases, since the masks aren't rectangles oriented with
202  * the image coordinate system, when we DO extract such rectangles
203  * as subimages for kernel fitting, some corner pixels can be found
204  * in multiple subimages.
205  *
206  */
207  afwDetect::Footprint::Ptr fpGrow =
208  afwDetect::growFootprint(fp, fpGrowPix, false);
209 
210  /* Next we look at the image within this Footprint.
211  */
212  afwGeom::Box2I fpGrowBBox = fpGrow->getBBox();
213  LOGL_DEBUG("TRACE5.ip.diffim.KernelCandidateDetection.apply",
214  "Grown footprint in parent : %d,%d -> %d,%d -> %d,%d",
215  fpGrowBBox.getMinX(), fpGrowBBox.getMinY(),
216  int(0.5 * (fpGrowBBox.getMinX() + fpGrowBBox.getMaxX())),
217  int(0.5 * (fpGrowBBox.getMinY() + fpGrowBBox.getMaxY())),
218  fpGrowBBox.getMaxX(), fpGrowBBox.getMaxY());
219 
220  /* Failure Condition 2)
221  * Grown off the image
222  */
223  if (!(templateMaskedImage->getBBox().contains(fpGrowBBox))) {
224  LOGL_DEBUG("TRACE3.ip.diffim.KernelCandidateDetection.apply",
225  "Footprint grown off image");
226  return false;
227  }
228 
229  /* Grab subimages; report any exception */
230  bool subimageHasFailed = false;
231  try {
232  afwImage::MaskedImage<PixelT> templateSubimage(*templateMaskedImage, fpGrowBBox);
233  afwImage::MaskedImage<PixelT> scienceSubimage(*scienceMaskedImage, fpGrowBBox);
234 
235  // Search for any masked pixels within the footprint
236  fsb.apply(*(templateSubimage.getMask()));
237  if (fsb.getBits() & _badBitMask) {
238  LOGL_DEBUG("TRACE3.ip.diffim.KernelCandidateDetection.apply",
239  "Footprint has masked pix (vals=%d) in image to convolve",
240  fsb.getBits());
241  subimageHasFailed = true;
242  }
243 
244  fsb.apply(*(scienceSubimage.getMask()));
245  if (fsb.getBits() & _badBitMask) {
246  LOGL_DEBUG("TRACE3.ip.diffim.KernelCandidateDetection.apply",
247  "Footprint has masked pix (vals=%d) in image not to convolve",
248  fsb.getBits());
249  subimageHasFailed = true;
250  }
251 
252  } catch (pexExcept::Exception& e) {
253  LOGL_DEBUG("TRACE3.ip.diffim.KernelCandidateDetection.apply",
254  "Exception caught extracting Footprint");
255  LOGL_DEBUG("TRACE4.ip.diffim.KernelCandidateDetection.apply",
256  e.what());
257  subimageHasFailed = true;
258  }
259  if (subimageHasFailed) {
260  return false;
261  } else {
262  /* We have a good candidate */
263  _footprints.push_back(fpGrow);
264  return true;
265  }
266  }
267 
268 /***********************************************************************************************************/
269 //
270 // Explicit instantiations
271 //
272  typedef float PixelT;
273  template class KernelCandidateDetection<PixelT>;
274 
275 }}} // end of namespace lsst::ip::diffim
276 
Image Subtraction helper functions.
An include file to include the header files for lsst::afw::geom.
int getMinY() const
Definition: Box.h:125
boost::shared_ptr< FootprintList > getFootprints()
: Return the Footprints of detected objects
Definition: FootprintSet.h:148
std::shared_ptr< lsst::afw::image::MaskedImage< PixelT > > MaskedImagePtr
a container for holding hierarchical configuration data in memory.
Definition: Policy.h:169
lsst::afw::detection::Footprint Footprint
Definition: Source.h:60
StringArray getStringArray(const std::string &name) const
return an array of values associated with the given name
Definition: Policy.h:1027
void apply(MaskT const &mask)
Definition: FindSetBits.h:68
std::shared_ptr< Footprint > Ptr
Definition: Footprint.h:67
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)
#define LOGL_DEBUG(logger, message...)
Log a debug-level message using a varargs/printf style interface.
Definition: Log.h:513
Class to accumulate Mask bits.
Definition: FindSetBits.h:53
An integer coordinate rectangle.
Definition: Box.h:53
int getMinX() const
Definition: Box.h:124
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
std::shared_ptr< KernelCandidateDetection > Ptr
LSST DM logging module built on log4cxx.
MaskT::Pixel getBits() const
Definition: FindSetBits.h:65
An include file to include the header files for lsst::afw::image.
int getMaxX() const
Definition: Box.h:128
boost::shared_ptr< Footprint > growFootprint(Footprint const &foot, int nGrow, bool isotropic=true)
Grow a Footprint by nGrow pixels, returning a new Footprint.
An include file to include the header files for lsst::afw::detection.
MaskPtr getMask(bool const noThrow=false) const
Return a (Ptr to) the MaskedImage&#39;s mask.
Definition: MaskedImage.h:885
Search through images for Footprints with no masked pixels.
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:78
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(double const value, std::string const typeStr, bool const polarity)
Factory method for creating Threshold objects.
Definition: Threshold.cc:138
virtual char const * what(void) const
Return a character string summarizing this exception.
Definition: Exception.cc:112
#define LSST_EXCEPT(type,...)
Create an exception with a given type and message and optionally other arguments (dependent on the ty...
Definition: Exception.h:46
A set of Footprints, associated with a MaskedImage.
Definition: FootprintSet.h:55
int getMaxY() const
Definition: Box.h:129