14 #include "boost/timer.hpp"
24 namespace pexExcept = lsst::pex::exceptions;
25 namespace afwGeom = lsst::afw::geom;
27 namespace afwMath = lsst::afw::math;
28 namespace pexLogging = lsst::pex::logging;
52 if ((width < 1) || (height < 1)) {
55 const int signedWidth =
static_cast<int>(width);
56 const int signedHeight =
static_cast<int>(height);
58 for (
int row = 0;
row < signedHeight; ++
row) {
59 for (
int col = 0;
col < signedWidth; ++
col) {
60 boost::shared_ptr<afwMath::Kernel>
62 kernelBasisList.push_back(kernelPtr);
65 return kernelBasisList;
82 std::vector<double>
const &sigGauss,
83 std::vector<int>
const °Gauss
91 if (nGauss != static_cast<int>(sigGauss.size())) {
94 if (nGauss != static_cast<int>(degGauss.size())) {
97 int fullWidth = 2 * halfWidth + 1;
101 for (
int i = 0; i < nGauss; i++) {
105 double sig = sigGauss[i];
106 int deg = degGauss[i];
108 pexLogging::TTrace<2>(
"lsst.ip.diffim.BasisLists.makeAlardLuptonBasisList",
109 "Gaussian %d : sigma %.2f degree %d", i, sig, deg);
115 for (
int j = 0, n = 0; j <= deg; j++) {
116 for (
int k = 0; k <= (deg - j); k++, n++) {
120 boost::shared_ptr<afwMath::Kernel>
122 kernelBasisList.push_back(kernelPtr);
129 for (
int y = 0, v = -halfWidth;
y < image.getHeight();
y++, v++) {
131 for (Image::xy_locator ptr = image.xy_at(0,
y),
132 end = image.xy_at(image.getWidth(),
y);
133 ptr != end; ++ptr.x(), u++) {
135 *ptr = *ptr * polynomial(u/static_cast<double>(halfWidth),
136 v/static_cast<double>(halfWidth));
139 boost::shared_ptr<afwMath::Kernel>
141 kernelBasisList.push_back(kernelPtr);
150 boost::shared_ptr<Eigen::MatrixXd>
185 std::string regularizationType = policy.
getString(
"regularizationType");
186 int width = policy.
getInt(
"kernelSize");
187 int height = policy.
getInt(
"kernelSize");
188 float borderPenalty = policy.
getDouble(
"regularizationBorderPenalty");
189 bool fitForBackground = policy.
getBool(
"fitForBackground");
191 boost::shared_ptr<Eigen::MatrixXd> bMat;
192 if (regularizationType ==
"centralDifference") {
193 int stencil = policy.
getInt(
"centralRegularizationStencil");
196 else if (regularizationType ==
"forwardDifference") {
197 std::vector<int> orders = policy.
getIntArray(
"forwardRegularizationOrders");
204 boost::shared_ptr<Eigen::MatrixXd> hMat (
new Eigen::MatrixXd((*bMat).transpose() * (*bMat)));
211 boost::shared_ptr<Eigen::MatrixXd>
217 bool fitForBackground
240 if (borderPenalty < 0)
243 std::vector<std::vector<float> >
244 coeffs(3, std::vector<float>(3,0));
261 else if (stencil == 9) {
263 coeffs[0][0] = 1. / 6.;
264 coeffs[0][1] = 4. / 6.;
265 coeffs[0][2] = 1. / 6.;
266 coeffs[1][0] = 4. / 6.;
267 coeffs[1][1] = -20. / 6.;
268 coeffs[1][2] = 4. / 6.;
269 coeffs[2][0] = 1. / 6.;
270 coeffs[2][1] = 4. / 6.;
271 coeffs[2][2] = 1. / 6.;
277 int nBgTerms = fitForBackground ? 1 : 0;
278 Eigen::MatrixXd bMat = Eigen::MatrixXd::Zero(width * height + nBgTerms, width * height + nBgTerms);
280 for (
int i = 0; i < width*height; i++) {
281 int const x0 = i % width;
282 int const y0 = i / width;
283 int const distX = width - x0 - 1;
284 int const distY = height - y0 - 1;
286 if ( (x0 > 0) && (y0 > 0) && (distX > 0) && (distY > 0) ) {
287 for (
int dx = -1; dx < 2; dx += 1) {
288 for (
int dy = -1; dy < 2; dy += 1) {
289 bMat(i, i + dx + dy * width) += coeffs[dx+1][dy+1];
294 bMat(i, i) = borderPenalty;
298 if (fitForBackground) {
300 if (bMat.col(width*height).sum() != 0.) {
303 if (bMat.row(width*height).sum() != 0.) {
308 boost::shared_ptr<Eigen::MatrixXd> bMatPtr (
new Eigen::MatrixXd(bMat));
315 boost::shared_ptr<Eigen::MatrixXd>
319 std::vector<int>
const& orders,
321 bool fitForBackground
340 if (borderPenalty < 0)
343 std::vector<std::vector<float> >
344 coeffs(4, std::vector<float>(4,0));
347 coeffs[0][0] = borderPenalty;
364 int nBgTerms = fitForBackground ? 1 : 0;
365 Eigen::MatrixXd bTot = Eigen::MatrixXd::Zero(width * height + nBgTerms, width * height + nBgTerms);
367 std::vector<int>::const_iterator order;
368 for (order = orders.begin(); order != orders.end(); order++) {
369 if ((*order < 1) || (*order > 3))
372 Eigen::MatrixXd bMatX = Eigen::MatrixXd::Zero(width * height + nBgTerms,
373 width * height + nBgTerms);
374 Eigen::MatrixXd bMatY = Eigen::MatrixXd::Zero(width * height + nBgTerms,
375 width * height + nBgTerms);
377 for (
int i = 0; i < width*height; i++) {
378 int const x0 = i % width;
379 int const y0 = i / width;
381 int distX = width - x0 - 1;
382 int orderToUseX = std::min(distX, *order);
383 for (
int j = 0; j < orderToUseX+1; j++) {
384 bMatX(i, i + j) = coeffs[orderToUseX][j];
387 int distY = height - y0 - 1;
388 int orderToUseY = std::min(distY, *order);
389 for (
int j = 0; j < orderToUseY+1; j++) {
390 bMatY(i, i + j * width) = coeffs[orderToUseY][j];
397 if (fitForBackground) {
399 if (bTot.col(width*height).sum() != 0.) {
402 if (bTot.row(width*height).sum() != 0.) {
407 boost::shared_ptr<Eigen::MatrixXd> bMatPtr (
new Eigen::MatrixXd(bTot));
451 if (kernelListIn.size() == 0) {
452 return kernelListOut;
455 Image image0(kernelListIn[0]->getDimensions());
456 for (
unsigned int i = 0; i < kernelListIn.size(); i++) {
459 (void)kernelListIn[i]->computeImage(image0,
true);
460 boost::shared_ptr<afwMath::Kernel>
462 kernelListOut.push_back(kernelPtr);
468 Image
image(kernelListIn[i]->getDimensions());
469 (void)kernelListIn[i]->computeImage(image,
false);
474 for (
int y = 0;
y < image.getHeight();
y++) {
475 for (Image::xy_locator ptr = image.xy_at(0,
y), end = image.xy_at(image.getWidth(),
y);
476 ptr != end; ++ptr.x()) {
487 if (fabs(kSum) > std::numeric_limits<float>::epsilon()) {
494 for (
int y = 0;
y < image.getHeight();
y++) {
495 for (Image::xy_locator ptr = image.xy_at(0,
y), end = image.xy_at(image.getWidth(),
y);
496 ptr != end; ++ptr.x()) {
500 image /= std::sqrt(kSum);
503 boost::shared_ptr<afwMath::Kernel>
505 kernelListOut.push_back(kernelPtr);
507 return kernelListOut;
515 boost::shared_ptr<Eigen::MatrixXd>
520 unsigned int boundary_style,
521 unsigned int difference_style,
527 if (boundary_style > 2) {
530 if (difference_style > 1) {
564 std::vector<std::vector<std::vector<float> > >
565 coeffs(3, std::vector<std::vector<float> >(5, std::vector<float>(5,0)));
566 unsigned int x_cen = 0, y_cen = 0;
567 unsigned int x_cen1 = 0, y_cen1 = 0;
568 unsigned int x_cen2 = 0, y_cen2 = 0;
569 unsigned int x_size = 0, y_size = 0;
572 if (difference_style == 0) {
578 x_size = y_size = order + 2;
582 coeffs[0][0][0] = -2;
588 coeffs[1][0][0] = -2;
590 coeffs[1][0][2] = -1;
594 coeffs[1][2][0] = -1;
599 coeffs[2][0][0] = -2;
601 coeffs[2][0][2] = -3;
607 coeffs[2][2][0] = -3;
618 if (difference_style == 1) {
627 coeffs[0][0][1] = -1;
630 coeffs[0][1][0] = -1;
651 coeffs[1][1][1] = -4;
668 coeffs[2][0][2] = -1;
678 coeffs[2][2][0] = -1;
681 coeffs[2][2][3] = -2;
686 coeffs[2][3][2] = -2;
700 Eigen::MatrixXd bMat = Eigen::MatrixXd::Zero(width*height+1, width*height+1);
703 for (
unsigned int i = 0; i < width*height; i++) {
705 unsigned int const x0 = i % width;
706 unsigned int const y0 = i / width;
708 unsigned int x_edge_distance = (x0 > (width - x0 - 1)) ? width - x0 - 1 :
x0;
709 unsigned int y_edge_distance = (y0 > (height - y0 - 1)) ? height - y0 - 1 :
y0;
710 unsigned int edge_distance = (x_edge_distance < y_edge_distance) ? x_edge_distance :
713 for (
unsigned int dx = 0; dx < x_size; dx++) {
714 for (
unsigned int dy = 0; dy < y_size; dy++) {
722 double this_coeff = 0;
725 if (boundary_style == 0) {
728 if (y > height - 1 || x > width - 1) {
731 this_coeff = coeffs[order][dx][dy];
734 }
else if (boundary_style == 1) {
735 x = (width + x0 + dx - x_cen) % width;
736 y = (height + y0 + dy - y_cen) % height;
737 this_coeff = coeffs[order][dx][dy];
741 }
else if (boundary_style == 2) {
744 if (edge_distance == 0) {
750 else if (edge_distance == 1 && order > 0) {
751 x = (width + x0 + dx - x_cen1) % width;
752 y = (height + y0 + dy - y_cen1) % height;
753 if ((dx < 3) && (dy < 3)) { this_coeff = coeffs[1][dx][dy]; }
756 else if (edge_distance == 2 && order > 1){
757 x = (width + x0 + dx - x_cen2) % width;
758 y = (height + y0 + dy - y_cen2) % height;
759 if ((dx < 5) && (dy < 5)) { this_coeff = coeffs[2][dx][dy]; }
762 else if (edge_distance > order) {
763 x = (width + x0 + dx - x_cen) % width;
764 y = (height + y0 + dy - y_cen) % height;
765 this_coeff = coeffs[order][dx][dy];
770 bMat(i, y*width + x) = this_coeff;
779 std::cout << bMat << std::endl;
782 boost::shared_ptr<Eigen::MatrixXd> hMat (
new Eigen::MatrixXd(bMat.transpose() * bMat));
An include file to include the public header files for lsst::afw::math.
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.
An include file to include the header files for lsst::afw::geom.
lsst::afw::math::KernelList renormalizeKernelList(lsst::afw::math::KernelList const &kernelListIn)
Rescale an input set of kernels.
IntArray getIntArray(const std::string &name) const
a container for holding hierarchical configuration data in memory.
2-dimensional polynomial function with cross terms
const std::string getString(const std::string &name) const
definition of the Trace messaging facilities
void setParameter(unsigned int ind, double newValue)
Set one function parameter without range checking.
double getDouble(const std::string &name) const
int getInt(const std::string &name) const
boost::shared_ptr< Eigen::MatrixXd > makeCentralDifferenceMatrix(int width, int height, int stencil, float borderPenalty, bool fitForBackground)
Generate regularization matrix for delta function kernels.
table::Key< table::Array< Kernel::Pixel > > image
An include file to include the header files for lsst::afw::image.
boost::shared_ptr< Eigen::MatrixXd > makeRegularizationMatrix(lsst::pex::policy::Policy policy)
Build a regularization matrix for Delta function kernels.
boost::shared_ptr< 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.
bool getBool(const std::string &name) const
#define LSST_EXCEPT(type,...)
lsst::afw::math::KernelList makeDeltaFunctionBasisList(int width, int height)
Generate a basis set of delta function Kernels.
std::vector< boost::shared_ptr< Kernel > > KernelList
A kernel described by a function.
Subroutines associated with generating, normalising, and regularising Basis functions.
A kernel that has only one non-zero pixel (of value 1)
A kernel created from an Image.
boost::shared_ptr< Eigen::MatrixXd > makeForwardDifferenceMatrix(int width, int height, std::vector< int > const &orders, float borderPenalty, bool fitForBackground)
Generate 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)
Generate an Alard-Lupton basis set of Kernels.