user1033629
user1033629

Reputation: 43

MATLAB imresize with a custom interpolation kernel

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

Upvotes: 4

Views: 5498

Answers (2)

Hugues Fontenelle
Hugues Fontenelle

Reputation: 5435

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.

Upvotes: 4

Sam Roberts
Sam Roberts

Reputation: 24127

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.

Upvotes: 5

Related Questions