14 #include "boost/timer.hpp"
51 if ((width < 1) || (height < 1)) {
54 const int signedWidth =
static_cast<int>(width);
55 const int signedHeight =
static_cast<int>(height);
57 for (
int row = 0;
row < signedHeight; ++
row) {
58 for (
int col = 0;
col < signedWidth; ++
col) {
64 return kernelBasisList;
90 if (nGauss !=
static_cast<int>(sigGauss.
size())) {
93 if (nGauss !=
static_cast<int>(degGauss.
size())) {
96 int fullWidth = 2 * halfWidth + 1;
100 for (
int i = 0; i < nGauss; i++) {
104 double sig = sigGauss[i];
105 int deg = degGauss[i];
107 LOGL_DEBUG(
"TRACE1.ip.diffim.BasisLists.makeAlardLuptonBasisList",
108 "Gaussian %d : sigma %.2f degree %d", i, sig, deg);
114 for (
int j = 0, n = 0; j <= deg; j++) {
115 for (
int k = 0; k <= (deg - j); k++, n++) {
128 for (
int y = 0, v = -halfWidth;
y <
image.getHeight();
y++, v++) {
132 *
ptr = *
ptr * polynomial(u/
static_cast<double>(halfWidth),
133 v/
static_cast<double>(halfWidth));
182 int width = ps.
getAsInt(
"kernelSize");
183 int height = ps.
getAsInt(
"kernelSize");
184 float borderPenalty = ps.
getAsDouble(
"regularizationBorderPenalty");
185 bool fitForBackground = ps.
getAsBool(
"fitForBackground");
187 Eigen::MatrixXd bMat;
188 if (regularizationType ==
"centralDifference") {
189 int stencil = ps.
getAsInt(
"centralRegularizationStencil");
192 else if (regularizationType ==
"forwardDifference") {
200 Eigen::MatrixXd hMat = bMat.transpose() * bMat;
212 bool fitForBackground
235 if (borderPenalty < 0)
256 else if (stencil == 9) {
258 coeffs[0][0] = 1. / 6.;
259 coeffs[0][1] = 4. / 6.;
260 coeffs[0][2] = 1. / 6.;
261 coeffs[1][0] = 4. / 6.;
262 coeffs[1][1] = -20. / 6.;
263 coeffs[1][2] = 4. / 6.;
264 coeffs[2][0] = 1. / 6.;
265 coeffs[2][1] = 4. / 6.;
266 coeffs[2][2] = 1. / 6.;
272 int nBgTerms = fitForBackground ? 1 : 0;
273 Eigen::MatrixXd bMat = Eigen::MatrixXd::Zero(width * height + nBgTerms, width * height + nBgTerms);
275 for (
int i = 0; i < width*height; i++) {
276 int const x0 = i % width;
277 int const y0 = i / width;
278 int const distX = width - x0 - 1;
279 int const distY = height - y0 - 1;
281 if ( (x0 > 0) && (y0 > 0) && (distX > 0) && (distY > 0) ) {
282 for (
int dx = -1; dx < 2; dx += 1) {
283 for (
int dy = -1; dy < 2; dy += 1) {
284 bMat(i, i + dx + dy * width) += coeffs[dx+1][dy+1];
289 bMat(i, i) = borderPenalty;
293 if (fitForBackground) {
295 if (bMat.col(width*height).sum() != 0.) {
298 if (bMat.row(width*height).sum() != 0.) {
314 bool fitForBackground
333 if (borderPenalty < 0)
340 coeffs[0][0] = borderPenalty;
357 int nBgTerms = fitForBackground ? 1 : 0;
358 Eigen::MatrixXd bTot = Eigen::MatrixXd::Zero(width * height + nBgTerms, width * height + nBgTerms);
365 Eigen::MatrixXd bMatX = Eigen::MatrixXd::Zero(width * height + nBgTerms,
366 width * height + nBgTerms);
367 Eigen::MatrixXd bMatY = Eigen::MatrixXd::Zero(width * height + nBgTerms,
368 width * height + nBgTerms);
370 for (
int i = 0; i < width*height; i++) {
371 int const x0 = i % width;
372 int const y0 = i / width;
374 int distX = width - x0 - 1;
376 for (
int j = 0; j < orderToUseX+1; j++) {
377 bMatX(i, i + j) = coeffs[orderToUseX][j];
380 int distY = height - y0 - 1;
382 for (
int j = 0; j < orderToUseY+1; j++) {
383 bMatY(i, i + j * width) = coeffs[orderToUseY][j];
390 if (fitForBackground) {
392 if (bTot.col(width*height).sum() != 0.) {
395 if (bTot.row(width*height).sum() != 0.) {
443 if (kernelListIn.
size() == 0) {
444 return kernelListOut;
447 Image image0(kernelListIn[0]->getDimensions());
448 for (
unsigned int i = 0; i < kernelListIn.
size(); i++) {
451 (void)kernelListIn[i]->computeImage(image0,
true);
460 Image
image(kernelListIn[i]->getDimensions());
461 (void)kernelListIn[i]->computeImage(
image,
false);
466 for (
int y = 0;
y <
image.getHeight();
y++) {
485 for (
int y = 0;
y <
image.getHeight();
y++) {
497 return kernelListOut;
509 unsigned int boundary_style,
510 unsigned int difference_style,
516 if (boundary_style > 2) {
519 if (difference_style > 1) {
555 unsigned int x_cen = 0, y_cen = 0;
556 unsigned int x_cen1 = 0, y_cen1 = 0;
557 unsigned int x_cen2 = 0, y_cen2 = 0;
558 unsigned int x_size = 0, y_size = 0;
561 if (difference_style == 0) {
567 x_size = y_size =
order + 2;
571 coeffs[0][0][0] = -2;
577 coeffs[1][0][0] = -2;
579 coeffs[1][0][2] = -1;
583 coeffs[1][2][0] = -1;
588 coeffs[2][0][0] = -2;
590 coeffs[2][0][2] = -3;
596 coeffs[2][2][0] = -3;
607 if (difference_style == 1) {
616 coeffs[0][0][1] = -1;
619 coeffs[0][1][0] = -1;
640 coeffs[1][1][1] = -4;
657 coeffs[2][0][2] = -1;
667 coeffs[2][2][0] = -1;
670 coeffs[2][2][3] = -2;
675 coeffs[2][3][2] = -2;
689 Eigen::MatrixXd bMat = Eigen::MatrixXd::Zero(width*height+1, width*height+1);
692 for (
unsigned int i = 0; i < width*height; i++) {
694 unsigned int const x0 = i % width;
695 unsigned int const y0 = i / width;
697 unsigned int x_edge_distance = (x0 > (width - x0 - 1)) ? width - x0 - 1 : x0;
698 unsigned int y_edge_distance = (y0 > (height - y0 - 1)) ? height - y0 - 1 : y0;
699 unsigned int edge_distance = (x_edge_distance < y_edge_distance) ? x_edge_distance :
702 for (
unsigned int dx = 0; dx < x_size; dx++) {
703 for (
unsigned int dy = 0; dy < y_size; dy++) {
711 double this_coeff = 0;
714 if (boundary_style == 0) {
717 if (
y > height - 1 ||
x > width - 1) {
720 this_coeff = coeffs[
order][dx][dy];
723 }
else if (boundary_style == 1) {
724 x = (width + x0 + dx - x_cen) % width;
725 y = (height + y0 + dy - y_cen) % height;
726 this_coeff = coeffs[
order][dx][dy];
730 }
else if (boundary_style == 2) {
733 if (edge_distance == 0) {
739 else if (edge_distance == 1 &&
order > 0) {
740 x = (width + x0 + dx - x_cen1) % width;
741 y = (height + y0 + dy - y_cen1) % height;
742 if ((dx < 3) && (dy < 3)) { this_coeff = coeffs[1][dx][dy]; }
745 else if (edge_distance == 2 &&
order > 1){
746 x = (width + x0 + dx - x_cen2) % width;
747 y = (height + y0 + dy - y_cen2) % height;
748 if ((dx < 5) && (dy < 5)) { this_coeff = coeffs[2][dx][dy]; }
751 else if (edge_distance >
order) {
752 x = (width + x0 + dx - x_cen) % width;
753 y = (height + y0 + dy - y_cen) % height;
754 this_coeff = coeffs[
order][dx][dy];
759 bMat(i,
y*width +
x) = this_coeff;
771 Eigen::MatrixXd hMat = bMat.transpose() * bMat;
Subroutines associated with generating, normalising, and regularising Basis functions.
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
LSST DM logging module built on log4cxx.
#define LOGL_DEBUG(logger, message...)
Log a debug-level message using a varargs/printf style interface.
A kernel described by a function.
double computeImage(lsst::afw::image::Image< Pixel > &image, bool doNormalize, double x=0.0, double y=0.0) const
Compute an image (pixellized representation of the kernel) in place.
A kernel that has only one non-zero pixel (of value 1)
A kernel created from an Image.
void setParameter(unsigned int ind, double newValue)
Set one function parameter without range checking.
2-dimensional polynomial function with cross terms
Class for storing generic metadata.
std::string getAsString(std::string const &name) const
Get the last value for a string property name (possibly hierarchical).
int getAsInt(std::string const &name) const
Get the last value for a bool/char/short/int property name (possibly hierarchical).
std::vector< T > getArray(std::string const &name) const
Get the vector of values for a property name (possibly hierarchical).
double getAsDouble(std::string const &name) const
Get the last value for any arithmetic property name (possibly hierarchical).
bool getAsBool(std::string const &name) const
Get the last value for a bool property name (possibly hierarchical).
Provides consistent interface for LSST exceptions.
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
lsst::afw::math::KernelList makeDeltaFunctionBasisList(int width, int height)
Build a set of Delta Function basis kernels.
lsst::afw::math::KernelList renormalizeKernelList(lsst::afw::math::KernelList const &kernelListIn)
Renormalize a list of basis kernels.
Eigen::MatrixXd makeFiniteDifferenceRegularizationDeprecated(unsigned int width, unsigned int height, unsigned int order, unsigned int boundary_style, unsigned int difference_style, bool printB)
Generate regularization matrix for delta function kernels.
Eigen::MatrixXd makeRegularizationMatrix(lsst::daf::base::PropertySet const &ps)
Build a regularization matrix for Delta function kernels.
Eigen::MatrixXd makeCentralDifferenceMatrix(int width, int height, int stencil, float borderPenalty, bool fitForBackground)
Build a central difference Laplacian regularization matrix for Delta function kernels.
Eigen::MatrixXd makeForwardDifferenceMatrix(int width, int height, std::vector< int > const &orders, float borderPenalty, bool fitForBackground)
Build a forward difference regularization matrix for Delta function kernels.
lsst::afw::math::KernelList makeAlardLuptonBasisList(int halfWidth, int nGauss, std::vector< double > const &sigGauss, std::vector< int > const °Gauss)
Build a set of Alard/Lupton basis kernels.
float Pixel
Typedefs to be used for pixel values.
A base class for image defects.