## Digital Image Zooming on MATLAB using spline Interpolation

In my previous article on image zooming I shared how I implemented the basic image zooming methods namely ‘nearest neighbour’, ‘bi-linear’, ‘bi-cubic’ on MATLAB. In this article I will share how I used MATLAB built-in function ‘spline ()’ to zooms images on MATLAB.

### Function spline () on MATLAB

Just for getting basic idea let’s have a brief look on the MATLAB spline function. MATLAB spline function performs cubic spline interpolation (on Wikipedia, gatech.edu) for a given data set. The spline interpolation function is as below.

yy = spline(x, y, xx)

The yy[] is the cubic spline interpolated value for the values y[] if underlying function at the breakpoints defined by x[] for the interpolant xx. The first example found in MATLAB help (‘doc spline’) is as below.

x = 0:10; y = sin(x); xx = 0:.25:10; yy = spline(x,y,xx); plot(x,y,'o',xx,yy);

### My method on image zooming using spline interpolation

In my attempt on the 2D interpolation first I interpolated the whole image in row by row so that the number of columns will be equal to the desired column size of the zoomed image and next I interpolated the resultant image column-wise to obtain the required number of rows in the zoomed image. This can be graphically represented as follows.

Raw interpolation code is as below.

factor = 1/zoom; x = 1:c; xx = 1:factor:(c+1-factor); for i = 1:r cs(i,:,1) = spline(x,[0 img(i,:,1) 0],xx); cs(i,:,2) = spline(x,[0 img(i,:,2) 0],xx); cs(i,:,3) = spline(x,[0 img(i,:,3) 0],xx); end

Column interpolation code is as below.

