lsst.meas.base  14.0-12-g233aa8e+6
NaiveCentroid.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 /*
3  * LSST Data Management System
4  * Copyright 2008-2013 LSST Corporation.
5  *
6  * This product includes software developed by the
7  * LSST Project (http://www.lsst.org/).
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the LSST License Statement and
20  * the GNU General Public License along with this program. If not,
21  * see <http://www.lsstcorp.org/LegalNotices/>.
22  */
23 #include "ndarray/eigen.h"
24 #include "lsst/afw/table/Source.h"
26 
27 
28 namespace lsst { namespace meas { namespace base {
29 namespace {
30 FlagDefinitionList flagDefinitions;
31 } // end anonymous
32 
33 FlagDefinition const NaiveCentroidAlgorithm::FAILURE = flagDefinitions.addFailureFlag();
34 FlagDefinition const NaiveCentroidAlgorithm::NO_COUNTS = flagDefinitions.add("flag_noCounts", "Object to be centroided has no counts");
35 FlagDefinition const NaiveCentroidAlgorithm::EDGE = flagDefinitions.add("flag_edge", "Object too close to edge");
36 
38  return flagDefinitions;
39 }
40 
42  Control const & ctrl,
43  std::string const & name,
44  afw::table::Schema & schema
45 ) : _ctrl(ctrl),
46  _centroidKey(
47  CentroidResultKey::addFields(schema, name, "centroid from Naive Centroid algorithm", NO_UNCERTAINTY)
48  ),
49  _flagHandler(FlagHandler::addFields(schema, name,
51  _centroidExtractor(schema, name, true),
52  _centroidChecker(schema, name, ctrl.doFootprintCheck, ctrl.maxDistToPeak)
53 {
54 }
55 
57  afw::table::SourceRecord & measRecord,
58  afw::image::Exposure<float> const & exposure
59 ) const {
60 
61  afw::geom::Point2D center = _centroidExtractor(measRecord, _flagHandler);
62  CentroidResult result;
63  result.x = center.getX();
64  result.y = center.getY();
65  measRecord.set(_centroidKey, result); // better than NaN
66 
67  typedef afw::image::Image<float> ImageT;
68  ImageT const& image = *exposure.getMaskedImage().getImage();
69 
70  int x = center.getX(); // FIXME: this is different from GaussianCentroid and SdssCentroid here,
71  int y = center.getY(); // and probably shouldn't be.
72 
73  x -= image.getX0(); // work in image Pixel coordinates
74  y -= image.getY0();
75 
76  if (x < 1 || x >= image.getWidth() - 1 || y < 1 || y >= image.getHeight() - 1) {
77 
78  throw LSST_EXCEPT(
80  EDGE.doc,
81  EDGE.number
82  );
83  }
84 
85  ImageT::xy_locator im = image.xy_at(x, y);
86 
87  double const sum =
88  (im(-1, 1) + im( 0, 1) + im( 1, 1) +
89  im(-1, 0) + im( 0, 0) + im( 1, 0) +
90  im(-1, -1) + im( 0, -1) + im( 1, -1))
91  - 9 * _ctrl.background;
92 
93  if (sum == 0.0) {
94  throw LSST_EXCEPT(
96  NO_COUNTS.doc,
98  );
99  }
100 
101  double const sum_x =
102  -im(-1, 1) + im( 1, 1) +
103  -im(-1, 0) + im( 1, 0) +
104  -im(-1, -1) + im( 1, -1);
105  double const sum_y =
106  (im(-1, 1) + im( 0, 1) + im( 1, 1)) -
107  (im(-1, -1) + im( 0, -1) + im( 1, -1));
108 
109  result.x = lsst::afw::image::indexToPosition(x + image.getX0()) + sum_x / sum;
110  result.y = lsst::afw::image::indexToPosition(y + image.getY0()) + sum_y / sum;
111  measRecord.set(_centroidKey, result);
112  _centroidChecker(measRecord);
113 }
114 
115 
117  _flagHandler.handleFailure(measRecord, error);
118 }
119 
121  Control const & ctrl,
122  std::string const & name,
123  afw::table::SchemaMapper & mapper
124 ) :
125  CentroidTransform{name, mapper}
126 {
129  if (flag == NaiveCentroidAlgorithm::FAILURE) continue;
130  if (mapper.getInputSchema().getNames().count(
131  mapper.getInputSchema().join(name, flag.name)) == 0) continue;
132  afw::table::Key<afw::table::Flag> key = mapper.getInputSchema().find<afw::table::Flag>(
133  name + "_" + flag.name).key;
134  mapper.addMapping(key);
135  }
136 }
137 
138 }}} // namespace lsst::meas::base
139 
140 
int y
std::size_t size() const
return the current size (number of defined elements) of the collection
Definition: FlagHandler.h:133
NaiveCentroidAlgorithm(Control const &ctrl, std::string const &name, afw::table::Schema &schema)
afw::table::Key< afw::table::Array< ImagePixelT > > image
double indexToPosition(double ind)
Simple class used to define and document flags The name and doc constitute the identity of the FlagDe...
Definition: FlagHandler.h:38
A reusable struct for centroid measurements.
static FlagDefinition const EDGE
Definition: NaiveCentroid.h:73
CentroidElement x
x (column) coordinate of the measured position
Exception to be thrown when a measurement algorithm experiences a known failure mode.
Definition: exceptions.h:48
string name
STL class.
NaiveCentroidTransform(Control const &ctrl, std::string const &name, afw::table::SchemaMapper &mapper)
Utility class for handling flag fields that indicate the failure modes of an algorithm.
Definition: FlagHandler.h:156
virtual void fail(afw::table::SourceRecord &measRecord, MeasurementError *error=nullptr) const
Handle an exception thrown by the current algorithm by setting flags in the given record...
virtual void measure(afw::table::SourceRecord &measRecord, afw::image::Exposure< float > const &exposure) const
Called to measure a single child source in an image.
MaskedImageT getMaskedImage()
static FlagDefinitionList const & getFlagDefinitions()
static FlagDefinition const NO_COUNTS
Definition: NaiveCentroid.h:72
int x
#define LSST_EXCEPT(type,...)
Algorithm provides no uncertainy information at all.
Definition: constants.h:42
double background
"Value to subtract from the image pixel values" ;
Definition: NaiveCentroid.h:44
void handleFailure(afw::table::BaseRecord &record, MeasurementError const *error=nullptr) const
Handle an expected or unexpected Exception thrown by a measurement algorithm.
Definition: FlagHandler.cc:93
A FunctorKey for CentroidResult.
void set(Key< T > const &key, U const &value)
CentroidElement y
y (row) coordinate of the measured position
A C++ control class to handle NaiveCentroidAlgorithm&#39;s configuration.
Definition: NaiveCentroid.h:42
vector-type utility class to build a collection of FlagDefinitions
Definition: FlagHandler.h:63
static FlagDefinition const FAILURE
Definition: NaiveCentroid.h:71
Base for centroid measurement transformations.