LSSTApplications  18.1.0
LSSTDataManagementBasePackage
Public Types | Public Member Functions | List of all members
lsst::ip::diffim::KernelCandidateDetection< PixelT > Class Template Reference

Search through images for Footprints with no masked pixels. More...

#include <KernelCandidateDetection.h>

Public Types

typedef std::shared_ptr< KernelCandidateDetectionPtr
 
typedef std::shared_ptr< lsst::afw::image::MaskedImage< PixelT > > MaskedImagePtr
 

Public Member Functions

 KernelCandidateDetection (lsst::pex::policy::Policy const &policy)
 
virtual ~KernelCandidateDetection ()
 
void apply (MaskedImagePtr const &templateMaskedImage, MaskedImagePtr const &scienceMaskedImage)
 Runs Detection on a single image for significant peaks, and checks returned Footprints for Masked pixels. More...
 
bool growCandidate (std::shared_ptr< lsst::afw::detection::Footprint > fp, int fpGrowPix, MaskedImagePtr const &templateMaskedImage, MaskedImagePtr const &scienceMaskedImage)
 
std::vector< std::shared_ptr< lsst::afw::detection::Footprint > > getFootprints ()
 

Detailed Description

template<typename PixelT>
class lsst::ip::diffim::KernelCandidateDetection< PixelT >

Search through images for Footprints with no masked pixels.

Note
Runs detection on the template; searches through both images for masked pixels
Parameters
templateMaskedImageMaskedImage that will be convolved with kernel
scienceMaskedImageMaskedImage to subtract convolved template from
policyPolicy for operations; in particular object detection

Definition at line 35 of file KernelCandidateDetection.h.

Member Typedef Documentation

◆ MaskedImagePtr

Definition at line 38 of file KernelCandidateDetection.h.

◆ Ptr

Definition at line 37 of file KernelCandidateDetection.h.

Constructor & Destructor Documentation

◆ KernelCandidateDetection()

Definition at line 33 of file KernelCandidateDetection.cc.

35  :
36  _policy(policy),
37  _badBitMask(0),
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  }
T end(T... args)
Provides consistent interface for LSST exceptions.
Definition: Exception.h:107
#define LOGL_DEBUG(logger, message...)
Log a debug-level message using a varargs/printf style interface.
Definition: Log.h:489
virtual char const * what(void) const noexcept
Return a character string summarizing this exception.
Definition: Exception.cc:99
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:383
STL class.
T begin(T... args)
StringArray getStringArray(const std::string &name) const
return an array of values associated with the given name
Definition: Policy.h:1027

◆ ~KernelCandidateDetection()

template<typename PixelT >
virtual lsst::ip::diffim::KernelCandidateDetection< PixelT >::~KernelCandidateDetection ( )
inlinevirtual

Definition at line 42 of file KernelCandidateDetection.h.

42 {};

Member Function Documentation

◆ apply()

template<typename PixelT >
void lsst::ip::diffim::KernelCandidateDetection< PixelT >::apply ( MaskedImagePtr const &  templateMaskedImage,
MaskedImagePtr const &  scienceMaskedImage 
)

Runs Detection on a single image for significant peaks, and checks returned Footprints for Masked pixels.

Note
Accepts two MaskedImages, one of which is to be convolved to match the other. The Detection package is run on either the image to be convolved (assumed to be higher S/N than the other image), or the image to not be convolved (assumed lower S/N; however if you run detection on a very deep template, you might not have significant S/N objects in the science image). The subimages associated with each returned Footprint in both images are checked for Masked pixels; Footprints containing Masked pixels are rejected. The Footprints are grown by an amount specified in the Policy. The acceptible Footprints are returned in a vector.
Returns
Vector of "clean" Footprints around which Image Subtraction Kernels will be built.

