File Exchange

image thumbnail

Image Interpolation (ba_interp2)

version (1.89 MB) by Brian
Fast nearest, bi-linear and bi-cubic interpolation for image data


Updated 08 Oct 2010

View Version History

View License

% Fast nearest, bi-linear and bi-cubic interpolation for image data
% Usage:
% ------
% Z = ba_interp2(F, X, Y, [method])
% where method is one off nearest, linear, or cubic.
% F is a WxHxD Image with an arbitrary number of layers D.
% X, Y are I_1 x ... x I_n matrices with the x and y coordinates to
% interpolate.
% Z is a I_1 x ... x I_n x D matrix, which contains the interpolated image channels.
% Notes:
% ------
% This method handles the border by repeating the closest values to the point accessed.
% This is different from matlabs border handling.
% Example
% ------
% %% The mandrills eye
% clear
% IMG=load('mandrill');
% IMG = ind2rgb(IMG.X,;
% [Dx Dy] = meshgrid(130:0.1:250, -150:0.1:-50);
% R = [cos(pi/4) sin(pi/4); -sin(pi/4) cos(pi/4)];
% RD = R * [Dx(:)'; Dy(:)'] + 250;
% RDx = reshape(RD(1,:), size(Dx));
% RDy = reshape(RD(2,:), size(Dy));
% methods = {'nearest', 'linear', 'cubic'};
% la=nan(1,3);
% for i=1:3
% la(i) = subplot(2,2,i);
% tic;
% IMG_R = ba_interp2(IMG, RDx, RDy, methods{i});
% elapsed=toc;
% imshow(IMG_R);
% title(sprintf('Rotation and zoom using %s interpolation took %gs', methods{i}, elapsed));
% end
% linkaxes(la);
% License:
% --------
% (c) 2008 Brian Amberg

Cite As

Brian (2021). Image Interpolation (ba_interp2) (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (15)

Raghvendra Verma

how to put other images for processing

jia guan

Thusitha Chandrapala

Work well on linux machines, but doesntcompile on windows either with the lcc or VS2010 compiler(32bit).

As Vahid said, adding

#ifdef _WIN32
inline double round( double d )
return floor( d + 0.5 );


Thomas Hampshire

Great piece of code. However will not compile on windows machines due to lack of a round() function in the c standard. Including the following piece of code in the cpp file will allow it to compile:

static inline double round(double val){
return floor(val + 0.5);

Jaroslaw Tuszynski

can not compile mex file


Hello Michael,

it has a slightly different feature set.
The differences are:
- No support for X, Y, you have to do that yourself to the XI and YI variables.
That behaviour is what I need most often, and it saves a few cycles.
- Support for Z with dimensions HxWxD1xD2x..., returning Z(XI,YI,:,:,:,...) in slightly abused notation. I.e. it treats the dimensions following the first two dimensions as vectors and interpolates these. This is useful for color image interpolation, where you have arrays of size [H x W x 3].

I hope that helps,



Hi Brian,

I just wanted to verify without checking the entire code. Does your code do the same as
ZI = interp2(X,Y,Z,XI,YI)
? If not, what is the difference? Thanks! It works very fast!

Michael Chan

Hi Brian,

yes, I am using Windows. This also happens to ba_interp3 as well. Could you supply a windows version as well, as though it may be a controversial platforms, there are many such users. Thank you for your efforts.

I really want to put a 5 stars rating, however, I cannot see the effects.


Hello Michael,

have you tried to compile other mex files. Compilation works for me on Ubuntu and Fedora, but it seems that you are using Windows. I do not actively support windows, as I do not have a windows machine with matlab available.


Michael Chan

Hi Brian,

I am getting this error. Please advise me. Thank you.

>> mex -O ba_interp2.cpp
lcc preprocessor error: ba_interp2.cpp:55 Could not find include file <iostream>
Error ba_interp2.cpp: 57 syntax error; found `double' expecting `;'
Error ba_interp2.cpp: 64 syntax error; found `static' expecting `;'
Error ba_interp2.cpp: 72 syntax error; found `static' expecting `;'
Error ba_interp2.cpp: 74 syntax error; found `&' expecting `)'
Error ba_interp2.cpp: 74 skipping `&' `f00_i' `,'
Error ba_interp2.cpp: 75 missing name for parameter 1 to function `indices_linear'
Error ba_interp2.cpp: 75 syntax error; found `int' expecting `{'
Error ba_interp2.cpp: 75 empty declaration
Error ba_interp2.cpp: 75 syntax error; found `&' expecting `;'
Error ba_interp2.cpp: 75 undeclared identifier `f10_i'
Error ba_interp2.cpp: 76 illegal expression
Error ba_interp2.cpp: 76 undeclared identifier `f01_i'
Error ba_interp2.cpp: 77 illegal expression
Error ba_interp2.cpp: 77 undeclared identifier `f11_i'
Error ba_interp2.cpp: 78 illegal expression
Warning ba_interp2.cpp: 78 Statement has no effect
Error ba_interp2.cpp: 78 syntax error; found `int' expecting `;'
Error ba_interp2.cpp: 78 illegal statement termination
Error ba_interp2.cpp: 78 skipping `int'
Error ba_interp2.cpp: 78 undeclared identifier `x'
Error ba_interp2.cpp: 78 too many errors

C:\PROGRA~1\MATLAB\R2010A\BIN\MEX.PL: Error: Compile of 'ba_interp2.cpp' failed.

??? Error using ==> mex at 222
Unable to complete successfully.

Vahid Kazemi

You may want to add:

#ifdef _WIN32
inline double round( double d )
return floor( d + 0.5 );

at the beginning (after includes) of C++ file to compile the code on windows.

Vahid Kazemi

Rob Campbell

Courtney, it's unfair to give an author one star simply because you couldn't compile the code.


Hello Courtney,

the file is actually working well for me with gnu c++ compiler and intel compiler. If you could state the errors you receive under windows, I might be willing to fix it.


Won't run without compiling ba_interp2.cpp ba_interp2.cpp won't compile. Perhaps the mex file could be included for Windows?

MATLAB Release Compatibility
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!