SourceXtractorPlusPlus  0.15
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FluxMeasurement.cpp
Go to the documentation of this file.
1 
17 /*
18  * FluxMeasurement.cpp
19  *
20  * Created on: Oct 19, 2018
21  * Author: Alejandro Alvarez
22  */
23 
26 
27 namespace SourceXtractor {
28 
30 
32 getMirrorPixel(SeFloat centroid_x, SeFloat centroid_y,
33  PixelCoordinate min_pixel, int pixel_x, int pixel_y,
34  const ImageChunk<SeFloat>& img,
35  const ImageChunk<SeFloat>& variance_map,
36  SeFloat variance_threshold) {
37  centroid_x -= min_pixel.m_x;
38  centroid_y -= min_pixel.m_y;
39  auto mirror_x = 2 * centroid_x - pixel_x + 0.49999;
40  auto mirror_y = 2 * centroid_y - pixel_y + 0.49999;
41  if (img.isInside(mirror_x, mirror_y)) {
42  auto variance_tmp = variance_map.getValue(mirror_x, mirror_y);
43  if (variance_tmp < variance_threshold) {
44  // mirror pixel is OK: take the value
45  return std::make_pair(img.getValue(mirror_x, mirror_y), variance_tmp);
46  }
47  }
48  return std::make_pair(0., 0.);
49 }
50 
51 FluxMeasurement measureFlux(const std::shared_ptr<Aperture> &aperture, SeFloat centroid_x, SeFloat centroid_y,
52  const std::shared_ptr<Image<SeFloat>> &img,
53  const std::shared_ptr<Image<SeFloat>> &variance_map, SeFloat variance_threshold,
54  bool use_symmetry) {
55  FluxMeasurement measurement;
56 
57  auto min_pixel = aperture->getMinPixel(centroid_x, centroid_y);
58  auto max_pixel = aperture->getMaxPixel(centroid_x, centroid_y);
59 
60  // Skip if the full source is outside the frame
61  if (max_pixel.m_x < 0 || max_pixel.m_y < 0 || min_pixel.m_x >= img->getWidth() ||
62  min_pixel.m_y >= img->getHeight()) {
63  measurement.m_flags = Flags::OUTSIDE;
64  return measurement;
65  }
66 
67  // Clip to the image and recenter centroid to the cut
68  if (min_pixel.clip(img->getWidth(), img->getHeight()))
69  measurement.m_flags |= Flags::BOUNDARY;
70  if (max_pixel.clip(img->getWidth(), img->getHeight()))
71  measurement.m_flags |= Flags::BOUNDARY;
72 
73  // Cutout
74  auto img_cutout = img->getChunk(min_pixel, max_pixel);
75  auto var_cutout = variance_map->getChunk(min_pixel, max_pixel);
76 
77  // iterate over the aperture pixels
78  for (int pixel_y = 0; pixel_y < img_cutout->getHeight(); pixel_y++) {
79  for (int pixel_x = 0; pixel_x < img_cutout->getWidth(); pixel_x++) {
80  SeFloat pixel_value = 0;
81  SeFloat pixel_variance = 0;
82 
83  // get the area coverage and continue if there is overlap
84  auto area = aperture->getArea(centroid_x, centroid_y, min_pixel.m_x + pixel_x,
85  min_pixel.m_y + pixel_y);
86  if (area == 0) {
87  continue;
88  }
89 
90  measurement.m_total_area += area;
91 
92  SeFloat variance_tmp = var_cutout->getValue(pixel_x, pixel_y);
93  if (variance_tmp > variance_threshold) {
94  measurement.m_bad_area += 1;
95  if (use_symmetry) {
96  std::tie(pixel_value, pixel_variance) = getMirrorPixel(
97  centroid_x, centroid_y, min_pixel, pixel_x, pixel_y, *img_cutout, *var_cutout,
98  variance_threshold
99  );
100  }
101  }
102  else {
103  pixel_value = img_cutout->getValue(pixel_x, pixel_y);
104  pixel_variance = variance_tmp;
105  }
106 
107  measurement.m_flux += pixel_value * area;
108  measurement.m_variance += pixel_variance * area;
109  }
110  }
111 
112  // check/set the bad area flag
113  bool is_biased = measurement.m_total_area > 0 && measurement.m_bad_area / measurement.m_total_area > BADAREA_THRESHOLD_APER;
114  measurement.m_flags |= Flags::BIASED * is_biased;
115  return measurement;
116 }
117 
118 } // end SourceXtractor
T tie(T...args)
The object is completely outside of the measurement frame.
SeFloat32 SeFloat
Definition: Types.h:32
T getValue(int x, int y) const
Returns the value of the pixel with the coordinates (x,y)
Definition: ImageChunk.h:56
static std::tuple< SeFloat, SeFloat > getMirrorPixel(SeFloat centroid_x, SeFloat centroid_y, PixelCoordinate min_pixel, int pixel_x, int pixel_y, const ImageChunk< SeFloat > &img, const ImageChunk< SeFloat > &variance_map, SeFloat variance_threshold)
The object is truncated (too close to an image boundary)
T make_pair(T...args)
The object has bad pixels.
A pixel coordinate made of two integers m_x and m_y.
bool isInside(int x, int y) const
Returns true if the given coordinates are inside the image bounds.
Definition: Image.h:72
Interface representing an image.
Definition: Image.h:43
const SeFloat BADAREA_THRESHOLD_APER
Definition: Flagging.cpp:25
FluxMeasurement measureFlux(const std::shared_ptr< Aperture > &aperture, SeFloat centroid_x, SeFloat centroid_y, const std::shared_ptr< Image< SeFloat >> &img, const std::shared_ptr< Image< SeFloat >> &variance_map, SeFloat variance_threshold, bool use_symmetry)