49 #include "boost/format.hpp"
50 #include "lsst/pex/exceptions.h"
65 typedef std::uint64_t IdPixelT;
67 struct Threshold_traits {};
68 struct ThresholdLevel_traits :
public Threshold_traits {
70 struct ThresholdPixelLevel_traits :
public Threshold_traits {
72 struct ThresholdBitmask_traits :
public Threshold_traits {
75 template <
typename PixelT>
78 explicit setIdImage(std::uint64_t
const id,
bool overwriteId =
false,
long const idMask = 0x0)
81 _withSetReplace(false),
82 _overwriteId(overwriteId),
87 pex::exceptions::InvalidParameterError,
88 str(boost::format(
"Id 0x%x sets bits in the protected mask 0x%x") %
_id % _idMask));
92 setIdImage(std::uint64_t
const id,
typename std::set<std::uint64_t> *oldIds,
bool overwriteId =
false,
93 long const idMask = 0x0)
96 _withSetReplace(true),
97 _overwriteId(overwriteId),
99 _pos(oldIds->begin()) {
102 pex::exceptions::InvalidParameterError,
103 str(boost::format(
"Id 0x%x sets bits in the protected mask 0x%x") %
_id % _idMask));
109 auto val = input & ~_idMask;
111 if (val != 0 && _withSetReplace) {
112 _pos = _oldIds->insert(_pos, val);
115 input = (input & _idMask) +
_id;
122 std::uint64_t
const _id;
124 bool _withSetReplace;
126 typename std::set<std::uint64_t> *_oldIds;
127 typename std::set<std::uint64_t>::const_iterator _pos;
134 template <
typename T>
135 inline bool isBadPixel(T) {
140 inline bool isBadPixel(
float val) {
141 return std::isnan(val);
145 inline bool isBadPixel(
double val) {
146 return std::isnan(val);
152 int nbit(
unsigned long i) {
166 template <
typename T>
167 class FindIdsInFootprint {
169 explicit FindIdsInFootprint() : _ids(), _old(0) {}
185 std::set<T>
const &getIds()
const {
return _ids; }
196 bool operator()(std::shared_ptr<PeakRecord const> a, std::shared_ptr<PeakRecord const> b) {
197 if (a->getPeakValue() != b->getPeakValue()) {
198 return (a->getPeakValue() > b->getPeakValue());
201 if (a->getIx() != b->getIx()) {
202 return (a->getIx() < b->getIx());
205 return (a->getIy() < b->getIy());
211 FootprintSet mergeFootprintSets(FootprintSet
const &lhs,
213 FootprintSet
const &rhs,
215 FootprintControl
const &ctrl
219 bool const circular = ctrl.isCircular().first && ctrl.isCircular().second;
220 bool const isotropic = ctrl.isIsotropic().second;
222 bool const left = ctrl.isLeft().first && ctrl.isLeft().second;
223 bool const right = ctrl.isRight().first && ctrl.isRight().second;
224 bool const up = ctrl.isUp().first && ctrl.isUp().second;
225 bool const down = ctrl.isDown().first && ctrl.isDown().second;
227 geom::Box2I
const region = lhs.getRegion();
228 if (region != rhs.getRegion()) {
229 throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
230 boost::format(
"The two FootprintSets must have the same region").str());
233 auto idImage = std::make_shared<image::Image<IdPixelT>>(region);
234 idImage->setXY0(region.getMinX(), region.getMinY());
237 FootprintList
const &lhsFootprints = *lhs.getFootprints();
238 FootprintList
const &rhsFootprints = *rhs.getFootprints();
239 int const nLhs = lhsFootprints.size();
240 int const nRhs = rhsFootprints.size();
245 int const lhsIdNbit = nbit(nLhs);
246 int const lhsIdMask = (lhsIdNbit == 0) ? 0x0 : (1 << lhsIdNbit) - 1;
248 if (std::size_t(nRhs << lhsIdNbit) > std::numeric_limits<IdPixelT>::max() - 1) {
250 pex::exceptions::OverflowError,
251 (boost::format(
"%d + %d footprints need too many bits; change IdPixelT typedef") % nLhs %
259 typedef std::map<int, std::set<std::uint64_t>> OldIdMap;
260 OldIdMap overwrittenIds;
262 auto grower = [&circular, &up, &down, &left, &right, &isotropic](
263 std::shared_ptr<Footprint>
const &foot,
int amount) -> std::shared_ptr<Footprint> {
266 auto tmpFoot = std::make_shared<Footprint>(foot->getSpans()->dilated(amount, element),
270 int top = up ? amount : 0;
271 int bottom = down ? amount : 0;
272 int lLimit = left ? amount : 0;
273 int rLimit = right ? amount : 0;
275 auto yRange = top + bottom + 1;
276 std::vector<geom::Span> spanList;
277 spanList.reserve(yRange);
279 for (
auto dy = 1; dy <= top; ++dy) {
280 spanList.push_back(geom::Span(dy, 0, 0));
282 for (
auto dy = -1; dy >= -bottom; --dy) {
283 spanList.push_back(geom::Span(dy, 0, 0));
285 spanList.push_back(geom::Span(0, -lLimit, rLimit));
286 geom::SpanSet structure(std::move(spanList));
288 std::make_shared<Footprint>(foot->getSpans()->dilated(structure), foot->getRegion());
294 for (FootprintList::const_iterator ptr = lhsFootprints.begin(), end = lhsFootprints.end(); ptr != end;
296 std::shared_ptr<Footprint> foot = *ptr;
298 if (rLhs > 0 && foot->getArea() > 0) {
299 foot = grower(foot, rLhs);
302 std::set<std::uint64_t> overwritten;
304 ->clippedTo(idImage->getBBox())
305 ->applyFunctor(setIdImage<IdPixelT>(
id, &overwritten,
true), *idImage);
307 if (!overwritten.empty()) {
308 overwrittenIds.insert(overwrittenIds.end(), std::make_pair(
id, overwritten));
312 assert(
id <= std::size_t(1 << lhsIdNbit));
313 id = (1 << lhsIdNbit);
314 for (FootprintList::const_iterator ptr = rhsFootprints.begin(), end = rhsFootprints.end(); ptr != end;
315 ++ptr,
id += (1 << lhsIdNbit)) {
316 std::shared_ptr<Footprint> foot = *ptr;
318 if (rRhs > 0 && foot->getArea() > 0) {
319 foot = grower(foot, rRhs);
322 std::set<std::uint64_t> overwritten;
324 ->clippedTo(idImage->getBBox())
325 ->applyFunctor(setIdImage<IdPixelT>(
id, &overwritten,
true, lhsIdMask), *idImage);
327 if (!overwritten.empty()) {
328 overwrittenIds.insert(overwrittenIds.end(), std::make_pair(
id, overwritten));
332 FootprintSet fs(*idImage, Threshold(1), 1,
false);
344 FindIdsInFootprint<IdPixelT> idFinder;
345 for (FootprintList::iterator ptr = fs.getFootprints()->begin(), end = fs.getFootprints()->end();
347 std::shared_ptr<Footprint> foot = *ptr;
350 foot->getSpans()->applyFunctor(idFinder, *idImage);
352 std::set<std::uint64_t> lhsFootprintIndxs, rhsFootprintIndxs;
354 for (std::set<IdPixelT>::iterator idptr = idFinder.getIds().begin(), idend = idFinder.getIds().end();
355 idptr != idend; ++idptr) {
356 unsigned int indx = *idptr;
357 if ((indx & lhsIdMask) > 0) {
358 std::uint64_t i = (indx & lhsIdMask) - 1;
359 lhsFootprintIndxs.insert(i);
363 OldIdMap::iterator mapPtr = overwrittenIds.find(indx);
364 if (mapPtr != overwrittenIds.end()) {
365 std::set<std::uint64_t> &overwritten = mapPtr->second;
367 for (std::set<std::uint64_t>::iterator ptr = overwritten.begin(), end = overwritten.end();
369 lhsFootprintIndxs.insert((*ptr & lhsIdMask) - 1);
376 std::uint64_t i = indx - 1;
377 rhsFootprintIndxs.insert(i);
381 OldIdMap::iterator mapPtr = overwrittenIds.find(indx);
382 if (mapPtr != overwrittenIds.end()) {
383 std::set<std::uint64_t> &overwritten = mapPtr->second;
385 for (std::set<std::uint64_t>::iterator ptr = overwritten.begin(), end = overwritten.end();
387 rhsFootprintIndxs.insert(*ptr - 1);
398 for (std::set<std::uint64_t>::iterator ptr = lhsFootprintIndxs.begin(), end = lhsFootprintIndxs.end();
400 std::uint64_t i = *ptr;
401 assert(i < lhsFootprints.size());
402 PeakCatalog const &oldPeaks = lhsFootprints[i]->getPeaks();
404 int const nold = peaks.
size();
405 peaks.insert(peaks.end(), oldPeaks.begin(), oldPeaks.end());
409 std::inplace_merge(peaks.getInternal().begin(), peaks.getInternal().begin() + nold,
410 peaks.getInternal().end(), SortPeaks());
413 for (std::set<std::uint64_t>::iterator ptr = rhsFootprintIndxs.begin(), end = rhsFootprintIndxs.end();
415 std::uint64_t i = *ptr;
416 assert(i < rhsFootprints.size());
417 PeakCatalog const &oldPeaks = rhsFootprints[i]->getPeaks();
419 int const nold = peaks.
size();
420 peaks.insert(peaks.end(), oldPeaks.begin(), oldPeaks.end());
422 std::inplace_merge(peaks.getInternal().begin(), peaks.getInternal().begin() + nold,
423 peaks.getInternal().end(), SortPeaks());
435 explicit IdSpan(
int id,
int y,
int x0,
int x1,
double good) : id(id), y(y), x0(x0), x1(x1), good(good) {}
445 :
public std::binary_function<const std::shared_ptr<IdSpan>, const std::shared_ptr<IdSpan>, bool> {
446 bool operator()(std::shared_ptr<IdSpan>
const a, std::shared_ptr<IdSpan>
const b) {
449 }
else if (a->id > b->id) {
452 return (a->y < b->y) ?
true :
false;
459 int resolve_alias(std::vector<int>
const &aliases,
463 while (
id != aliases[
id]) {
464 resolved =
id = aliases[id];
473 template <
typename ImageT>
475 std::size_t
const margin = 0) {
476 auto spanSet = foot.getSpans();
477 if (spanSet->size() == 0) {
480 auto bbox = image.getBBox();
481 for (
auto const &spanIter : *spanSet) {
482 auto y = spanIter.getY() - image.getY0();
483 if (static_cast<std::size_t>(y + image.getY0()) < bbox.getMinY() + margin ||
484 static_cast<std::size_t
>(y + image.getY0()) > bbox.getMaxY() - margin) {
487 for (
auto x = spanIter.getMinX() - image.getX0();
x <= spanIter.getMaxX() - image.getX0(); ++
x) {
488 if (static_cast<std::size_t>(
x + image.getX0()) < (bbox.getMinX() + margin) ||
489 static_cast<std::size_t>(
x + image.getX0()) > (bbox.getMaxX() - margin)) {
494 if (
image(
x - 1, y + 1) > val ||
image(
x, y + 1) > val ||
image(
x + 1, y + 1) > val ||
500 if (
image(
x - 1, y + 1) < val ||
image(
x, y + 1) < val ||
image(
x + 1, y + 1) < val ||
507 foot.addPeak(
x + image.getX0(), y + image.getY0(), val);
512 template <
typename ImageT>
513 class FindMaxInFootprint {
515 explicit FindMaxInFootprint(
bool polarity)
516 : _polarity(polarity),
519 _min(std::numeric_limits<double>::max()),
520 _max(-std::numeric_limits<double>::max()) {}
522 void operator()(
geom::Point2I const &point, ImageT
const &val) {
538 void addRecord(
Footprint &foot)
const { foot.addPeak(_x, _y, _polarity ? _max : _min); }
546 template <
typename ImageT,
typename ThresholdT>
547 void findPeaks(std::shared_ptr<Footprint> foot, ImageT
const &img,
bool polarity, ThresholdT) {
548 findPeaksInFootprint(img, polarity, foot->getPeaks(), *foot, 1);
553 std::stable_sort(foot->getPeaks().getInternal().begin(), foot->getPeaks().getInternal().end(),
556 if (foot->getPeaks().empty()) {
557 FindMaxInFootprint<typename ImageT::Pixel> maxFinder(polarity);
558 foot->getSpans()->applyFunctor(maxFinder,
ndarray::ndImage(img.getArray(), img.getXY0()));
559 maxFinder.addRecord(*foot);
564 template <
typename ImageT>
565 void findPeaks(std::shared_ptr<Footprint>, ImageT
const &,
bool, ThresholdBitmask_traits) {
573 template <
typename ImagePixelT,
typename IterT>
574 static inline bool inFootprint(ImagePixelT pixVal, IterT,
bool polarity,
double thresholdVal,
575 ThresholdLevel_traits) {
576 return (polarity ? pixVal : -pixVal) >= thresholdVal;
579 template <
typename ImagePixelT,
typename IterT>
580 static inline bool inFootprint(ImagePixelT pixVal, IterT var,
bool polarity,
double thresholdVal,
581 ThresholdPixelLevel_traits) {
582 return (polarity ? pixVal : -pixVal) >= thresholdVal * ::sqrt(*var);
585 template <
typename ImagePixelT,
typename IterT>
586 static inline bool inFootprint(ImagePixelT pixVal, IterT,
bool,
double thresholdVal,
587 ThresholdBitmask_traits) {
588 return (pixVal & static_cast<long>(thresholdVal));
594 template <
typename IterT>
595 static inline IterT advancePtr(IterT varPtr, Threshold_traits) {
599 template <
typename IterT>
600 static inline IterT advancePtr(IterT varPtr, ThresholdPixelLevel_traits) {
608 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT,
typename ThresholdTraitT>
609 static void findFootprints(
611 geom::Box2I
const &_region,
614 double const footprintThreshold,
615 double const includeThresholdMultiplier,
626 double includeThreshold = footprintThreshold * includeThresholdMultiplier;
628 int const row0 = img.
getY0();
629 int const col0 = img.
getX0();
636 std::vector<int> id1(width + 2);
637 std::fill(id1.begin(), id1.end(), 0);
638 std::vector<int> id2(width + 2);
639 std::fill(id2.begin(), id2.end(), 0);
640 std::vector<int>::iterator idc = id1.begin() + 1;
641 std::vector<int>::iterator idp = id2.begin() + 1;
643 std::vector<int> aliases;
644 aliases.reserve(1 + height / 20);
646 std::vector<std::shared_ptr<IdSpan>> spans;
647 spans.reserve(aliases.capacity());
649 aliases.push_back(0);
657 for (
int y = 0; y != height; ++y) {
658 if (idc == id1.begin() + 1) {
659 idc = id2.
begin() + 1;
660 idp = id1.begin() + 1;
662 idc = id1.begin() + 1;
663 idp = id2.begin() + 1;
665 std::fill_n(idc - 1, width + 2, 0);
668 bool good = (includeThresholdMultiplier == 1.0);
671 x_var_iterator varPtr = (var == NULL) ? NULL : var->
row_begin(y);
672 for (
int x = 0;
x < width; ++
x, ++pixPtr, varPtr = advancePtr(varPtr, ThresholdTraitT())) {
673 ImagePixelT
const pixVal = *pixPtr;
675 if (isBadPixel(pixVal) ||
676 !inFootprint(pixVal, varPtr, polarity, footprintThreshold, ThresholdTraitT())) {
678 auto sp = std::make_shared<IdSpan>(in_span, y, x0,
x - 1, good);
685 if (idc[
x - 1] != 0) {
687 }
else if (idp[
x - 1] != 0) {
689 }
else if (idp[
x] != 0) {
691 }
else if (idp[
x + 1] != 0) {
695 aliases.push_back(
id);
706 if (idp[
x + 1] != 0 && idp[
x + 1] !=
id) {
707 aliases[resolve_alias(aliases, idp[
x + 1])] = resolve_alias(aliases,
id);
709 idc[
x] =
id = idp[
x + 1];
712 if (!good && inFootprint(pixVal, varPtr, polarity, includeThreshold, ThresholdTraitT())) {
719 auto sp = std::make_shared<IdSpan>(in_span, y, x0, width - 1, good);
726 for (
unsigned int i = 0; i < spans.size(); i++) {
727 spans[i]->id = resolve_alias(aliases, spans[i]->
id);
732 if (spans.size() > 0) {
733 std::sort(spans.begin(), spans.end(), IdSpanCompar());
739 if (spans.size() > 0) {
742 for (
unsigned int i = 0; i <= spans.size(); i++) {
743 if (i == spans.size() || spans[i]->id != id) {
745 std::vector<geom::Span> tempSpanList;
746 for (; i0 < i; i0++) {
747 good |= spans[i0]->good;
748 tempSpanList.push_back(
749 geom::Span(spans[i0]->y + row0, spans[i0]->x0 + col0, spans[i0]->x1 + col0));
751 auto tempSpanSet = std::make_shared<geom::SpanSet>(std::move(tempSpanList));
752 auto fp = std::make_shared<Footprint>(tempSpanSet, _region);
754 if (good && fp->getArea() >=
static_cast<std::size_t
>(npixMin)) {
755 _footprints->push_back(fp);
759 if (i < spans.size()) {
768 typedef FootprintSet::FootprintList::iterator fiterator;
769 for (fiterator ptr = _footprints->begin(), end = _footprints->end(); ptr != end; ++ptr) {
770 findPeaks(*ptr, img, polarity, ThresholdTraitT());
775 template <
typename ImagePixelT>
777 int const npixMin,
bool const setPeaks)
778 : daf::base::Citizen(typeid(this)), _footprints(new FootprintList()), _region(img.getBBox()) {
779 typedef float VariancePixelT;
781 findFootprints<ImagePixelT, image::MaskPixel, VariancePixelT, ThresholdLevel_traits>(
782 _footprints.get(), _region, img, NULL, threshold.getValue(img), threshold.getIncludeMultiplier(),
783 threshold.getPolarity(), npixMin, setPeaks);
788 template <
typename MaskPixelT>
790 : daf::base::Citizen(typeid(this)), _footprints(new FootprintList()), _region(msk.getBBox()) {
791 switch (threshold.getType()) {
793 findFootprints<MaskPixelT, MaskPixelT, float, ThresholdBitmask_traits>(
794 _footprints.get(), _region, msk, NULL, threshold.getValue(),
795 threshold.getIncludeMultiplier(), threshold.getPolarity(), npixMin,
false);
799 findFootprints<MaskPixelT, MaskPixelT, float, ThresholdLevel_traits>(
800 _footprints.get(), _region, msk, NULL, threshold.getValue(),
801 threshold.getIncludeMultiplier(), threshold.getPolarity(), npixMin,
false);
805 throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
806 "You must specify a numerical threshold value with a Mask");
810 template <
typename ImagePixelT,
typename MaskPixelT>
812 Threshold
const &threshold, std::string
const &planeName,
int const npixMin,
814 : daf::base::Citizen(typeid(this)),
815 _footprints(new FootprintList()),
816 _region(geom::
Point2I(maskedImg.getX0(), maskedImg.getY0()),
817 geom::
Extent2I(maskedImg.getWidth(), maskedImg.getHeight())) {
820 switch (threshold.getType()) {
822 findFootprints<ImagePixelT, MaskPixelT, VariancePixelT, ThresholdPixelLevel_traits>(
824 threshold.getValue(maskedImg), threshold.getIncludeMultiplier(), threshold.getPolarity(),
828 findFootprints<ImagePixelT, MaskPixelT, VariancePixelT, ThresholdLevel_traits>(
830 threshold.getValue(maskedImg), threshold.getIncludeMultiplier(), threshold.getPolarity(),
835 if (planeName ==
"") {
841 const std::shared_ptr<image::Mask<MaskPixelT>>
mask = maskedImg.
getMask();
842 mask->addMaskPlane(planeName);
844 MaskPixelT
const bitPlane = mask->getPlaneBitMask(planeName);
848 for (
auto const &fIter : *_footprints) {
849 fIter->getSpans()->setMask(*(maskedImg.
getMask()), bitPlane);
874 template <
typename MaskPixelT>
878 ~Startspan() {
delete _span; }
880 bool getSpan() {
return _span; }
881 bool Stop() {
return _stop; }
882 DIRECTION getDirection() {
return _direction; }
884 static int detectedPlane;
885 static int stopPlane;
887 std::shared_ptr<geom::Span const> _span;
888 DIRECTION _direction;
892 template <
typename MaskPixelT>
893 Startspan<MaskPixelT>::Startspan(
894 geom::Span
const *span,
898 : _span(span), _direction(dir), _stop(false) {
902 int const y = span->getY() - mask->
getY0();
903 for (
int x = span->getX0() - mask->
getX0();
x <= span->getX1() - mask->
getX0();
x++) {
904 if (
mask(
x, y, stopPlane)) {
912 template <
typename ImagePixelT,
typename MaskPixelT>
916 : _image(image->getImage()), _mask(image->getMask()) {}
919 bool add(geom::Span *span, DIRECTION
const dir,
bool addToMask =
true);
921 Threshold
const &threshold,
922 double const param = -1);
926 std::vector<std::shared_ptr<Startspan<MaskPixelT>>> _spans;
932 template <
typename ImagePixelT,
typename MaskPixelT>
933 bool StartspanSet<ImagePixelT, MaskPixelT>::add(geom::Span *span,
936 if (dir == RESTART) {
937 if (add(span, UP) || add(span, DOWN,
false)) {
941 auto sspan = std::make_shared<MaskPixelT>(span, dir);
945 _spans.push_back(sspan);
960 template <
typename ImagePixelT,
typename MaskPixelT>
961 bool StartspanSet<ImagePixelT, MaskPixelT>::process(
Footprint *fp,
962 Threshold
const &threshold,
965 int const row0 = _image->
getY0();
966 int const col0 = _image->getOffsetCols();
967 int const height = _image->getHeight();
969 Startspan<MaskPixelT> *sspan = NULL;
970 for (
auto iter = _spans.begin(); iter != _spans.end(); iter++) {
972 if (sspan->getDirection() != DONE) {
979 if (sspan == NULL || sspan->getDirection() == DONE) {
988 DIRECTION
const dir = sspan->getDirection();
992 int x0 = sspan->getSpan()->getX0() - col0;
997 int const di = (dir == UP) ? 1 : -1;
1001 double const thresholdVal = threshold.getValue(param);
1002 bool const polarity = threshold.getPolarity();
1004 for (
int i = sspan->span->y - row0 + di; i < height && i >= 0; i += di) {
1005 pixAccessT imgRow = _image->origin().advance(0, i);
1013 for (
int j = x0 - 1; j >= -1; j--) {
1014 ImagePixelT pixVal = (j < 0) ? thresholdVal - 100 : (polarity ? imgRow[j] : -imgRow[j]);
1015 if (_mask(j, i, Startspan<MaskPixelT>::detectedPlane) || pixVal < threshold) {
1030 for (
int j = nx0 + 1; j <= width; j++) {
1031 ImagePixelT pixVal = (j >= width) ? threshold - 100 :
1032 (polarity ? (F32 ? imgRowF32[j] : imgRowS32[j]) :
1033 (F32 ? -imgRowF32[j] : -imgRowS32[j]));
1034 if ((maskRow[j] & DETECTED) || pixVal < threshold) {
1040 pmSpan
const *sp = pmFootprintAddSpan(fp, i + row0, nx0 + col0, nx1 + col0);
1042 if (add_startspan(startspans, sp, mask, RESTART)) {
1058 for (
int j = nx1 + 1; j <= x1 + 1; j++) {
1059 ImagePixelT pixVal = (j >= width) ? threshold - 100 :
1060 (polarity ? (F32 ? imgRowF32[j] : imgRowS32[j]) : (F32 ? -imgRowF32[j] : -imgRowS32[j]));
1061 if (!(maskRow[j] & DETECTED) && pixVal >= threshold) {
1064 for (; j <= width; j++) {
1065 ImagePixelT pixVal = (j >= width) ? threshold - 100 :
1066 (polarity ? (F32 ? imgRowF32[j] : imgRowS32[j]) :
1067 (F32 ? -imgRowF32[j] : -imgRowS32[j]));
1068 if ((maskRow[j] & DETECTED) || pixVal < threshold) {
1078 sp = pmFootprintAddSpan(fp, i + row0, sx0 + col0, sx1 + col0 - 1);
1079 if (add_startspan(startspans, sp, mask, DONE)) {
1084 sp = pmFootprintAddSpan(fp, i + row0, sx0 + col0, x1 + col0);
1085 if (add_startspan(startspans, sp, mask, DONE)) {
1089 sp = pmFootprintAddSpan(fp, i + row0, x1 + 1 + col0, sx1 + col0 - 1);
1090 if (add_startspan(startspans, sp, mask, RESTART)) {
1097 sp = pmFootprintAddSpan(fp, i + row0, sx0 + col0, sx1 + col0 - 1);
1098 if (add_startspan(startspans, sp, mask, RESTART)) {
1106 if (stop || first ==
false) {
1118 sspan->_direction = DONE;
1119 return stop ?
false :
true;
1140 pmFindFootprintAtPoint(psImage
const *img,
1141 Threshold
const &threshold,
1142 psArray
const *peaks,
1144 assert(img != NULL);
1147 if (img->type.type == PS_TYPE_F32) {
1149 }
else if (img->type.type == PS_TYPE_S32) {
1152 psError(PS_ERR_UNKNOWN,
true,
"Unsupported psImage type: %d", img->type.type);
1155 psF32 *imgRowF32 = NULL;
1156 psS32 *imgRowS32 = NULL;
1158 int const row0 = img->row0;
1159 int const col0 = img->col0;
1160 int const height = img->getHeight();
1161 int const width = img->getWidth();
1167 if (row < 0 || row >= height ||
1168 col < 0 || col >= width) {
1169 psError(PS_ERR_BAD_PARAMETER_VALUE,
true,
1170 "row/col == (%d, %d) are out of bounds [%d--%d, %d--%d]",
1171 row + row0, col + col0, row0, row0 + height - 1, col0, col0 + width - 1);
1175 ImagePixelT pixVal = F32 ? img->data.F32[row][col] : img->data.S32[row][col];
1176 if (pixVal < threshold) {
1177 return pmFootprintAlloc(0, img);
1180 pmFootprint *fp = pmFootprintAlloc(1 + img->getHeight()/10, img);
1186 psImage *mask = psImageAlloc(width, height, PS_TYPE_MASK);
1187 P_PSIMAGE_SET_ROW0(mask, row0);
1188 P_PSIMAGE_SET_COL0(mask, col0);
1189 psImageInit(mask, INITIAL);
1193 assert (peaks == NULL || peaks->n == 0 || pmIsPeak(peaks->data[0]));
1194 if (peaks != NULL) {
1195 for (
int i = 0; i < peaks->n; i++) {
1196 pmPeak *peak = peaks->data[i];
1197 mask->data.PS_TYPE_MASK_DATA[peak->y - mask->row0][peak->x - mask->col0] |= STOP;
1203 psArray *startspans = psArrayAllocEmpty(1);
1205 imgRowF32 = img->data.F32[row];
1206 imgRowS32 = img->data.S32[row];
1207 psMaskType *maskRow = mask->data.PS_TYPE_MASK_DATA[row];
1210 for (i = col; i >= 0; i--) {
1211 pixVal = F32 ? imgRowF32[i] : imgRowS32[i];
1212 if ((maskRow[i] & DETECTED) || pixVal < threshold) {
1217 for (i = col; i < width; i++) {
1218 pixVal = F32 ? imgRowF32[i] : imgRowS32[i];
1219 if ((maskRow[i] & DETECTED) || pixVal < threshold) {
1224 pmSpan
const *sp = pmFootprintAddSpan(fp, row + row0, i0 + col0 + 1, i1 + col0 - 1);
1226 (void)add_startspan(startspans, sp, mask, RESTART);
1231 while (do_startspan(fp, img, mask, threshold, startspans))
continue;
1242 FootprintSet::FootprintSet(geom::Box2I region)
1243 : daf::base::Citizen(typeid(this)), _footprints(std::make_shared<FootprintList>()), _region(region) {}
1245 FootprintSet::FootprintSet(FootprintSet
const &rhs)
1246 : daf::base::Citizen(typeid(this)), _footprints(new FootprintList), _region(rhs._region) {
1247 _footprints->reserve(rhs._footprints->size());
1248 for (FootprintSet::FootprintList::const_iterator ptr = rhs._footprints->begin(),
1249 end = rhs._footprints->end();
1250 ptr != end; ++ptr) {
1251 _footprints->push_back(std::make_shared<Footprint>(**ptr));
1255 FootprintSet &FootprintSet::operator=(FootprintSet
const &rhs) {
1256 FootprintSet tmp(rhs);
1261 void FootprintSet::merge(FootprintSet
const &rhs,
int tGrow,
int rGrow,
bool isotropic) {
1262 FootprintControl
const ctrl(
true, isotropic);
1263 FootprintSet fs = mergeFootprintSets(*
this, tGrow, rhs, rGrow, ctrl);
1267 void FootprintSet::setRegion(geom::Box2I
const ®ion) {
1270 for (FootprintSet::FootprintList::iterator ptr = _footprints->begin(), end = _footprints->end();
1271 ptr != end; ++ptr) {
1272 (*ptr)->setRegion(region);
1276 FootprintSet::FootprintSet(FootprintSet
const &rhs,
int r,
bool isotropic)
1277 : daf::base::Citizen(typeid(this)), _footprints(new FootprintList), _region(rhs._region) {
1279 FootprintSet fs = rhs;
1283 throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
1284 (boost::format(
"I cannot grow by negative numbers: %d") % r).str());
1287 FootprintControl
const ctrl(
true, isotropic);
1288 FootprintSet fs = mergeFootprintSets(FootprintSet(rhs.getRegion()), 0, rhs, r, ctrl);
1292 FootprintSet::FootprintSet(FootprintSet
const &rhs,
int ngrow, FootprintControl
const &ctrl)
1293 : daf::base::Citizen(typeid(this)), _footprints(new FootprintList), _region(rhs._region) {
1295 FootprintSet fs = rhs;
1298 }
else if (ngrow < 0) {
1299 throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
1300 str(boost::format(
"I cannot grow by negative numbers: %d") % ngrow));
1303 FootprintSet fs = mergeFootprintSets(FootprintSet(rhs.getRegion()), 0, rhs, ngrow, ctrl);
1307 FootprintSet::FootprintSet(FootprintSet
const &fs1, FootprintSet
const &fs2,
bool const)
1308 : daf::base::Citizen(typeid(this)), _footprints(new FootprintList()), _region(fs1._region) {
1309 _region.include(fs2._region);
1310 throw LSST_EXCEPT(pex::exceptions::LogicError,
"NOT IMPLEMENTED");
1313 std::shared_ptr<image::Image<FootprintIdPixel>> FootprintSet::insertIntoImage(
bool const relativeIDs)
const {
1314 auto im = std::make_shared<image::Image<FootprintIdPixel>>(_region);
1318 for (
auto const &fIter : *_footprints) {
1322 id = fIter->getId();
1325 fIter->getSpans()->applyFunctor(setIdImage<FootprintIdPixel>(
id), *im);
1331 template <
typename ImagePixelT,
typename MaskPixelT>
1333 HeavyFootprintCtrl
const *ctrl) {
1334 HeavyFootprintCtrl ctrl_s = HeavyFootprintCtrl();
1340 for (FootprintList::iterator ptr = _footprints->begin(), end = _footprints->end(); ptr != end; ++ptr) {
1341 ptr->reset(
new HeavyFootprint<ImagePixelT, MaskPixelT>(**ptr, mimg, ctrl));
1346 for (FootprintList::const_iterator i = _footprints->begin(); i != _footprints->end(); ++i) {
1347 std::shared_ptr<afw::table::SourceRecord> r = cat.addNew();
1348 r->setFootprint(*i);
1358 #define INSTANTIATE(PIXEL) \
1359 template FootprintSet::FootprintSet(image::Image<PIXEL> const &, Threshold const &, int const, \
1361 template FootprintSet::FootprintSet(image::MaskedImage<PIXEL, image::MaskPixel> const &, \
1362 Threshold const &, std::string const &, int const, bool const); \
1363 template void FootprintSet::makeHeavy(image::MaskedImage<PIXEL, image::MaskPixel> const &, \
1364 HeavyFootprintCtrl const *)
iterator begin() const
Return an STL compliant iterator to the start of the image.
void swap(ImageBase< PixelT > &a, ImageBase< PixelT > &b)
Use (pixels & (given mask))
Extent< int, 2 > Extent2I
ImagePtr getImage(bool const noThrow=false) const
Return a (Ptr to) the MaskedImage's image.
The base class for all image classed (Image, Mask, MaskedImage, ...)
size_type size() const
Return the number of elements in the catalog.
int getX0() const
Return the image's column-origin.
VariancePtr getVariance(bool const noThrow=false) const
Return a (Ptr to) the MaskedImage's variance.
afw::table::CatalogT< PeakRecord > PeakCatalog
Represent a 2-dimensional array of bitmask pixels.
MaskPtr getMask(bool const noThrow=false) const
Return a (Ptr to) the MaskedImage's mask.
A class to manipulate images, masks, and variance as a single object.
void setMaskPlaneValues(const int plane, const int x0, const int x1, const int y)
Set the bit specified by "planeId" for pixels (x0, y) ...
int getHeight() const
Return the number of rows in the image.
details::ImageNdGetter< T, inA, inB > ndImage(ndarray::Array< T, inA, inB > const &array, afwGeom::Point2I xy0=afwGeom::Point2I())
Marks a ndarray to be interpreted as an image when applying a functor from a SpanSet.
SortedCatalogT< SourceRecord > SourceCatalog
lsst::afw::detection::Footprint Footprint
x_iterator row_begin(int y) const
Return an x_iterator to the start of the y'th row.
Use number of sigma given per-pixel s.d.
int getWidth() const
Return the number of columns in the image.
A class to represent a 2-dimensional array of pixels.
std::uint64_t FootprintIdPixel
Pixel type for FootprintSet::insertIntoImage()
int getY0() const
Return the image's row-origin.