LSSTApplications  11.0-13-gbb96280,12.1+18,12.1+7,12.1-1-g14f38d3+72,12.1-1-g16c0db7+5,12.1-1-g5961e7a+84,12.1-1-ge22e12b+23,12.1-11-g06625e2+4,12.1-11-g0d7f63b+4,12.1-19-gd507bfc,12.1-2-g7dda0ab+38,12.1-2-gc0bc6ab+81,12.1-21-g6ffe579+2,12.1-21-gbdb6c2a+4,12.1-24-g941c398+5,12.1-3-g57f6835+7,12.1-3-gf0736f3,12.1-37-g3ddd237,12.1-4-gf46015e+5,12.1-5-g06c326c+20,12.1-5-g648ee80+3,12.1-5-gc2189d7+4,12.1-6-ga608fc0+1,12.1-7-g3349e2a+5,12.1-7-gfd75620+9,12.1-9-g577b946+5,12.1-9-gc4df26a+10
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 std::array<FlagDefinition,NaiveCentroidAlgorithm::N_FLAGS> const & getFlagDefinitions() {
33  static std::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  _flagHandler(FlagHandler::addFields(schema, name,
52  getFlagDefinitions().begin(), getFlagDefinitions().end())),
53  _centroidExtractor(schema, name, true),
54  _centroidChecker(schema, name, ctrl.doFootprintCheck, ctrl.maxDistToPeak)
55 {
56 }
57 
59  afw::table::SourceRecord & measRecord,
60  afw::image::Exposure<float> const & exposure
61 ) const {
62 
64  CentroidResult result;
65  result.x = center.getX();
66  result.y = center.getY();
67  measRecord.set(_centroidKey, result); // better than NaN
68 
69  typedef afw::image::Image<float> ImageT;
70  ImageT const& image = *exposure.getMaskedImage().getImage();
71 
72  int x = center.getX(); // FIXME: this is different from GaussianCentroid and SdssCentroid here,
73  int y = center.getY(); // and probably shouldn't be.
74 
75  x -= image.getX0(); // work in image Pixel coordinates
76  y -= image.getY0();
77 
78  if (x < 1 || x >= image.getWidth() - 1 || y < 1 || y >= image.getHeight() - 1) {
79 
80  throw LSST_EXCEPT(
83  EDGE
84  );
85  }
86 
87  ImageT::xy_locator im = image.xy_at(x, y);
88 
89  double const sum =
90  (im(-1, 1) + im( 0, 1) + im( 1, 1) +
91  im(-1, 0) + im( 0, 0) + im( 1, 0) +
92  im(-1, -1) + im( 0, -1) + im( 1, -1))
93  - 9 * _ctrl.background;
94 
95  if (sum == 0.0) {
96  throw LSST_EXCEPT(
99  NO_COUNTS
100  );
101  }
102 
103  double const sum_x =
104  -im(-1, 1) + im( 1, 1) +
105  -im(-1, 0) + im( 1, 0) +
106  -im(-1, -1) + im( 1, -1);
107  double const sum_y =
108  (im(-1, 1) + im( 0, 1) + im( 1, 1)) -
109  (im(-1, -1) + im( 0, -1) + im( 1, -1));
110 
111  result.x = lsst::afw::image::indexToPosition(x + image.getX0()) + sum_x / sum;
112  result.y = lsst::afw::image::indexToPosition(y + image.getY0()) + sum_y / sum;
113  measRecord.set(_centroidKey, result);
114  _centroidChecker(measRecord);
115 }
116 
117 
119  _flagHandler.handleFailure(measRecord, error);
120 }
121 
123  Control const & ctrl,
124  std::string const & name,
125  afw::table::SchemaMapper & mapper
126 ) :
127  CentroidTransform{name, mapper}
128 {
129  for (auto flag = getFlagDefinitions().begin() + 1; flag < getFlagDefinitions().end(); ++flag) {
130  mapper.addMapping(mapper.getInputSchema().find<afw::table::Flag>(
131  mapper.getInputSchema().join(name, flag->name)).key);
132  }
133 }
134 
135 }}} // namespace lsst::meas::base
136 
137 
int y
Defines the fields and offsets for a table.
Definition: Schema.h:44
virtual void measure(afw::table::SourceRecord &measRecord, afw::image::Exposure< float > const &exposure) const
Called to measure a single child source in an image.
NaiveCentroidAlgorithm(Control const &ctrl, std::string const &name, afw::table::Schema &schema)
virtual void fail(afw::table::SourceRecord &measRecord, MeasurementError *error=NULL) const
Handle an exception thrown by the current algorithm by setting flags in the given record...
double indexToPosition(double ind)
Convert image index to image position.
Definition: ImageUtils.h:54
table::Key< std::string > name
Definition: ApCorrMap.cc:71
afw::table::Schema schema
Definition: GaussianPsf.cc:41
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
ImagePtr getImage(bool const noThrow=false) const
Return a (Ptr to) the MaskedImage&#39;s image.
Definition: MaskedImage.h:875
std::array< FlagDefinition, BlendednessAlgorithm::N_FLAGS > const & getFlagDefinitions()
Definition: Blendedness.cc:215
Exception to be thrown when a measurement algorithm experiences a known failure mode.
Definition: exceptions.h:48
void set(Key< T > const &key, U const &value)
Set value of a field for the given key.
Definition: BaseRecord.h:145
FlagDefinition getDefinition(std::size_t i) const
Return the FlagDefinition object that corresponds to a particular enum value.
Definition: FlagHandler.h:172
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
NaiveCentroidTransform(Control const &ctrl, std::string const &name, afw::table::SchemaMapper &mapper)
def error
Definition: log.py:103
Utility class for handling flag fields that indicate the failure modes of an algorithm.
Definition: FlagHandler.h:73
MaskedImageT getMaskedImage()
Return the MaskedImage.
Definition: Exposure.h:153
if(width!=gim.getWidth()||height!=gim.getHeight()||x0!=gim.getX0()||y0!=gim.getY0())
Definition: saturated.cc:47
double x
#define LSST_EXCEPT(type,...)
Create an exception with a given type and message and optionally other arguments (dependent on the ty...
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
A FunctorKey for CentroidResult.
CentroidElement y
y (row) coordinate of the measured position
void handleFailure(afw::table::BaseRecord &record, MeasurementError const *error=NULL) const
Handle an expected or unexpected Exception thrown by a measurement algorithm.
Definition: FlagHandler.cc:77
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:80
Base for centroid measurement transformations.
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.