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,
625 double includeThreshold = footprintThreshold * includeThresholdMultiplier;
627 int const row0 = img.
getY0();
628 int const col0 = img.
getX0();
635 std::vector<int> id1(width + 2);
636 std::fill(id1.begin(), id1.end(), 0);
637 std::vector<int> id2(width + 2);
638 std::fill(id2.begin(), id2.end(), 0);
639 std::vector<int>::iterator idc = id1.begin() + 1;
640 std::vector<int>::iterator idp = id2.begin() + 1;
642 std::vector<int> aliases;
643 aliases.reserve(1 + height / 20);
645 std::vector<std::shared_ptr<IdSpan>> spans;
646 spans.reserve(aliases.capacity());
648 aliases.push_back(0);
656 for (
int y = 0; y != height; ++y) {
657 if (idc == id1.begin() + 1) {
658 idc = id2.
begin() + 1;
659 idp = id1.begin() + 1;
661 idc = id1.begin() + 1;
662 idp = id2.begin() + 1;
664 std::fill_n(idc - 1, width + 2, 0);
667 bool good = (includeThresholdMultiplier == 1.0);
670 x_var_iterator varPtr = (var == NULL) ? NULL : var->
row_begin(y);
671 for (
int x = 0;
x < width; ++
x, ++pixPtr, varPtr = advancePtr(varPtr, ThresholdTraitT())) {
672 ImagePixelT
const pixVal = *pixPtr;
674 if (isBadPixel(pixVal) ||
675 !inFootprint(pixVal, varPtr, polarity, footprintThreshold, ThresholdTraitT())) {
677 auto sp = std::make_shared<IdSpan>(in_span, y, x0,
x - 1, good);
684 if (idc[
x - 1] != 0) {
686 }
else if (idp[
x - 1] != 0) {
688 }
else if (idp[
x] != 0) {
690 }
else if (idp[
x + 1] != 0) {
694 aliases.push_back(
id);
705 if (idp[
x + 1] != 0 && idp[
x + 1] !=
id) {
706 aliases[resolve_alias(aliases, idp[
x + 1])] = resolve_alias(aliases,
id);
708 idc[
x] =
id = idp[
x + 1];
711 if (!good && inFootprint(pixVal, varPtr, polarity, includeThreshold, ThresholdTraitT())) {
718 auto sp = std::make_shared<IdSpan>(in_span, y, x0, width - 1, good);
725 for (
unsigned int i = 0; i < spans.size(); i++) {
726 spans[i]->id = resolve_alias(aliases, spans[i]->
id);
731 if (spans.size() > 0) {
732 std::sort(spans.begin(), spans.end(), IdSpanCompar());
738 if (spans.size() > 0) {
741 for (
unsigned int i = 0; i <= spans.size(); i++) {
742 if (i == spans.size() || spans[i]->id != id) {
744 std::vector<geom::Span> tempSpanList;
745 for (; i0 < i; i0++) {
746 good |= spans[i0]->good;
747 tempSpanList.push_back(
748 geom::Span(spans[i0]->y + row0, spans[i0]->x0 + col0, spans[i0]->x1 + col0));
750 auto tempSpanSet = std::make_shared<geom::SpanSet>(std::move(tempSpanList));
751 auto fp = std::make_shared<Footprint>(tempSpanSet, _region);
753 if (good && fp->getArea() >=
static_cast<std::size_t
>(npixMin)) {
754 _footprints->push_back(fp);
758 if (i < spans.size()) {
767 typedef FootprintSet::FootprintList::iterator fiterator;
768 for (fiterator ptr = _footprints->begin(), end = _footprints->end(); ptr != end; ++ptr) {
769 findPeaks(*ptr, img, polarity, ThresholdTraitT());
774 template <
typename ImagePixelT>
776 int const npixMin,
bool const setPeaks)
777 : daf::base::Citizen(typeid(this)), _footprints(new FootprintList()), _region(img.getBBox()) {
778 typedef float VariancePixelT;
780 findFootprints<ImagePixelT, image::MaskPixel, VariancePixelT, ThresholdLevel_traits>(
781 _footprints.get(), _region, img, NULL, threshold.getValue(img), threshold.getIncludeMultiplier(),
782 threshold.getPolarity(), npixMin, setPeaks);
787 template <
typename MaskPixelT>
789 : daf::base::Citizen(typeid(this)), _footprints(new FootprintList()), _region(msk.getBBox()) {
790 switch (threshold.getType()) {
792 findFootprints<MaskPixelT, MaskPixelT, float, ThresholdBitmask_traits>(
793 _footprints.get(), _region, msk, NULL, threshold.getValue(),
794 threshold.getIncludeMultiplier(), threshold.getPolarity(), npixMin,
false);
798 findFootprints<MaskPixelT, MaskPixelT, float, ThresholdLevel_traits>(
799 _footprints.get(), _region, msk, NULL, threshold.getValue(),
800 threshold.getIncludeMultiplier(), threshold.getPolarity(), npixMin,
false);
804 throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
805 "You must specify a numerical threshold value with a Mask");
809 template <
typename ImagePixelT,
typename MaskPixelT>
811 Threshold
const &threshold, std::string
const &planeName,
int const npixMin,
813 : daf::base::Citizen(typeid(this)),
814 _footprints(new FootprintList()),
815 _region(geom::
Point2I(maskedImg.getX0(), maskedImg.getY0()),
816 geom::
Extent2I(maskedImg.getWidth(), maskedImg.getHeight())) {
819 switch (threshold.getType()) {
821 findFootprints<ImagePixelT, MaskPixelT, VariancePixelT, ThresholdPixelLevel_traits>(
823 threshold.getValue(maskedImg), threshold.getIncludeMultiplier(), threshold.getPolarity(),
827 findFootprints<ImagePixelT, MaskPixelT, VariancePixelT, ThresholdLevel_traits>(
829 threshold.getValue(maskedImg), threshold.getIncludeMultiplier(), threshold.getPolarity(),
834 if (planeName ==
"") {
840 const std::shared_ptr<image::Mask<MaskPixelT>>
mask = maskedImg.
getMask();
841 mask->addMaskPlane(planeName);
843 MaskPixelT
const bitPlane = mask->getPlaneBitMask(planeName);
847 for (
auto const &fIter : *_footprints) {
848 fIter->getSpans()->setMask(*(maskedImg.
getMask()), bitPlane);
873 template <
typename MaskPixelT>
877 ~Startspan() {
delete _span; }
879 bool getSpan() {
return _span; }
880 bool Stop() {
return _stop; }
881 DIRECTION getDirection() {
return _direction; }
883 static int detectedPlane;
884 static int stopPlane;
886 std::shared_ptr<geom::Span const> _span;
887 DIRECTION _direction;
891 template <
typename MaskPixelT>
892 Startspan<MaskPixelT>::Startspan(
893 geom::Span
const *span,
897 : _span(span), _direction(dir), _stop(false) {
901 int const y = span->getY() - mask->
getY0();
902 for (
int x = span->getX0() - mask->
getX0();
x <= span->getX1() - mask->
getX0();
x++) {
903 if (
mask(
x, y, stopPlane)) {
911 template <
typename ImagePixelT,
typename MaskPixelT>
915 : _image(image->getImage()), _mask(image->getMask()) {}
918 bool add(geom::Span *span, DIRECTION
const dir,
bool addToMask =
true);
920 Threshold
const &threshold,
921 double const param = -1);
925 std::vector<std::shared_ptr<Startspan<MaskPixelT>>> _spans;
931 template <
typename ImagePixelT,
typename MaskPixelT>
932 bool StartspanSet<ImagePixelT, MaskPixelT>::add(geom::Span *span,
935 if (dir == RESTART) {
936 if (add(span, UP) || add(span, DOWN,
false)) {
940 auto sspan = std::make_shared<MaskPixelT>(span, dir);
944 _spans.push_back(sspan);
959 template <
typename ImagePixelT,
typename MaskPixelT>
960 bool StartspanSet<ImagePixelT, MaskPixelT>::process(
Footprint *fp,
961 Threshold
const &threshold,
964 int const row0 = _image->
getY0();
965 int const col0 = _image->getOffsetCols();
966 int const height = _image->getHeight();
968 Startspan<MaskPixelT> *sspan = NULL;
969 for (
auto iter = _spans.begin(); iter != _spans.end(); iter++) {
971 if (sspan->getDirection() != DONE) {
978 if (sspan == NULL || sspan->getDirection() == DONE) {
987 DIRECTION
const dir = sspan->getDirection();
991 int x0 = sspan->getSpan()->getX0() - col0;
996 int const di = (dir == UP) ? 1 : -1;
1000 double const thresholdVal = threshold.getValue(param);
1001 bool const polarity = threshold.getPolarity();
1003 for (
int i = sspan->span->y - row0 + di; i < height && i >= 0; i += di) {
1004 pixAccessT imgRow = _image->origin().advance(0, i);
1012 for (
int j = x0 - 1; j >= -1; j--) {
1013 ImagePixelT pixVal = (j < 0) ? thresholdVal - 100 : (polarity ? imgRow[j] : -imgRow[j]);
1014 if (_mask(j, i, Startspan<MaskPixelT>::detectedPlane) || pixVal < threshold) {
1029 for (
int j = nx0 + 1; j <= width; j++) {
1030 ImagePixelT pixVal = (j >= width) ? threshold - 100 :
1031 (polarity ? (F32 ? imgRowF32[j] : imgRowS32[j]) :
1032 (F32 ? -imgRowF32[j] : -imgRowS32[j]));
1033 if ((maskRow[j] & DETECTED) || pixVal < threshold) {
1039 pmSpan
const *sp = pmFootprintAddSpan(fp, i + row0, nx0 + col0, nx1 + col0);
1041 if (add_startspan(startspans, sp, mask, RESTART)) {
1057 for (
int j = nx1 + 1; j <= x1 + 1; j++) {
1058 ImagePixelT pixVal = (j >= width) ? threshold - 100 :
1059 (polarity ? (F32 ? imgRowF32[j] : imgRowS32[j]) : (F32 ? -imgRowF32[j] : -imgRowS32[j]));
1060 if (!(maskRow[j] & DETECTED) && pixVal >= threshold) {
1063 for (; j <= width; j++) {
1064 ImagePixelT pixVal = (j >= width) ? threshold - 100 :
1065 (polarity ? (F32 ? imgRowF32[j] : imgRowS32[j]) :
1066 (F32 ? -imgRowF32[j] : -imgRowS32[j]));
1067 if ((maskRow[j] & DETECTED) || pixVal < threshold) {
1077 sp = pmFootprintAddSpan(fp, i + row0, sx0 + col0, sx1 + col0 - 1);
1078 if (add_startspan(startspans, sp, mask, DONE)) {
1083 sp = pmFootprintAddSpan(fp, i + row0, sx0 + col0, x1 + col0);
1084 if (add_startspan(startspans, sp, mask, DONE)) {
1088 sp = pmFootprintAddSpan(fp, i + row0, x1 + 1 + col0, sx1 + col0 - 1);
1089 if (add_startspan(startspans, sp, mask, RESTART)) {
1096 sp = pmFootprintAddSpan(fp, i + row0, sx0 + col0, sx1 + col0 - 1);
1097 if (add_startspan(startspans, sp, mask, RESTART)) {
1105 if (stop || first ==
false) {
1117 sspan->_direction = DONE;
1118 return stop ?
false :
true;
1139 pmFindFootprintAtPoint(psImage
const *img,
1140 Threshold
const &threshold,
1141 psArray
const *peaks,
1143 assert(img != NULL);
1146 if (img->type.type == PS_TYPE_F32) {
1148 }
else if (img->type.type == PS_TYPE_S32) {
1151 psError(PS_ERR_UNKNOWN,
true,
"Unsupported psImage type: %d", img->type.type);
1154 psF32 *imgRowF32 = NULL;
1155 psS32 *imgRowS32 = NULL;
1157 int const row0 = img->row0;
1158 int const col0 = img->col0;
1159 int const height = img->getHeight();
1160 int const width = img->getWidth();
1166 if (row < 0 || row >= height ||
1167 col < 0 || col >= width) {
1168 psError(PS_ERR_BAD_PARAMETER_VALUE,
true,
1169 "row/col == (%d, %d) are out of bounds [%d--%d, %d--%d]",
1170 row + row0, col + col0, row0, row0 + height - 1, col0, col0 + width - 1);
1174 ImagePixelT pixVal = F32 ? img->data.F32[row][col] : img->data.S32[row][col];
1175 if (pixVal < threshold) {
1176 return pmFootprintAlloc(0, img);
1179 pmFootprint *fp = pmFootprintAlloc(1 + img->getHeight()/10, img);
1185 psImage *mask = psImageAlloc(width, height, PS_TYPE_MASK);
1186 P_PSIMAGE_SET_ROW0(mask, row0);
1187 P_PSIMAGE_SET_COL0(mask, col0);
1188 psImageInit(mask, INITIAL);
1192 assert (peaks == NULL || peaks->n == 0 || pmIsPeak(peaks->data[0]));
1193 if (peaks != NULL) {
1194 for (
int i = 0; i < peaks->n; i++) {
1195 pmPeak *peak = peaks->data[i];
1196 mask->data.PS_TYPE_MASK_DATA[peak->y - mask->row0][peak->x - mask->col0] |= STOP;
1202 psArray *startspans = psArrayAllocEmpty(1);
1204 imgRowF32 = img->data.F32[row];
1205 imgRowS32 = img->data.S32[row];
1206 psMaskType *maskRow = mask->data.PS_TYPE_MASK_DATA[row];
1209 for (i = col; i >= 0; i--) {
1210 pixVal = F32 ? imgRowF32[i] : imgRowS32[i];
1211 if ((maskRow[i] & DETECTED) || pixVal < threshold) {
1216 for (i = col; i < width; i++) {
1217 pixVal = F32 ? imgRowF32[i] : imgRowS32[i];
1218 if ((maskRow[i] & DETECTED) || pixVal < threshold) {
1223 pmSpan
const *sp = pmFootprintAddSpan(fp, row + row0, i0 + col0 + 1, i1 + col0 - 1);
1225 (void)add_startspan(startspans, sp, mask, RESTART);
1230 while (do_startspan(fp, img, mask, threshold, startspans))
continue;
1241 FootprintSet::FootprintSet(geom::Box2I region)
1242 : daf::base::Citizen(typeid(this)), _footprints(std::make_shared<FootprintList>()), _region(region) {}
1244 FootprintSet::FootprintSet(FootprintSet
const &rhs)
1245 : daf::base::Citizen(typeid(this)), _footprints(new FootprintList), _region(rhs._region) {
1246 _footprints->reserve(rhs._footprints->size());
1247 for (FootprintSet::FootprintList::const_iterator ptr = rhs._footprints->begin(),
1248 end = rhs._footprints->end();
1249 ptr != end; ++ptr) {
1250 _footprints->push_back(std::make_shared<Footprint>(**ptr));
1254 FootprintSet &FootprintSet::operator=(FootprintSet
const &rhs) {
1255 FootprintSet tmp(rhs);
1260 void FootprintSet::merge(FootprintSet
const &rhs,
int tGrow,
int rGrow,
bool isotropic) {
1261 FootprintControl
const ctrl(
true, isotropic);
1262 FootprintSet fs = mergeFootprintSets(*
this, tGrow, rhs, rGrow, ctrl);
1266 void FootprintSet::setRegion(geom::Box2I
const ®ion) {
1269 for (FootprintSet::FootprintList::iterator ptr = _footprints->begin(), end = _footprints->end();
1270 ptr != end; ++ptr) {
1271 (*ptr)->setRegion(region);
1275 FootprintSet::FootprintSet(FootprintSet
const &rhs,
int r,
bool isotropic)
1276 : daf::base::Citizen(typeid(this)), _footprints(new FootprintList), _region(rhs._region) {
1278 FootprintSet fs = rhs;
1282 throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
1283 (boost::format(
"I cannot grow by negative numbers: %d") % r).str());
1286 FootprintControl
const ctrl(
true, isotropic);
1287 FootprintSet fs = mergeFootprintSets(FootprintSet(rhs.getRegion()), 0, rhs, r, ctrl);
1291 FootprintSet::FootprintSet(FootprintSet
const &rhs,
int ngrow, FootprintControl
const &ctrl)
1292 : daf::base::Citizen(typeid(this)), _footprints(new FootprintList), _region(rhs._region) {
1294 FootprintSet fs = rhs;
1297 }
else if (ngrow < 0) {
1298 throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
1299 str(boost::format(
"I cannot grow by negative numbers: %d") % ngrow));
1302 FootprintSet fs = mergeFootprintSets(FootprintSet(rhs.getRegion()), 0, rhs, ngrow, ctrl);
1306 FootprintSet::FootprintSet(FootprintSet
const &fs1, FootprintSet
const &fs2,
bool const)
1307 : daf::base::Citizen(typeid(this)), _footprints(new FootprintList()), _region(fs1._region) {
1308 _region.include(fs2._region);
1309 throw LSST_EXCEPT(pex::exceptions::LogicError,
"NOT IMPLEMENTED");
1312 std::shared_ptr<image::Image<FootprintIdPixel>> FootprintSet::insertIntoImage(
bool const relativeIDs)
const {
1313 auto im = std::make_shared<image::Image<FootprintIdPixel>>(_region);
1317 for (
auto const &fIter : *_footprints) {
1321 id = fIter->getId();
1324 fIter->getSpans()->applyFunctor(setIdImage<FootprintIdPixel>(
id), *im);
1330 template <
typename ImagePixelT,
typename MaskPixelT>
1332 HeavyFootprintCtrl
const *ctrl) {
1333 HeavyFootprintCtrl ctrl_s = HeavyFootprintCtrl();
1339 for (FootprintList::iterator ptr = _footprints->begin(), end = _footprints->end(); ptr != end; ++ptr) {
1340 ptr->reset(
new HeavyFootprint<ImagePixelT, MaskPixelT>(**ptr, mimg, ctrl));
1345 for (FootprintList::const_iterator i = _footprints->begin(); i != _footprints->end(); ++i) {
1346 std::shared_ptr<afw::table::SourceRecord> r = cat.addNew();
1347 r->setFootprint(*i);
1357 #define INSTANTIATE(PIXEL) \
1358 template FootprintSet::FootprintSet(image::Image<PIXEL> const &, Threshold const &, int const, \
1360 template FootprintSet::FootprintSet(image::MaskedImage<PIXEL, image::MaskPixel> const &, \
1361 Threshold const &, std::string const &, int const, bool const); \
1362 template void FootprintSet::makeHeavy(image::MaskedImage<PIXEL, image::MaskPixel> const &, \
1363 HeavyFootprintCtrl const *)
iterator begin() const
Return an STL compliant iterator to the start of the image.
void swap(ImageBase< PixelT > &a, ImageBase< PixelT > &b)
VariancePtr getVariance() const
Return a (shared_ptr to) the MaskedImage's variance.
Use (pixels & (given mask))
Extent< int, 2 > Extent2I
ImagePtr getImage() const
Return a (shared_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.
MaskPtr getMask() const
Return a (shared_ptr to) the MaskedImage's mask.
afw::table::CatalogT< PeakRecord > PeakCatalog
Represent a 2-dimensional array of bitmask pixels.
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.