y = 1:r; yy = 1:factor:(r+1-factor); for i = 1:(zoom*(c)) us(:,i,1) = (spline(y,[0 cs(:,i,1)' 0],yy))'; us(:,i,2) = (spline(y,[0 cs(:,i,2)' 0],yy))'; us(:,i,3) = (spline(y,[0 cs(:,i,3)' 0],yy))'; end

With this interpolation images can be zoomed with much better results. Test code for the system is as below. In here I used RGB image with the data type of ‘uint8’.

clc; clear all; imIn = imread('image.jpg'); im = imresize(imIn,0.5,'nearest'); [r c d] = size(im); img = cast(im,'double'); zoom = 3; factor = 1/zoom; x = 1:c; xx = 1:factor:(c+1-factor); for i = 1:r cs(i,:,1) = spline(x,[0 img(i,:,1) 0],xx); cs(i,:,2) = spline(x,[0 img(i,:,2) 0],xx); cs(i,:,3) = spline(x,[0 img(i,:,3) 0],xx); end y = 1:r; yy = 1:factor:(r+1-factor); for i = 1:(zoom*(c)) us(:,i,1) = (spline(y,[0 cs(:,i,1)' 0],yy))'; us(:,i,2) = (spline(y,[0 cs(:,i,2)' 0],yy))'; us(:,i,3) = (spline(y,[0 cs(:,i,3)' 0],yy))'; end us = cast(us,'uint8'); imshow(us);

Finally I implemented the code as a MATLAB function.

function imZoom = spzoom(im,zoom) [r c d] = size(im); img = cast(im,'double'); factor = 1/zoom; x = 1:c; xx = 1:factor:(c+1-factor); for i = 1:r cs(i,:,1) = [spline(x,[0 img(i,:,1) 0],xx),img(i,end,1)]; if d > 1 cs(i,:,2) = [spline(x,[0 img(i,:,2) 0],xx),img(i,end,2)]; cs(i,:,3) = [spline(x,[0 img(i,:,3) 0],xx),img(i,end,3)]; end end y = 1:r; yy = 1:factor:(r+1-factor); for i = 1:(zoom*c) us(:,i,1) = (spline(y,[0 cs(:,i,1)' 0],yy))'; if d > 1 us(:,i,2) = (spline(y,[0 cs(:,i,2)' 0],yy))'; us(:,i,3) = (spline(y,[0 cs(:,i,3)' 0],yy))'; end end imZoom = cast(us,'uint8');

Results are as below.

Please note that this is not a correct implementation and I just wanted to share the idea that there is a way to zoom images using spline interpolation since I haven’t able to find MATLAB function to perform such zoom operation. Thank you very much for reading.

Hi,

Thank you very much, this is very helpful for my numeric methods class project.

Comment by Szabó Hunor | 2011 May 5 |

🙂 🙂

Comment by Thilina S. | 2011 May 6 |

Hi,

I have an erro:

??? Error using ==> spline

Too many input arguments.

Error in ==> proba at 12

cs(i,:,1) = spline(x,[0 img(i,:,1) 0],xx);

Your matlab didn’t write this?

Thank you,

Have a nice weekend.

Comment by Szabó Hunor | 2011 May 7 |

Hi,

I have an erro:

??? Error using ==> spline

Too many input arguments.

Error in ==> proba at 12

cs(i,:,1) = spline(x,[0 img(i,:,1) 0],xx);

Your matlab didn’t write this?

Thank you,

Have a nice weekend.

Comment by aseel | 2019 January 6 |

Hi,

I haven’t got such problem while implementing this code,

my test file is like this,

Comment by Thilina S. | 2011 May 8 |

The same error. I’m using matlab 2010B.

??? Error using ==> spline

Too many input arguments.

Comment by Szabó Hunor | 2011 May 8 |

just type ‘doc spline’ in MATLAB command prompt and check the input format for the spline function in your MATLAB version. I’ll also check the code with MATLAB 2010b

Comment by Thilina S. | 2011 May 8 |

Is is the same as on the internet.

Comment by Szabó Hunor | 2011 May 8 |

Like the matlab official page.

Comment by Szabó Hunor | 2011 May 8 |

Hi,

One of my friends tested the code on MATLAB 2010b, its working in 2010b

Comment by Thilina S. | 2011 May 8 |

Now, I will install matlab for an another computer and test it.

Thank you,

Comment by Szabó Hunor | 2011 May 8 |

It’s working in my another pc. Thank you.

Now, i try to understand how it’s work the algorithm.

Thank you,

Comment by Szabó Hunor | 2011 May 8 |

At this line: spline(x,[0 img(i,:,1) 0],xx)

You calculate zoom*r piece value. Example: 2*2 image zoom=3. The result has 6 pieces, why add the value “img(i,end,1)”? Is 7 pieces in this row?

Comment by Szabó Hunor | 2011 May 8 |

Well, If you divide a region to n pieces, spline interpolation returns n-1 values. The last one I added is the nth element of the array..

Comment by Thilina S. | 2011 May 8 |

can u use spline functionHDR SCENE

Comment by HIMEN | 2012 April 4 |

Hi, I think yes, I haven’t tried yet

Comment by Thilina S. | 2012 April 19 |

Hi Sir,

i m nanthini…in my project i need to interpolate images,,,,i mean…whatever the number of images be….i have to interpolate them to 10 images….using b-spline interpolation….could you help me with this….please….

reply me to nanthinianbu21@gmail.com….thank u…..

waiting for your reply….

Comment by nanthini | 2013 January 31 |

hi.

Im have need coding by cubic spline and cubic B-spline for image processing by matlab .

Comment by simon | 2013 April 6 |

hi.

i need to intrpolation cubic spline and cubic B-spline for my image by matlab,do you help my ?

Comment by simon | 2013 April 7 |

Hi ..plz explain how the image can be zoomed by odd factor like 1.5 etc.

Comment by sush | 2014 March 19 |

hello every one, when i use spline code for image interpolation for number of values like 1.1,1.2,1.3……. using either while or for loop then it give error msg……..

Subscripted assignment dimension mismatch…………..

please help as soon as possible……………..

Comment by mudit | 2014 April 7 |

Hi

Thank you very much for your codes. I’m working on Bspline image interpolation. Is it possible for you to write matlab code for image zooming using by Bspline basis functions? Or do you have any matlab code related to Bspline image interpolation?

Comment by mojgan | 2015 August 22 |