Main Content

Create and Compare Resizing Interpolation Kernels

This example shows how to define a kernel for image resizing and compare different interpolation kernels on a sample image.

An interpolation kernel calculates the value of a pixel using a weighted average of neighboring pixel values. The imresize function offers many built-in kernels that perform bilinear, bicubic, and Lanczos resampling. You can also define a custom kernel and then resize images using the custom kernel.

To evaluate and compare interpolation kernels, this example magnifies a small image using each kernel. Fully assessing the performance of interpolation kernels requires examining a variety of different images and scale factors.

Create Kernels Based on imresize Interpolation Methods

The bilinear method uses a triangular interpolation kernel, which is defined as:

f(x)={1-|x||x|10otherwise

Create a bilinear interpolation kernel using a function called triangleResampling. This function is defined in the Helper Functions section at the end of this example. Then, display the bilinear interpolation kernel for a neighborhood of [-3.5, 3.5].

nhood = [-3.5 3.5];
fplot(@triangleResampling,nhood)
title("Bilinear (Triangular) Interpolation Kernel")

Figure contains an axes object. The axes object with title Bilinear (Triangular) Interpolation Kernel contains an object of type functionline.

The bicubic method uses this piecewise cubic interpolation kernel:

f(x)={1.5|x|3-2.5|x|2+1|x|1-0.5|x|3+2.5|x|2-4|x|+21|x|20otherwise

Create a bicubic interpolation kernel using a helper function called bicubicResampling. This function is defined in the Helper Functions section at the end of this example. Then, display the bicubic interpolation kernel.

fplot(@bicubicResampling,nhood)
title("Bicubic Interpolation Kernel")

Figure contains an axes object. The axes object with title Bicubic Interpolation Kernel contains an object of type functionline.

The lanczos2 and lanczos3 kernels are based on the Lanczos family of interpolation kernels. The Lanczos kernels are defined as follows, with a=2 or a=3, respectively:

f(x)={sinc(x)sinc(x/a)|x|a0otherwise

Create a lanczos2 and lanczos3 interpolation kernel using a function called lanczosResampling and specifying the factor a.

lanczos2 = @(x) lanczosResampling(x,2);
lanczos3 = @(x) lanczosResampling(x,3);

Display the lanczos2 and lanczos3 interpolation kernels.

fplot(lanczos2,nhood)
hold on
fplot(lanczos3,nhood)
hold off
legend(["Lanczos 2","Lanczos 3"])
title("lanczos2 and lanczos3 Interpolation Kernels")

Figure contains an axes object. The axes object with title lanczos2 and lanczos3 Interpolation Kernels contains 2 objects of type functionline. These objects represent Lanczos 2, Lanczos 3.

Define Custom Interpolation Kernel

Consider a piecewise rational function based on osculatory rational interpolation [1].

f(x)={-0.168|x|2-0.9129|x|+1.0808|x|2-0.8319|x|+1.0808|x|10.1953|x|2-0.5858|x|+0.3905|x|2-2.4402|x|+1.76761<|x|202<|x|

Create a custom interpolation kernel that performs osculatory rational interpolation using a function called oscResampling. This function is defined in the Helper Functions section at the end of this example. Then, display the custom interpolation kernel.

fplot(@oscResampling,nhood)
title("Custom Osculatory Rational Interpolation Kernel")

Figure contains an axes object. The axes object with title Custom Osculatory Rational Interpolation Kernel contains an object of type functionline.

Resize Image Using Interpolation Kernels

Read and display a small icon image at 100% magnification.

A = imread("region-analyzer-icon.png");
imshow(A,"InitialMagnification",100)

Figure contains an axes object. The hidden axes object contains an object of type image.

Resize the image by a factor f using each built-in interpolation method. Note that the nearest-neighbor method does not take a weighted average of neighborhood pixels. Instead, the nearest neighbor method assigns the output pixels the value of the nearest input pixel.

f = 10;
B_nearest = imresize(A,f,'nearest');
B_bilinear = imresize(A,f,'bilinear');
B_bicubic = imresize(A,f,'bicubic');
B_lanczos2 = imresize(A,f,'lanczos2');
B_lanczos3 = imresize(A,f,'lanczos3');

To resize the image using the custom kernel, specify the function handle and the nonzero kernel width as the method argument for imresize:

width = 4;
B_osc = imresize(A,f,{@oscResampling,width});

Display the resized images as a tiled image, and compare the results subjectively.

The nearest-neighbor result (upper left) appears quite blocky. The bilinear result (upper center) is better in most respects than the nearest-neighbor result but looks a little blurry. The bicubic result (upper right) and lanczos2 result (lower left) appear very similar and are sharper than the bilinear result. For example, look closely at the digits "3" and "8" near the top of the image. The lanczos3 result (lower center) is sharper than the bicubic and lanczos2 results but exhibits a visible "ringing" artifact. The ringing artifact appears as a faint echo outside the gray boundary, or by looking just to the left and right of the thick black stripe running down the middle of the image.

The custom interpolation result (lower right) is slightly sharper than the bicubic and lanczos2 results with slightly smoother diagonal edges. The custom interpolation result does not exhibit a ringing artifact.

t = imtile({B_nearest,B_bilinear,B_bicubic, ...
    B_lanczos2,B_lanczos3,B_osc},BackgroundColor="white");
imshow(t)

Figure contains an axes object. The hidden axes object contains an object of type image.

Helper Functions

The triangleResampling helper function performs bilinear interpolation.

function f = triangleResampling(x)
    f = (1 - abs(x)) .* (abs(x) <= 1);
end

The bicubicResampling helper function performs bicubic interpolation.

function f = bicubicResampling(x)
    absx = abs(x);
    absx2 = absx.^2;
    absx3 = absx.^3;
    
    f = (1.5*absx3 - 2.5*absx2 + 1) .* (absx <= 1) + ...
        (-0.5*absx3 + 2.5*absx2 - 4*absx + 2) .* ...
        ((1 < absx) & (absx <= 2));
end

The lanczosResampling helper function performs Lanczos interpolation using a specified factor a.

function f = lanczosResampling(x,a)
    f = a*sin(pi*x) .* sin(pi*x/a) ./ ...
        (pi^2 * x.^2);
    f(abs(x) > a) = 0;
    f(x == 0) = 1;
end

The oscResampling helper function performs osculatory rational interpolation.

function f = oscResampling(x)
    absx = abs(x);
    absx2 = absx.^2;
    
    f = (absx <= 1) .* ...
        ((-0.168*absx2 - 0.9129*absx + 1.0808) ./ ...
        (absx2 - 0.8319*absx + 1.0808)) ...
        + ...
        ((1 < absx) & (absx <= 2)) .* ...
        ((0.1953*absx2 - 0.5858*absx + 0.3905) ./ ...
        (absx2 - 2.4402*absx + 1.7676));
end

References

[1] Hu, Min, and Jieqing Tan. "Adaptive Osculatory Rational Interpolation for Image Processing." Journal of Computational and Applied Mathematics 195, no. 1–2 (October 2006): 46–53. https://doi.org/10.1016/j.cam.2005.07.011.

See Also