File Exchange

## Image Interpolation (ba_interp2)

version 1.2 (1.89 MB) by

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

3.16667
6 Ratings

Updated

% 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 = ind2rgb(IMG.X, IMG.map);
% [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
%
% --------
% GPL
% (c) 2008 Brian Amberg
% http://www.brian-amberg.de/

### Comments and Ratings (13)

Thusitha Chandrapala

### Thusitha Chandrapala (view profile)

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 );
}
#endif

works!

Thomas Hampshire

### Thomas Hampshire (view profile)

Hi,
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

### Jaroslaw Tuszynski (view profile)

can not compile mex file

Brian

### Brian (view profile)

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,

Brian

Michael

### Michael (view profile)

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

### Michael Chan (view profile)

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.

Brian

### Brian (view profile)

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.

Brian

Michael Chan

### Michael Chan (view profile)

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

### Vahid Kazemi (view profile)

You may want to add:

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

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

Vahid Kazemi

Rob Campbell

### Rob Campbell (view profile)

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

Brian

### Brian (view profile)

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.

Courtney

### Courtney (view profile)

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

 8 Nov 2010 1.3 Included patch by Vahid Kazemi. 8 Oct 2010 1.2 - Add single precision handling. - Updated the string comparison function, such that it might work under visual studio (tested only with linux)
##### MATLAB Release
MATLAB 7.8 (R2009a)
##### Acknowledgements

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

Play today