LSST Applications  21.0.0+04719a4bac,21.0.0-1-ga51b5d4+ae94e5adf4,21.0.0-10-g2408eff+ad7fe00a3b,21.0.0-10-g560fb7b+5d30037bff,21.0.0-10-gcf60f90+7fd8e8fd04,21.0.0-11-g25eff31+491f1498e8,21.0.0-11-gd78879e+d13a45ff19,21.0.0-12-g1e69a3f+69d54d99d8,21.0.0-17-g6590b197+c8c705a94e,21.0.0-2-g103fe59+29086b68f8,21.0.0-2-g1367e85+d793a9824f,21.0.0-2-g45278ab+04719a4bac,21.0.0-2-g5242d73+d793a9824f,21.0.0-2-g7f82c8f+7178d1fb8b,21.0.0-2-g8f08a60+fd0b970de5,21.0.0-2-g8faa9b5+3b24369756,21.0.0-2-ga326454+7178d1fb8b,21.0.0-2-gde069b7+ca45a81b40,21.0.0-2-gecfae73+3609a557ba,21.0.0-2-gfc62afb+d793a9824f,21.0.0-22-g2a5702db6+f385fa6f38,21.0.0-3-g357aad2+673ab9f056,21.0.0-3-g4be5c26+d793a9824f,21.0.0-3-g65f322c+45176dc65e,21.0.0-3-g7d9da8d+3b24369756,21.0.0-3-ge02ed75+d05e6d1be4,21.0.0-4-g591bb35+d05e6d1be4,21.0.0-4-g65b4814+5d30037bff,21.0.0-4-gccdca77+a631590478,21.0.0-4-ge8a399c+7f1b116a8b,21.0.0-5-gb7b9a9f+d793a9824f,21.0.0-5-gd00fb1e+de3bd29da1,21.0.0-55-g0be6b205+66ae927d20,21.0.0-6-g2d4f3f3+04719a4bac,21.0.0-7-g04766d7+510a52a951,21.0.0-7-g98eecf7+adb4d61a8d,21.0.0-9-g39e06b5+d05e6d1be4,master-gac4afde19b+d05e6d1be4,w.2021.12
LSST Data Management Base Package
saturated.cc
Go to the documentation of this file.
1 /*
2  * Handle saturated pixels when making colour images
3  */
4 #include <cmath>
5 #include "boost/format.hpp"
6 #include "lsst/afw/detection.h"
8 #include "rgb/Rgb.h"
9 
10 namespace lsst {
11 namespace afw {
12 namespace display {
13 
14 namespace {
15 template <typename ImageT>
16 class SetPixels {
17 public:
18  explicit SetPixels() : _value(0) {}
19 
20  void setValue(float value) { _value = value; }
21 
22  void operator()(lsst::geom::Point2I const& point, typename ImageT::Pixel& arrayInput) { arrayInput = _value; }
23 
24 private:
25  float _value;
26 };
27 }
28 
29 template <typename ImageT>
30 void replaceSaturatedPixels(ImageT& rim, // R image (e.g. i)
31  ImageT& gim, // G image (e.g. r)
32  ImageT& bim, // B image (e.g. g)
33  int borderWidth, // width of border used to estimate colour of saturated regions
34  float saturatedPixelValue // the brightness of a saturated pixel, once fixed
35  ) {
36  int const width = rim.getWidth(), height = rim.getHeight();
37  int const x0 = rim.getX0(), y0 = rim.getY0();
38 
39  if (width != gim.getWidth() || height != gim.getHeight() || x0 != gim.getX0() || y0 != gim.getY0()) {
40  throw LSST_EXCEPT(
42  str(boost::format("R image has different size/origin from G image "
43  "(%dx%d+%d+%d v. %dx%d+%d+%d") %
44  width % height % x0 % y0 % gim.getWidth() % gim.getHeight() % gim.getX0() % gim.getY0()));
45  }
46  if (width != bim.getWidth() || height != bim.getHeight() || x0 != bim.getX0() || y0 != bim.getY0()) {
47  throw LSST_EXCEPT(
49  str(boost::format("R image has different size/origin from B image "
50  "(%dx%d+%d+%d v. %dx%d+%d+%d") %
51  width % height % x0 % y0 % bim.getWidth() % bim.getHeight() % bim.getX0() % bim.getY0()));
52  }
53 
54  bool const useMaxPixel = !std::isfinite(saturatedPixelValue);
55 
56  SetPixels<typename ImageT::Image> setR, setG, setB; // functors used to set pixel values
57 
58  // Find all the saturated pixels in any of the three image
59  int const npixMin = 1; // minimum number of pixels in an object
60  afw::image::MaskPixel const SAT = rim.getMask()->getPlaneBitMask("SAT");
62 
63  detection::FootprintSet sat(*rim.getMask(), satThresh, npixMin);
64  sat.merge(detection::FootprintSet(*gim.getMask(), satThresh, npixMin));
65  sat.merge(detection::FootprintSet(*bim.getMask(), satThresh, npixMin));
66  // go through the list of saturated regions, determining the mean colour of the surrounding pixels
67  typedef detection::FootprintSet::FootprintList FootprintList;
69  for (FootprintList::const_iterator ptr = feet->begin(), end = feet->end(); ptr != end; ++ptr) {
71  auto const bigFoot = std::make_shared<detection::Footprint>(foot->getSpans()->dilated(borderWidth),
72  foot->getRegion());
73 
74  double sumR = 0, sumG = 0, sumB = 0; // sum of all non-saturated adjoining pixels
75  double maxR = 0, maxG = 0, maxB = 0; // maximum of non-saturated adjoining pixels
76 
77  for (auto span = bigFoot->getSpans()->begin(), send = bigFoot->getSpans()->end(); span != send;
78  ++span) {
79  int const y = span->getY() - y0;
80  if (y < 0 || y >= height) {
81  continue;
82  }
83  int sx0 = span->getX0() - x0;
84  if (sx0 < 0) {
85  sx0 = 0;
86  }
87  int sx1 = span->getX1() - x0;
88  if (sx1 >= width) {
89  sx1 = width - 1;
90  }
91 
92  for (typename ImageT::iterator rptr = rim.at(sx0, y), rend = rim.at(sx1 + 1, y),
93  gptr = gim.at(sx0, y), bptr = bim.at(sx0, y);
94  rptr != rend; ++rptr, ++gptr, ++bptr) {
95  if (!((rptr.mask() | gptr.mask() | bptr.mask()) & SAT)) {
96  float val = rptr.image();
97  sumR += val;
98  if (val > maxR) {
99  maxR = val;
100  }
101 
102  val = gptr.image();
103  sumG += val;
104  if (val > maxG) {
105  maxG = val;
106  }
107 
108  val = bptr.image();
109  sumB += val;
110  if (val > maxB) {
111  maxB = val;
112  }
113  }
114  }
115  }
116  // OK, we have the mean fluxes for the pixels surrounding this set of saturated pixels
117  // so we can figure out the proper values to use for the saturated ones
118  float R = 0, G = 0, B = 0; // mean intensities
119  if (sumR + sumB + sumG > 0) {
120  if (sumR > sumG) {
121  if (sumR > sumB) {
122  R = useMaxPixel ? maxR : saturatedPixelValue;
123 
124  G = (R * sumG) / sumR;
125  B = (R * sumB) / sumR;
126  } else {
127  B = useMaxPixel ? maxB : saturatedPixelValue;
128  R = (B * sumR) / sumB;
129  G = (B * sumG) / sumB;
130  }
131  } else {
132  if (sumG > sumB) {
133  G = useMaxPixel ? maxG : saturatedPixelValue;
134  R = (G * sumR) / sumG;
135  B = (G * sumB) / sumG;
136  } else {
137  B = useMaxPixel ? maxB : saturatedPixelValue;
138  R = (B * sumR) / sumB;
139  G = (B * sumG) / sumB;
140  }
141  }
142  }
143  // Now that we know R, G, and B we can fix the values
144  setR.setValue(R);
145  foot->getSpans()->applyFunctor(setR, *rim.getImage());
146  setG.setValue(G);
147  foot->getSpans()->applyFunctor(setG, *gim.getImage());
148  setB.setValue(B);
149  foot->getSpans()->applyFunctor(setB, *bim.getImage());
150  }
151 }
152 
154  image::MaskedImage<float>& bim, int borderWidth,
155  float saturatedPixelValue);
156 }
157 }
158 }
int end
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
uint64_t * ptr
Definition: RangeSet.cc:88
int y
Definition: SpanSet.cc:49
A set of Footprints, associated with a MaskedImage.
Definition: FootprintSet.h:53
std::shared_ptr< FootprintList > getFootprints()
: Return the Footprints of detected objects
Definition: FootprintSet.h:156
void merge(FootprintSet const &rhs, int tGrow=0, int rGrow=0, bool isotropic=true)
Merge a FootprintSet into *this.
A Threshold is used to pass a threshold value to detection algorithms.
Definition: Threshold.h:43
@ BITMASK
Use (pixels & (given mask))
Definition: Threshold.h:48
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:73
Reports invalid arguments.
Definition: Runtime.h:66
T isfinite(T... args)
void replaceSaturatedPixels(ImageT &rim, ImageT &gim, ImageT &bim, int borderWidth=2, float saturatedPixelValue=65535)
Definition: saturated.cc:30
float Pixel
Typedefs to be used for pixel values.
Definition: common.h:37
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
A base class for image defects.
ImageT val
Definition: CR.cc:146