Code covered by the BSD License  

Highlights from
Image Interpolation (ba_interp2)


3.2 | 5 ratings Rate this file 53 Downloads (last 30 days) File Size: 1.89 MB File ID: #20342
image thumbnail

Image Interpolation (ba_interp2)



18 Jun 2008 (Updated )

Fast nearest, bi-linear and bi-cubic interpolation for image data

| Watch this File

File Information

% 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


This file inspired 3 D Volume Interpolation With Ba Interp3: Fast Interp3 Replacement.

MATLAB release MATLAB 7.8 (R2009a)
Other requirements mex compiler
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (12)
26 Jul 2012 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);

04 Oct 2011 Jaroslaw Tuszynski

can not compile mex file

10 Feb 2011 Brian

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,


10 Feb 2011 Michael

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!

23 Jan 2011 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.

20 Jan 2011 Brian

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.


19 Jan 2011 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.

03 Nov 2010 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.

03 Nov 2010 Vahid Kazemi  
09 Oct 2010 Rob Campbell

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

06 Oct 2010 Brian

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.

29 Mar 2010 Courtney

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

08 Oct 2010

- Add single precision handling.
- Updated the string comparison function, such that it might work under visual studio (tested only with linux)

08 Nov 2010

Included patch by Vahid Kazemi.

Contact us