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++) {
130 for (Image::xy_locator
ptr =
image.xy_at(0,
y),
134 *
ptr = *
ptr * polynomial(u/
static_cast<double>(halfWidth),
135 v/
static_cast<double>(halfWidth));
184 int width = ps.
getAsInt(
"kernelSize");
185 int height = ps.
getAsInt(
"kernelSize");
186 float borderPenalty = ps.
getAsDouble(
"regularizationBorderPenalty");
187 bool fitForBackground = ps.
getAsBool(
"fitForBackground");
189 Eigen::MatrixXd bMat;
190 if (regularizationType ==
"centralDifference") {
191 int stencil = ps.
getAsInt(
"centralRegularizationStencil");
194 else if (regularizationType ==
"forwardDifference") {
202 Eigen::MatrixXd hMat = bMat.transpose() * bMat;
214 bool fitForBackground
237 if (borderPenalty < 0)
258 else if (stencil == 9) {
260 coeffs[0][0] = 1. / 6.;
261 coeffs[0][1] = 4. / 6.;
262 coeffs[0][2] = 1. / 6.;
263 coeffs[1][0] = 4. / 6.;
264 coeffs[1][1] = -20. / 6.;
265 coeffs[1][2] = 4. / 6.;
266 coeffs[2][0] = 1. / 6.;
267 coeffs[2][1] = 4. / 6.;
268 coeffs[2][2] = 1. / 6.;
274 int nBgTerms = fitForBackground ? 1 : 0;
275 Eigen::MatrixXd bMat = Eigen::MatrixXd::Zero(width * height + nBgTerms, width * height + nBgTerms);
277 for (
int i = 0; i < width*height; i++) {
278 int const x0 = i % width;
279 int const y0 = i / width;
280 int const distX = width - x0 - 1;
281 int const distY = height - y0 - 1;
283 if ( (x0 > 0) && (y0 > 0) && (distX > 0) && (distY > 0) ) {
284 for (
int dx = -1; dx < 2; dx += 1) {
285 for (
int dy = -1; dy < 2; dy += 1) {
286 bMat(i, i + dx + dy * width) += coeffs[dx+1][dy+1];
291 bMat(i, i) = borderPenalty;
295 if (fitForBackground) {
297 if (bMat.col(width*height).sum() != 0.) {
300 if (bMat.row(width*height).sum() != 0.) {
316 bool fitForBackground
335 if (borderPenalty < 0)
342 coeffs[0][0] = borderPenalty;
359 int nBgTerms = fitForBackground ? 1 : 0;
360 Eigen::MatrixXd bTot = Eigen::MatrixXd::Zero(width * height + nBgTerms, width * height + nBgTerms);
363 for (order = orders.
begin(); order != orders.
end(); order++) {
364 if ((*order < 1) || (*order > 3))
367 Eigen::MatrixXd bMatX = Eigen::MatrixXd::Zero(width * height + nBgTerms,
368 width * height + nBgTerms);
369 Eigen::MatrixXd bMatY = Eigen::MatrixXd::Zero(width * height + nBgTerms,
370 width * height + nBgTerms);
372 for (
int i = 0; i < width*height; i++) {
373 int const x0 = i % width;
374 int const y0 = i / width;
376 int distX = width - x0 - 1;
377 int orderToUseX =
std::min(distX, *order);
378 for (
int j = 0; j < orderToUseX+1; j++) {
379 bMatX(i, i + j) = coeffs[orderToUseX][j];
382 int distY = height - y0 - 1;
383 int orderToUseY =
std::min(distY, *order);
384 for (
int j = 0; j < orderToUseY+1; j++) {
385 bMatY(i, i + j * width) = coeffs[orderToUseY][j];
392 if (fitForBackground) {
394 if (bTot.col(width*height).sum() != 0.) {
397 if (bTot.row(width*height).sum() != 0.) {
445 if (kernelListIn.
size() == 0) {
446 return kernelListOut;
449 Image image0(kernelListIn[0]->getDimensions());
450 for (
unsigned int i = 0; i < kernelListIn.
size(); i++) {
453 (void)kernelListIn[i]->computeImage(image0,
true);
462 Image
image(kernelListIn[i]->getDimensions());
463 (void)kernelListIn[i]->computeImage(
image,
false);
468 for (
int y = 0;
y <
image.getHeight();
y++) {
488 for (
int y = 0;
y <
image.getHeight();
y++) {
501 return kernelListOut;
513 unsigned int boundary_style,
514 unsigned int difference_style,
520 if (boundary_style > 2) {
523 if (difference_style > 1) {
559 unsigned int x_cen = 0, y_cen = 0;
560 unsigned int x_cen1 = 0, y_cen1 = 0;
561 unsigned int x_cen2 = 0, y_cen2 = 0;
562 unsigned int x_size = 0, y_size = 0;
565 if (difference_style == 0) {
571 x_size = y_size = order + 2;
575 coeffs[0][0][0] = -2;
581 coeffs[1][0][0] = -2;
583 coeffs[1][0][2] = -1;
587 coeffs[1][2][0] = -1;
592 coeffs[2][0][0] = -2;
594 coeffs[2][0][2] = -3;
600 coeffs[2][2][0] = -3;
611 if (difference_style == 1) {
620 coeffs[0][0][1] = -1;
623 coeffs[0][1][0] = -1;
644 coeffs[1][1][1] = -4;
661 coeffs[2][0][2] = -1;
671 coeffs[2][2][0] = -1;
674 coeffs[2][2][3] = -2;
679 coeffs[2][3][2] = -2;
693 Eigen::MatrixXd bMat = Eigen::MatrixXd::Zero(width*height+1, width*height+1);
696 for (
unsigned int i = 0; i < width*height; i++) {
698 unsigned int const x0 = i % width;
699 unsigned int const y0 = i / width;
701 unsigned int x_edge_distance = (x0 > (width - x0 - 1)) ? width - x0 - 1 : x0;
702 unsigned int y_edge_distance = (y0 > (height - y0 - 1)) ? height - y0 - 1 : y0;
703 unsigned int edge_distance = (x_edge_distance < y_edge_distance) ? x_edge_distance :
706 for (
unsigned int dx = 0; dx < x_size; dx++) {
707 for (
unsigned int dy = 0; dy < y_size; dy++) {
715 double this_coeff = 0;
718 if (boundary_style == 0) {
721 if (
y > height - 1 ||
x > width - 1) {
724 this_coeff = coeffs[order][dx][dy];
727 }
else if (boundary_style == 1) {
728 x = (width + x0 + dx - x_cen) % width;
729 y = (height + y0 + dy - y_cen) % height;
730 this_coeff = coeffs[order][dx][dy];
734 }
else if (boundary_style == 2) {
737 if (edge_distance == 0) {
743 else if (edge_distance == 1 && order > 0) {
744 x = (width + x0 + dx - x_cen1) % width;
745 y = (height + y0 + dy - y_cen1) % height;
746 if ((dx < 3) && (dy < 3)) { this_coeff = coeffs[1][dx][dy]; }
749 else if (edge_distance == 2 && order > 1){
750 x = (width + x0 + dx - x_cen2) % width;
751 y = (height + y0 + dy - y_cen2) % height;
752 if ((dx < 5) && (dy < 5)) { this_coeff = coeffs[2][dx][dy]; }
755 else if (edge_distance > order) {
756 x = (width + x0 + dx - x_cen) % width;
757 y = (height + y0 + dy - y_cen) % height;
758 this_coeff = coeffs[order][dx][dy];
763 bMat(i,
y*width +
x) = this_coeff;
775 Eigen::MatrixXd hMat = bMat.transpose() * bMat;