LSSTApplications  11.0-13-gbb96280,12.1.rc1,12.1.rc1+1,12.1.rc1+2,12.1.rc1+5,12.1.rc1+8,12.1.rc1-1-g06d7636+1,12.1.rc1-1-g253890b+5,12.1.rc1-1-g3d31b68+7,12.1.rc1-1-g3db6b75+1,12.1.rc1-1-g5c1385a+3,12.1.rc1-1-g83b2247,12.1.rc1-1-g90cb4cf+6,12.1.rc1-1-g91da24b+3,12.1.rc1-2-g3521f8a,12.1.rc1-2-g39433dd+4,12.1.rc1-2-g486411b+2,12.1.rc1-2-g4c2be76,12.1.rc1-2-gc9c0491,12.1.rc1-2-gda2cd4f+6,12.1.rc1-3-g3391c73+2,12.1.rc1-3-g8c1bd6c+1,12.1.rc1-3-gcf4b6cb+2,12.1.rc1-4-g057223e+1,12.1.rc1-4-g19ed13b+2,12.1.rc1-4-g30492a7
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
CentroidElement y
y (row) coordinate of the measured position
FlagDefinition getDefinition(std::size_t i) const
Definition: FlagHandler.h:172
double indexToPosition(double ind)
Convert image index to image position.
Definition: ImageUtils.h:54
table::Key< std::string > name
Definition: ApCorrMap.cc:71
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.
SafeCentroidExtractor _centroidExtractor
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.
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
virtual void fail(afw::table::SourceRecord &measRecord, MeasurementError *error=NULL) const
Algorithm provides no uncertainy information at all.
Definition: constants.h:42
Exception to be thrown when a measurement algorithm experiences a known failure mode.
Definition: exceptions.h:48
void handleFailure(afw::table::BaseRecord &record, MeasurementError const *error=NULL) const
Definition: FlagHandler.cc:77
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
def error
Definition: log.py:103
MaskedImageT getMaskedImage()
Return the MaskedImage.
Definition: Exposure.h:148
if(width!=gim.getWidth()||height!=gim.getHeight()||x0!=gim.getX0()||y0!=gim.getY0())
Definition: saturated.cc:47
double x
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
virtual void measure(afw::table::SourceRecord &measRecord, afw::image::Exposure< float > const &exposure) const
CentroidElement x
x (column) coordinate of the measured position
A FunctorKey for CentroidResult.
void set(Key< T > const &key, U const &value)
Set value of a field for the given key.
Definition: BaseRecord.h:145
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
double background
&quot;Value to subtract from the image pixel values&quot; ;
Definition: NaiveCentroid.h:44
NaiveCentroidAlgorithm(Control const &ctrl, std::string const &name, afw::table::Schema &schema)
NaiveCentroidTransform(Control const &ctrl, std::string const &name, afw::table::SchemaMapper &mapper)