32 #include "lsst/afw/detection/Footprint.h" 33 #include "lsst/afw/geom/Point.h" 34 #include "lsst/afw/geom/Extent.h" 35 #include "lsst/afw/geom/Box.h" 36 #include "lsst/afw/image/ImageAlgorithm.h" 37 #include "lsst/afw/image/Image.h" 38 #include "lsst/afw/math/offsetImage.h" 44 namespace afwMath = lsst::afw::math;
51 template <
typename PixelT>
53 template <
typename PixelT>
55 template <
typename PixelT>
57 template <
typename PixelT>
63 struct noop :
public afwImage::pixelOp1<T> {
64 T operator()(T x)
const {
return x; }
68 struct andMask :
public afwImage::pixelOp1<T> {
69 andMask(T mask) : _mask(mask) {}
70 T operator()(T x)
const {
return (x & _mask); }
76 andMask<T> makeAndMask(T
val) {
77 return andMask<T>(
val);
83 template<
typename LhsT,
typename RhsT>
84 std::shared_ptr<afwImage::Image<LhsT>>
85 makeImageFromMask(afwImage::Mask<RhsT>
const& rhs,
86 afwImage::pixelOp1<RhsT>
const& func=noop<RhsT>()
89 std::shared_ptr<afwImage::Image<LhsT>> lhs =
90 std::make_shared<afwImage::Image<LhsT> >(rhs.getDimensions());
91 lhs->setXY0(rhs.getXY0());
93 for (
int y = 0; y != lhs->getHeight(); ++y) {
97 lhsEnd = lhs->row_end(y); lhsPtr != lhsEnd; ++rhsPtr, ++lhsPtr) {
98 *lhsPtr = func(*rhsPtr);
106 double distanceSquared(
double x,
double y, afwDetection::PeakRecord
const& peak) {
107 return std::pow(peak.getIx() - x, 2) + std::pow(peak.getIy() - y, 2);
114 template <
typename MaskT>
115 class BlendedFunctor {
118 afwDetection::PeakRecord
const& central,
119 afwDetection::PeakCatalog
const& peaks,
120 afwImage::MaskPixel turnOff,
121 afwImage::MaskPixel turnOn
131 int x = point.getX();
132 int y = point.getY();
133 double const central = distanceSquared(x, y, _central);
134 for (afwDetection::PeakCatalog::const_iterator iter = _peaks.begin(), end = _peaks.end();
135 iter != end; ++iter) {
136 double const dist2 = distanceSquared(x, y, *iter);
137 if (dist2 < central) {
146 afwDetection::PeakRecord
const& _central;
147 afwDetection::PeakCatalog
const& _peaks;
148 afwImage::MaskPixel
const _turnOff;
149 afwImage::MaskPixel
const _turnOn;
172 template <
typename PixelT>
173 PTR(afwImage::MaskedImage<PixelT>)
179 afwImage::positionToIndex(getYCenter()));
181 cen[1] - height/2 - _parentExposure->getY0());
183 afwGeom::BoxI bbox(llc, afwGeom::ExtentI(width, height));
185 PTR(MaskedImageT)
image;
187 MaskedImageT mimg = _parentExposure->getMaskedImage();
188 image.reset(
new MaskedImageT(mimg, bbox, afwImage::LOCAL,
true));
189 }
catch(lsst::pex::exceptions::LengthError &e) {
190 LSST_EXCEPT_ADD(e,
"Extracting image of PSF candidate");
197 afwImage::MaskPixel
const intrp = MaskedImageT::Mask::getPlaneBitMask(
"INTRP");
198 afwImage::MaskPixel
const detected = MaskedImageT::Mask::getPlaneBitMask(
"DETECTED");
201 if (getMaskBlends()) {
202 CONST_PTR(afwDetection::Footprint) foot = getSource()->getFootprint();
203 typedef afwDetection::PeakCatalog PeakCatalog;
204 PeakCatalog
const& peaks = foot->getPeaks();
205 if (peaks.size() > 1) {
207 double best = std::numeric_limits<double>::infinity();
208 PTR(afwDetection::PeakRecord) central;
209 for (PeakCatalog::const_iterator iter = peaks.begin(), end = peaks.end(); iter != end; ++iter) {
210 double const dist2 = distanceSquared(getXCenter(), getYCenter(), *iter);
218 PeakCatalog others(peaks.getTable());
219 others.reserve(peaks.size() - 1);
220 for (PeakCatalog::const_iterator iter = peaks.begin(), end = peaks.end(); iter != end; ++iter) {
221 PTR(afwDetection::PeakRecord) ptr(iter);
222 if (central != ptr) {
223 others.push_back(ptr);
227 BlendedFunctor<typename MaskedImageT::Mask::Pixel> functor(*central, others, detected, intrp);
228 foot->getSpans()->clippedTo(
image->getBBox())->applyFunctor(functor, *
image->getMask());
236 typedef afwDetection::FootprintSet::FootprintList FootprintList;
239 PTR(afwDetection::FootprintSet) fs =
240 std::make_shared<afwDetection::FootprintSet>(*mim, afwDetection::Threshold(1));
241 CONST_PTR(FootprintList) feet = fs->getFootprints();
243 if (feet->size() > 1) {
248 for (FootprintList::const_iterator fiter = feet->begin(); fiter != feet->end(); ++fiter) {
249 PTR(afwDetection::Footprint) foot = *fiter;
250 if (foot->contains(cen)) {
255 auto bigSpan = foot->getSpans()->dilated(ngrow)->clippedTo(
image->getBBox());
256 bigSpan->clearMask(*
image->getMask(), detected);
257 bigSpan->setMask(*
image->getMask(), intrp);
262 if (_pixelThreshold > 0.0) {
263 CONST_PTR(afwDetection::FootprintSet) fpSet =
264 std::make_shared<afwDetection::FootprintSet>(*
image,
265 afwDetection::Threshold(_pixelThreshold, afwDetection::Threshold::PIXEL_STDEV));
266 for (FootprintList::const_iterator fpIter = fpSet->getFootprints()->begin();
267 fpIter != fpSet->getFootprints()->end(); ++fpIter) {
268 CONST_PTR(afwDetection::Footprint) fp = *fpIter;
269 if (!fp->contains(cen)) {
270 fp->getSpans()->clearMask(*image->getMask(), detected);
271 fp->getSpans()->setMask(*image->getMask(), intrp);
285 template <
typename PixelT>
286 CONST_PTR(afwImage::MaskedImage<PixelT>)
288 if (!_image || (width != _image->getWidth() || height != _image->getHeight())) {
289 _image = extractImage(width, height);
299 template <
typename PixelT>
301 int const width = getWidth() == 0 ? _defaultWidth : getWidth();
302 int const height = getHeight() == 0 ? _defaultWidth : getHeight();
303 return getMaskedImage(width, height);
311 template <
typename PixelT>
312 PTR(afwImage::MaskedImage<PixelT>)
314 std::
string const algorithm,
317 unsigned int const width = getWidth() == 0 ? _defaultWidth : getWidth();
318 unsigned int const height = getHeight() == 0 ? _defaultWidth : getHeight();
319 if (_offsetImage && static_cast<unsigned int>(_offsetImage->getWidth()) == width + 2*buffer &&
320 static_cast<unsigned int>(_offsetImage->getHeight()) == height + 2*buffer) {
324 PTR(
MaskedImageT) image = extractImage(width + 2*buffer, height + 2*buffer);
326 double const xcen = getXCenter(), ycen = getYCenter();
327 double const dx = afwImage::positionToIndex(xcen,
true).second;
328 double const dy = afwImage::positionToIndex(ycen,
true).second;
330 PTR(
MaskedImageT) offset = afwMath::offsetImage(*image, -dx, -dy, algorithm);
332 afwGeom::Extent2I dims(width, height);
333 afwGeom::Box2I box(llc, dims);
334 _offsetImage.reset(
new MaskedImageT(*offset, box, afwImage::LOCAL,
true));
boost::shared_ptr< afw::image::MaskedImage< PixelT > const > getMaskedImage() const
Return the image at the position of the Source, without any sub-pixel shifts to put the centre of the...
Fit spatial kernel using approximate fluxes for candidates, and solving a linear system of equations...
Class used by SpatialCell for spatial PSF fittig.
Class stored in SpatialCells for spatial Psf fitting.
lsst::afw::image::MaskedImage< PixelT > MaskedImageT