4

how can I use my function as an interpolation method for imresize function in MATLAB?

I read MATLAB's help about the way to use a custom function for interpolation method, but there was not any clear example. and I tried to write a code for ma

Sam Roberts
  • 23,951
  • 1
  • 40
  • 64
user1033629
  • 43
  • 1
  • 3
  • Please edit and post some code if the answers aren't what you expected. If one answer helps you, please click the "up" arrow to notify that the question has been answered. – Hugues Fontenelle Nov 07 '11 at 15:08
  • related question: [Resizing in MATLAB w/ different filters](http://stackoverflow.com/questions/7758078/resizing-in-matlab-w-different-filters) – Amro Nov 07 '11 at 20:03

2 Answers2

5

The imresize command will by default use the bicubic method. You can alternatively specify one of several other built-in interpolation methods or kernels, such as

imNewSize = imresize(imOldSize, sizeFactor, 'box')

for a box-shaped kernel. If you want to specify your own bespoke kernel, you can pass that in as a function handle, along with a kernel width, in a cell array. For example, to implement the box-shaped kernel yourself (without using the built-in one) with a kernel width of 4, try:

boxKernel = @(x)(-0.5 <= x) & (x < 0.5);
imNewSize = imresize(imOldSize, sizeFactor, {boxKernel, 4});

If you type edit imresize and look inside the function, from about line 893 you can find implementations of the other built-in kernels, which may give you some hints about how you can implement your own.

Sam Roberts
  • 23,951
  • 1
  • 40
  • 64
4

Here is how you call the resizing function, for an image A that would want to resize to 64x52, with a special kernel "lanczos2":

B = imresize(A, [64 52], {@lanczos2,4.0} );

Here is one example of one interpolation kernel, which you would save as lanczos2.m

function f = lanczos2(x)
f = (sin(pi*x) .* sin(pi*x/2) + eps) ./ ((pi^2 * x.^2 / 2) + eps);
f = f .* (abs(x) < 2);
end

Note that this particular kernel is already implemented in imresize.m I think that your issue had to do with the "@" which serves for referencing functions.

Hugues Fontenelle
  • 5,275
  • 2
  • 29
  • 44