32 namespace lsst {
namespace meas {
namespace base {
34 #define USE_WEIGHT 0 // zweight is only set, not used. It isn't even set if this is false
44 typedef Eigen::Matrix<double, FittedModel::NPARAM, FittedModel::NPARAM> Matrix;
45 typedef Eigen::Matrix<double, FittedModel::NPARAM, 1> Vector;
47 template<
typename PixelT>
48 explicit Fit2d(afw::image::Image<PixelT>
const& im) :
wide(32),
rast(*
new std::vector<Raster>(
wide*
wide)) {
49 ncols = im.getWidth();
50 nrows = im.getHeight();
110 static void dcalc2(
Fit2d *fit,
127 }
else if (s <= 0.0) {
142 for (
int i = 0, nrast = fit->
rast.size(); i != nrast; i++) {
143 double const dx = (fit->
rast[i].x - x0)/s;
144 double const dy = (fit->
rast[i].y - y0)/s;
145 double const d = hypot(dx, dy);
148 double const arg = exp(-d*d/2.0);
149 double const funct = a*arg +
b;
152 df << arg, 1.0, a*arg*dx/s, a*arg*dy/s, a*arg*(dx*dx/s + dy*dy/s);
154 double const r = fit->
rast[i].zo - funct;
157 *alpha += df*df.transpose();
174 static void curf2(
Fit2d *fit) {
179 if (fit->
iter == 0) {
200 for (
int poor = 0; poor != NPLIM; ++poor) {
202 if (fit->
alnew(j, j) == 0.0) {
215 fit->
alpha.computeInverse();
217 Eigen::FullPivLU<Fit2d::Matrix> alphaLU(fit->
alpha);
218 if (!alphaLU.isInvertible()) {
222 fit->
alpha = alphaLU.inverse();
269 static void cond2(
Fit2d *fit) {
273 if (fit->
iter <= 3) {
291 if (fit->
chnew <= 0.0) {
307 if (ex > fit->
lost || ey > fit->
lost) {
328 template<
typename PixelT>
329 static void fg2(afw::image::Image<PixelT>
const& im,
330 double x0,
double y0,
336 int ix0 =
static_cast<int>(x0 + 0.5);
337 int iy0 =
static_cast<int>(y0 + 0.5);
345 double peak = im(jx0, jy0);
349 double const w = fit->
wide/2;
350 int xl =
static_cast<int>(ix0 - w + 0.5);
354 int xh =
static_cast<int>(xl + 2*w + 0.5);
355 if (xh > im.getWidth()) {
358 int yl =
static_cast<int>(iy0 - w + 0.5);
362 int yh =
static_cast<int>(yl + 2*w + 0.5);
363 if (yh > im.getHeight()) {
367 double sky = im(xl, yl);
369 for (
int y = yl;
y != yh; ++
y) {
370 for (
int x = xl;
x != xh; ++
x) {
371 fit->
rast[put].x =
x;
372 fit->
rast[put].y =
y;
373 fit->
rast[put].zo = im(
x,
y);
375 fit->
rast[put].zweight = 1.0;
377 if (im(
x,
y) < sky) {
380 double const ex =
x - ix0;
381 double const ey =
y - iy0;
382 double const er = hypot(ex, ey);
383 if (er <= fit->
lost) {
384 if (im(
x,
y) > peak) {
393 fit->
rast.resize(put);
400 double xmax = xh - 1;
402 double ymax = yh - 1;
403 double const test = 0.5*(im(ix0, iy0) + sky);
404 for (
int x = ix0;
x < xh - 1; ++
x) {
405 if (im(
x + 1, iy0) <=
test) {
406 double a = test - im(
x, iy0);
407 double b = im(
x + 1, iy0) - im(
x, iy0);
409 xmax =
x + ((b == 0.0) ? 0.5 : a/
b);
413 for (
int x = ix0;
x > 0; --
x) {
414 if (im(
x - 1, iy0) <=
test) {
415 double a = test - im(
x, iy0);
416 double b = im(
x - 1, iy0) - im(
x, iy0);
418 xmin =
x - ((b == 0.0) ? 0.5 : a/
b);
422 for (
int y = iy0;
y < yh - 1; ++
y) {
423 if (im(ix0,
y + 1) <=
test) {
424 double a = test - im(ix0,
y);
425 double b = im(ix0,
y + 1) - im(ix0,
y);
427 ymax =
y + ((b == 0.0) ? 0.5 : a/
b);
431 for (
int y = iy0;
y > 0; --
y) {
432 if (im(ix0,
y - 1) <=
test) {
433 double a = test - im(ix0,
y);
434 double b = im(ix0,
y - 1) - im(ix0,
y);
436 ymin =
y - ((b == 0.0) ? 0.5 : a/
b);
460 template<
typename PixelT>
472 fg2(im, x0, y0, &fit);
495 #define MAKE_TWODG(IMAGE_T) \
496 template FittedModel twodg(IMAGE_T const& im, double x0, double y0)
506 std::string
const & name,
512 _centroidExtractor(schema, name, true)
514 static boost::array<FlagDefinition,N_FLAGS>
const flagDefs = {{
515 {
"flag",
"general failure flag, set if anything went wrong"},
516 {
"flag_noPeak",
"Fitted Centroid has a negative peak"}
529 result.
x = center.getX();
530 result.
y = center.getY();
535 int x =
static_cast<int>(center.getX() + 0.5);
536 int y =
static_cast<int>(center.getY() + 0.5);
std::vector< Raster > & rast
Defines the fields and offsets for a table.
GaussianCentroidAlgorithm(Control const &ctrl, std::string const &name, afw::table::Schema &schema)
#define MAKE_TWODG(IMAGE_T)
double indexToPosition(double ind)
Convert image index to image position.
Eigen matrix objects that present a view into an ndarray::Array.
virtual void fail(afw::table::SourceRecord &measRecord, MeasurementError *error=NULL) const
A class to contain the data, WCS, and other information needed to describe an image of the sky...
Only the diagonal elements of the covariance matrix are provided.
A reusable struct for centroid measurements.
SelectEigenView< T >::Type copy(Eigen::EigenBase< T > const &other)
Copy an arbitrary Eigen expression into a new EigenView.
Eigen::Matrix< double, FittedModel::NPARAM, FittedModel::NPARAM > Matrix
CentroidElement x
x (column) coordinate of the measured position
FittedModel twodg(afwImage::Image< PixelT > const &im, double x0, double y0)
Exception to be thrown when a measurement algorithm experiences a known failure mode.
table::Key< table::Array< Kernel::Pixel > > image
std::vector< Raster > & rast
FlagDefinition getDefinition(int i) const
MaskedImageT getMaskedImage()
Return the MaskedImage.
std::vector< double > params
A C++ control class to handle GaussianCentroidAlgorithm's configuration.
void setValue(afw::table::BaseRecord &record, int i, bool value) const
virtual void measure(afw::table::SourceRecord &measRecord, afw::image::Exposure< float > const &exposure) const
afw::table::Centroid::MeasKey _centroidKey
#define LSST_EXCEPT(type,...)
Eigen::Matrix< double, FittedModel::NPARAM, 1 > Vector
A FunctorKey for CentroidResult.
afw::table::Key< double > b
void set(Key< T > const &key, U const &value)
Set value of a field for the given key.
CentroidElement y
y (row) coordinate of the measured position
void handleFailure(afw::table::BaseRecord &record, MeasurementError const *error=NULL) const
Record class that contains measurements made on a single exposure.
A class to represent a 2-dimensional array of pixels.
CentroidResultKey _centroidKey
static FlagHandler addFields(afw::table::Schema &schema, std::string const &prefix, FlagDefinition const *begin, FlagDefinition const *end)
SafeCentroidExtractor _centroidExtractor