Definition at line 77 of file KernelCandidateDetection.cc.

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
95 
96  // Find detections
98  afwDetect::createThreshold(detThreshold, detThresholdType);
99 
100  if (detOnTemplate == true) {
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 {
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<std::shared_ptr<afwDetect::Footprint>>::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  }
A Threshold is used to pass a threshold value to detection algorithms.
Definition: Threshold.h:43
Provides consistent interface for LSST exceptions.
Definition: Exception.h:107
const std::string getString(const std::string &name) const
return a string value associated with the given name .
Definition: Policy.h:631
#define LOGL_DEBUG(logger, message...)
Log a debug-level message using a varargs/printf style interface.
Definition: Log.h:489
STL class.
T clear(T... args)
Threshold createThreshold(const double value, const std::string type="value", const bool polarity=true)
Factory method for creating Threshold objects.
Definition: Threshold.cc:109
bool getBool(const std::string &name) const
return a boolean value associated with the given name.
Definition: Policy.h:589
double getDouble(const std::string &name) const
return a double value associated with the given name.
Definition: Policy.h:617
T size(T... args)
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
A set of Footprints, associated with a MaskedImage.
Definition: FootprintSet.h:53
STL class.
T c_str(T... args)
bool growCandidate(std::shared_ptr< lsst::afw::detection::Footprint > fp, int fpGrowPix, MaskedImagePtr const &templateMaskedImage, MaskedImagePtr const &scienceMaskedImage)
int getInt(const std::string &name) const
return an integer value associated with the given name.
Definition: Policy.h:603

◆ getFootprints()

Definition at line 52 of file KernelCandidateDetection.h.

52 {return _footprints;};

◆ growCandidate()

template<typename PixelT >
bool lsst::ip::diffim::KernelCandidateDetection< PixelT >::growCandidate ( std::shared_ptr< lsst::afw::detection::Footprint fp,
int  fpGrowPix,
MaskedImagePtr const &  templateMaskedImage,
MaskedImagePtr const &  scienceMaskedImage 
)

Definition at line 147 of file KernelCandidateDetection.cc.

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  */
159  FindSetBits<afwImage::Mask<afwImage::MaskPixel> > fsb;
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->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);
173 
174  int xc = int(0.5 * (fpBBox.getMinX() + fpBBox.getMaxX()));
175  int yc = int(0.5 * (fpBBox.getMinY() + fpBBox.getMaxY()));
178  std::make_shared<afwGeom::SpanSet>(afwGeom::Box2I(afwGeom::Point2I(xc, yc),
179  afwGeom::Extent2I(1,1))))
180  );
181  return growCandidate(fpCore, fpGrowPix, templateMaskedImage, scienceMaskedImage);
182  }
183 
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());
190 
191  /* Grow the footprint
192  * flag true = isotropic grow = slow
193  * flag false = 'manhattan grow' = fast
194  *
195  * The manhattan masks are rotated 45 degree w.r.t. the coordinate
196  * system. They intersect the vertices of the rectangle that would
197  * connect pixels (X0,Y0) (X1,Y0), (X0,Y1), (X1,Y1).
198  *
199  * The isotropic masks do take considerably longer to grow and are
200  * basically elliptical. X0, X1, Y0, Y1 delimit the extent of the
201  * ellipse.
202  *
203  * In both cases, since the masks aren't rectangles oriented with
204  * the image coordinate system, when we DO extract such rectangles
205  * as subimages for kernel fitting, some corner pixels can be found
206  * in multiple subimages.
207  *
208  */
209  std::shared_ptr<afwDetect::Footprint> fpGrow = std::make_shared<afwDetect::Footprint>(
210  fp->getSpans()->dilated(fpGrowPix, afwGeom::Stencil::MANHATTAN)
211  );
212 
213  /* Next we look at the image within this Footprint.
214  */
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());
222 
223  /* Failure Condition 2)
224  * Grown off the image
225  */
226  if (!(templateMaskedImage->getBBox().contains(fpGrowBBox))) {
227  LOGL_DEBUG("TRACE3.ip.diffim.KernelCandidateDetection.apply",
228  "Footprint grown off image");
229  return false;
230  }
231 
232  /* Grab subimages; report any exception */
233  bool subimageHasFailed = false;
234  try {
235  afwImage::MaskedImage<PixelT> templateSubimage(*templateMaskedImage, fpGrowBBox);
236  afwImage::MaskedImage<PixelT> scienceSubimage(*scienceMaskedImage, fpGrowBBox);
237 
238  // Search for any masked pixels within the footprint
239  fsb.apply(*(templateSubimage.getMask()));
240  if (fsb.getBits() & _badBitMask) {
241  LOGL_DEBUG("TRACE3.ip.diffim.KernelCandidateDetection.apply",
242  "Footprint has masked pix (vals=%d) in image to convolve",
243  fsb.getBits());
244  subimageHasFailed = true;
245  }
246 
247  fsb.apply(*(scienceSubimage.getMask()));
248  if (fsb.getBits() & _badBitMask) {
249  LOGL_DEBUG("TRACE3.ip.diffim.KernelCandidateDetection.apply",
250  "Footprint has masked pix (vals=%d) in image not to convolve",
251  fsb.getBits());
252  subimageHasFailed = true;
253  }
254 
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",
259  e.what());
260  subimageHasFailed = true;
261  }
262  if (subimageHasFailed) {
263  return false;
264  } else {
265  /* We have a good candidate */
266  _footprints.push_back(fpGrow);
267  return true;
268  }
269  }
Provides consistent interface for LSST exceptions.
Definition: Exception.h:107
#define LOGL_DEBUG(logger, message...)
Log a debug-level message using a varargs/printf style interface.
Definition: Log.h:489
T push_back(T... args)
virtual char const * what(void) const noexcept
Return a character string summarizing this exception.
Definition: Exception.cc:99
int getMaxY() const noexcept
Definition: Box.h:149
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:74
int getMaxX() const noexcept
Definition: Box.h:148
Class to describe the properties of a detected object from an image.
Definition: Footprint.h:62
int getMinX() const noexcept
Definition: Box.h:144
bool growCandidate(std::shared_ptr< lsst::afw::detection::Footprint > fp, int fpGrowPix, MaskedImagePtr const &templateMaskedImage, MaskedImagePtr const &scienceMaskedImage)
int getInt(const std::string &name) const
return an integer value associated with the given name.
Definition: Policy.h:603
An integer coordinate rectangle.
Definition: Box.h:54
int getMinY() const noexcept
Definition: Box.h:145

The documentation for this class was generated from the following files: