isce3  0.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
2D Interpolation Tutorial

Tutorial

2D interpolation is a fundamental operation for image resampling, oversampling, terrain extraction, etc. ISCE currently implements several basic types of interpolation methods: bilinear, bicubic, spline, and sinc interpolation. For this example, we will test the first three interpolators on a synthetic low-resolution image.

// std
#include <cmath>
#include <cstdio>
#include <vector>
// isce3::core
#include "isce3/core/Constants.h"
#include "isce3/core/Interpolator.h"
int main() {
// Create coordinates for test data
std::vector<double> xvals = isce3::core::arange(-5.01, 5.01, 0.25);
std::vector<double> yvals = isce3::core::arange(-5.01, 5.01, 0.25);
size_t nx = xvals.size();
size_t ny = yvals.size();
// Fill matrix values with function z = sin(x**2 + y**2)
for (size_t i = 0; i < ny; ++i) {
for (size_t j = 0; j < nx; ++j) {
M(i,j) = std::sin(yvals[i]*yvals[i] + xvals[j]*xvals[j]);
}
}
// Create different interpolators
isce3::core::Interpolator<double> * bilinear = isce3::core::createInterpolator<double>(
isce3::core::BILINEAR_METHOD);
isce3::core::Interpolator<double> * bicubic = isce3::core::createInterpolator<double>(
isce3::core::BICUBIC_METHOD);
isce3::core::Interpolator<double> * spline = isce3::core::createInterpolator<double>(
isce3::core::BIQUINTIC_METHOD);
// Create coordinates we want to interpolate at
const size_t npts = 500;
std::vector<double> xnew = isce3::core::linspace(0.0, 1.0*(nx - 1), npts);
std::vector<double> ynew(npts, 1.0 * (ny / 2));
// Create vectors for storing interpolated results
std::vector<double> z_bilinear(npts), z_bicubic(npts), z_spline(npts);
// Loop over points to perform interpolation
for (size_t i = 0; i < npts; ++i) {
z_bilinear[i] = bilinear->interpolate(xnew[i], ynew[i], M);
z_bicubic[i] = bicubic->interpolate(xnew[i], ynew[i], M);
z_spline[i] = spline->interpolate(xnew[i], ynew[i], M);
}
// Print results to screen
for (size_t i = 0; i < npts; ++i) {
printf("%12.6f %12.6f %12.6f\n", z_bilinear[i], z_bicubic[i], z_spline[i]);
}
// Clean up
delete bilinear;
delete bicubic;
delete spline;
return 0;
}

We first generated a synthetic 2D low-resolution image which we wish to interpolate (this code follows the SciPy interp2d tutorial test data). The data are stored in an isce3::core::Matrix object which will be passed to all interpolation routines. Then, three different interpolators are created. Note that they are all instantiated as isce3::core::Interpolator pointers, which is the base class for all derived interpolator classes. The function isce3::core::createInterpolator takes as input an enum type that specifies the type of interpolation method we wish to use and then returns a pointer to the appropriate derived interpolation class. After creating vectors at which we want to perform the interpolation, we call each interpolator's interpolate() function. The plotted interpolation results are below:

interp_demo_cpp.png

Generated for ISCE3.0 by doxygen 1.8.5.