LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
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 
30 namespace {
31 
32 boost::array<FlagDefinition,NaiveCentroidAlgorithm::N_FLAGS> const & getFlagDefinitions() {
33  static boost::array<FlagDefinition,NaiveCentroidAlgorithm::N_FLAGS> const flagDefs = {{
34  {"flag", "general failure flag, set if anything went wrong"},
35  {"flag_noCounts", "Object to be centroided has no counts"},
36  {"flag_edge", "Object too close to edge"}
37  }};
38  return flagDefs;
39 }
40 
41 } // anonymous
42 
44  Control const & ctrl,
45  std::string const & name,
47 ) : _ctrl(ctrl),
48  _centroidKey(
49  CentroidResultKey::addFields(schema, name, "centroid from Naive Centroid algorithm", NO_UNCERTAINTY)
50  ),
51  _centroidExtractor(schema, name, true)
52 {
53  _flagHandler = FlagHandler::addFields(schema, name,
54  getFlagDefinitions().begin(), getFlagDefinitions().end());
55 }
56 
58  afw::table::SourceRecord & measRecord,
59  afw::image::Exposure<float> const & exposure
60 ) const {
61 
63  CentroidResult result;
64  result.x = center.getX();
65  result.y = center.getY();
66  measRecord.set(_centroidKey, result); // better than NaN
67 
68  typedef afw::image::Image<float> ImageT;
69  ImageT const& image = *exposure.getMaskedImage().getImage();
70 
71  int x = center.getX(); // FIXME: this is different from GaussianCentroid and SdssCentroid here,
72  int y = center.getY(); // and probably shouldn't be.
73 
74  x -= image.getX0(); // work in image Pixel coordinates
75  y -= image.getY0();
76 
77  if (x < 1 || x >= image.getWidth() - 1 || y < 1 || y >= image.getHeight() - 1) {
78 
79  throw LSST_EXCEPT(
82  EDGE
83  );
84  }
85 
86  ImageT::xy_locator im = image.xy_at(x, y);
87 
88  double const sum =
89  (im(-1, 1) + im( 0, 1) + im( 1, 1) +
90  im(-1, 0) + im( 0, 0) + im( 1, 0) +
91  im(-1, -1) + im( 0, -1) + im( 1, -1))
92  - 9 * _ctrl.background;
93 
94  if (sum == 0.0) {
95  throw LSST_EXCEPT(
98  NO_COUNTS
99  );
100  }
101 
102  double const sum_x =
103  -im(-1, 1) + im( 1, 1) +
104  -im(-1, 0) + im( 1, 0) +
105  -im(-1, -1) + im( 1, -1);
106  double const sum_y =
107  (im(-1, 1) + im( 0, 1) + im( 1, 1)) -
108  (im(-1, -1) + im( 0, -1) + im( 1, -1));
109 
110  result.x = lsst::afw::image::indexToPosition(x + image.getX0()) + sum_x / sum;
111  result.y = lsst::afw::image::indexToPosition(y + image.getY0()) + sum_y / sum;
112  measRecord.set(_centroidKey, result);
113  _flagHandler.setValue(measRecord, FAILURE, false); // if we had a suspect flag, we'd set that instead
114 }
115 
116 
118  _flagHandler.handleFailure(measRecord, error);
119 }
120 
122  Control const & ctrl,
123  std::string const & name,
124  afw::table::SchemaMapper & mapper
125 ) :
126  CentroidTransform{name, mapper}
127 {
128  for (auto flag = getFlagDefinitions().begin() + 1; flag < getFlagDefinitions().end(); ++flag) {
129  mapper.addMapping(mapper.getInputSchema().find<afw::table::Flag>(
130  mapper.getInputSchema().join(name, flag->name)).key);
131  }
132 }
133 
134 }}} // namespace lsst::meas::base
135 
136 
int y
Defines the fields and offsets for a table.
Definition: Schema.h:46
virtual void measure(afw::table::SourceRecord &measRecord, afw::image::Exposure< float > const &exposure) const
MaskedImageT getMaskedImage()
Return the MaskedImage.
Definition: Exposure.h:150
ImagePtr getImage(bool const noThrow=false) const
Return a (Ptr to) the MaskedImage&#39;s image.
Definition: MaskedImage.h:869
NaiveCentroidAlgorithm(Control const &ctrl, std::string const &name, afw::table::Schema &schema)
virtual void fail(afw::table::SourceRecord &measRecord, MeasurementError *error=NULL) const
double indexToPosition(double ind)
Convert image index to image position.
Definition: ImageUtils.h:54
table::Key< std::string > name
Definition: ApCorrMap.cc:71
Eigen matrix objects that present a view into an ndarray::Array.
A mapping between the keys of two Schemas, used to copy data between them.
Definition: SchemaMapper.h:19
A reusable struct for centroid measurements.
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
boost::enable_if< typename ExpressionTraits< Scalar >::IsScalar, Scalar >::type sum(Scalar const &scalar)
Definition: operators.h:1250
Key< T > addMapping(Key< T > const &inputKey, bool doReplace=false)
Add a new field to the output Schema that is a copy of a field in the input Schema.
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
SafeCentroidExtractor _centroidExtractor
Definition: NaiveCentroid.h:96
NaiveCentroidTransform(Control const &ctrl, std::string const &name, afw::table::SchemaMapper &mapper)
def error
Definition: log.py:108
FlagDefinition getDefinition(int i) const
Definition: FlagHandler.h:66
if(width!=gim.getWidth()||height!=gim.getHeight()||x0!=gim.getX0()||y0!=gim.getY0())
Definition: saturated.cc:47
void setValue(afw::table::BaseRecord &record, int i, bool value) const
Definition: FlagHandler.h:72
tbl::Schema schema
int x
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
Algorithm provides no uncertainy information at all.
Definition: constants.h:42
double background
&quot;Value to subtract from the image pixel values&quot; ;
Definition: NaiveCentroid.h:44
void set(Key< T > const &key, U const &value)
Set value of a field for the given key.
Definition: BaseRecord.h:136
A FunctorKey for CentroidResult.
CentroidElement y
y (row) coordinate of the measured position
void handleFailure(afw::table::BaseRecord &record, MeasurementError const *error=NULL) const
Definition: FlagHandler.cc:59
A C++ control class to handle NaiveCentroidAlgorithm&#39;s configuration.
Definition: NaiveCentroid.h:42
Record class that contains measurements made on a single exposure.
Definition: Source.h:81
static FlagHandler addFields(afw::table::Schema &schema, std::string const &prefix, FlagDefinition const *begin, FlagDefinition const *end)
Definition: FlagHandler.cc:28