File Exchange

image thumbnail

Magic kernel resizing

version 1.3.0.0 (2.46 KB) by Jan Motl
The “magic kernel” is a method of resampling images that gives clear results free of artifacts.

1 Download

Updated 07 Mar 2018

View License

The “magic kernel” is a method of resampling images that gives amazingly clear results (free of “aliasing” artifacts, free of “ringing”, and free of “width beat” for thin features) yet is lightning fast (the simplest version only requires a few integer additions and bit-shifts).
The full method description is available at: https://web.archive.org/web/20170707165835/http://johncostella.webs.com/magic/

Cite As

Jan Motl (2020). Magic kernel resizing (https://www.mathworks.com/matlabcentral/fileexchange/40392-magic-kernel-resizing), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (10)

Jan Motl

@drdrb: Changed. Thank you!

@bmv: Only "nearest neighbor" resizing (out of all that I know) is going to pass this test. All other methods (bilinear, bicubic, triangle, lanczos2, lanczos3, magic) perform blurring and fail this test.

sara

drdrb

% Also use this:

output = double(output(...
2:2:end-1,2:2:end-1));

% instead of this:

[height,width]=size(image);
height2=ceil(height/2);
width2=ceil(width/2);
height2l=floor(height/2);
width2l=floor(width/2);
matrix1=spalloc(height2,height,height2);
for j=1:height2l
matrix1(j,j*2)=1;
end
matrix2=spalloc(width,width2,width2);
for j=1:width2l
matrix2(j*2,j)=1;
end
output=matrix1*double(output)* matrix2;
output=output(1:end-1,1:end-1);

%sparse matrices are fancy, but just %avoiding "for" at any price(even %readability) can get you farther.

drdrb

% Also use this:

output = double(output(...
2:2:end-1,2:2:end-1));

% istaid of this:

[height,width]=size(image);
height2=ceil(height/2);
width2=ceil(width/2);
height2l=floor(height/2);
width2l=floor(width/2);
matrix1=spalloc(height2,height,height2);
for j=1:height2l
matrix1(j,j*2)=1;
end
matrix2=spalloc(width,width2,width2);
for j=1:width2l
matrix2(j*2,j)=1;
end
output=matrix1*double(output)* matrix2;
output=output(1:end-1,1:end-1);

%sparse matrices are fancy, but just %avoiding "for" at any price(even %readability) can get you farther.

drdrb

% Thank you!
% It's just what I was searching for!
% Great piece of code
% Just to note that this:

output=zeros(2*size(image));
output(2:2:end,2:2:end)=image;

% is much faster then this:

[height,width]=size(image);
matrix1=spalloc(2*height,height,height);
for i=1:height
matrix1(2*i,i)=1;%#ok
end
matrix2=spalloc(width,2*width,width);
for i=1:width
matrix2(i,2*i)=1;%#ok
end
output=matrix1*double(image)*matrix2;

bmv

I mean : B=magickernel(magickernel(A),'DOWN');

bmv

After these transformations:
A;
B=magickernel(magickernel(A),'DAWN');
Should A and B be the same? (A=B ?)

Anton Semechko

The cambridgeincolour link is pretty awesome. Thanks!

Jan Motl

It's a trade-off. Either you can have sharp images with halos (that's the case of bicubic interpolation) or blurred images without halos (like with Magic kernel).

More details about the interpolation trade-off are available at http://www.cambridgeincolour.com/tutorials/digital-photo-enlargement.htm

The artifacts particularly present in bicubic interpolation are described, for example, at Wikipedia: https://en.wikipedia.org/wiki/Bicubic_interpolation#Use_in_computer_graphics

Magic kernel is useful if you need extreme blow-ups or if you further post process the image and the used algorithm is sensitive to artifacts introduced by interpolation.

Anton Semechko

So what are the artifacts that are present in the image interpolated with a cubic spline?

Updates

1.3.0.0

The link replaced with the snapshot from web archive.

1.3.0.0

Streamlined the implementation

1.2.0.0

Fixed typo in the illustration image.

1.1.0.0

Typo.

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