Bicubic (Catrom) image downsampling without antialiasing?

Discuss digital image processing techniques and algorithms. We encourage its application to ImageMagick but you can discuss any software solutions here.
Post Reply
schrodingerscat
Posts: 3
Joined: 2019-08-20T09:16:01-07:00
Authentication code: 1152

Bicubic (Catrom) image downsampling without antialiasing?

Post by schrodingerscat » 2019-08-20T09:26:47-07:00

Hi,
I was wondering, does anybody know how to perform bicubic (catrom) image downsampling without executing the antialiasing step beforehand?

If I run the command

Code: Select all

convert image_file -resize 160x120 -interpolate Catrom -size 160x120 xc:black +swap -gravity center -depth 8 -composite destinFileName 
The image gets resized with anti-aliasing performed beforehand. Ive confirmed this by running the above command on the "rings" image. http://www.imagemagick.org/Usage/filter ... m_orig.gif

The resulting image looks like this:
http://www.imagemagick.org/Usage/filter ... resize.png

If antialiasing was disabled, it should look like this: http://www.imagemagick.org/Usage/filter ... sample.png
I have confirmed this using Matlab and the command

Code: Select all

image = imread('rings_sm_orig.gif');
figure, imshow(imresize(image, [120 NaN], 'Antialiasing',false));
(Note: Matlab uses bicubic/Catrom resizing by default).

Unfortunately, the additional parameter "+antialias" appears to have no effect on the imageMagick command above.

Anyone have any suggestions on how to disable it for bicubic/catrom interpolated resizing?

User avatar
fmw42
Posts: 25415
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Bicubic (Catrom) image downsampling without antialiasing?

Post by fmw42 » 2019-08-20T09:29:50-07:00

Anthony would have to look at this whenever he gets online. He was the developer. Unfortunately, he rarely contributes here at this time. What antialiasing step? I was not aware of any.

This is the code from pixel.c

Code: Select all

static inline void CatromWeights(const MagickRealType x,
  MagickRealType (*weights)[4])
{
  /*
    Nicolas Robidoux' 10 flops (4* + 5- + 1+) refactoring of the
    computation of the standard four 1D Catmull-Rom weights. The
    sampling location is assumed between the second and third input
    pixel locations, and x is the position relative to the second
    input pixel location. Formulas originally derived for the VIPS
    (Virtual Image Processing System) library.
  */
  MagickRealType
    alpha,
    beta,
    gamma;

  alpha=(MagickRealType) 1.0-x;
  beta=(MagickRealType) (-0.5)*x*alpha;
  (*weights)[0]=alpha*beta;
  (*weights)[3]=x*beta;
  /*
    The following computation of the inner weights from the outer ones
    works for all Keys cubics.
  */
  gamma=(*weights)[3]-(*weights)[0];
  (*weights)[1]=alpha-(*weights)[0]+gamma;
  (*weights)[2]=x-(*weights)[3]-gamma;
}
The code should implement the original Keys Cubic Convolution from his paper.

These are the diagrams that I originally sent to Anthony along with the Keys paper when I asked him to implement it.

Image

Image

snibgo
Posts: 12024
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Bicubic (Catrom) image downsampling without antialiasing?

Post by snibgo » 2019-08-20T10:16:04-07:00

Your command included:
schrodingerscat wrote:... -resize 160x120 -interpolate Catrom ...
If you want the interpolate setting to effect the resize, it must come before it, not after. But "-interpolate" has no effect on "-resize". Perhaps you intended "-filter".
schrodingerscat wrote:I was wondering, does anybody know how to perform bicubic (catrom) image downsampling without executing the antialiasing step beforehand?
As far as I know, "-resize" never performs an antialiasing step beforehand. Nor does any other IM operation. "-resize" calculates each new pixel from a weighted average of a number of input pixels. The nature of the averaging depend on the filter method.

If you don't want weighted averaging, you can use "-sample" instead of "-resize".
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 25415
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Bicubic (Catrom) image downsampling without antialiasing?

Post by fmw42 » 2019-08-20T11:22:49-07:00

Try

Code: Select all

convert image_file -filter catrom -resize 160x120 -background black -gravity center -extent 160x120 destinFileName 

schrodingerscat
Posts: 3
Joined: 2019-08-20T09:16:01-07:00
Authentication code: 1152

Re: Bicubic (Catrom) image downsampling without antialiasing?

Post by schrodingerscat » 2019-08-21T03:40:25-07:00

Thanks for the replies folks.
OK, I am confused now myself. So I have gone back over my tests to confirm them.

I have taken the following 200 x 200 image:
Image

In Matlab,
I resize this to 120 x 160 with aliasing enabled (their default method):

Code: Select all

image = imread('rings_sm_orig.gif');
outImage = imresize(image, [120 NaN], 'bicubic', 'Antialiasing',true); % note: this is the same as the default imresize(image, [120 NaN]);
imwrite(outImage, 'matlabBicubicDownsamplingWithAliasing.png');
Image

I resize this to 120 x 160 with aliasing disabled:

Code: Select all

outImage = imresize(image, [120 NaN], 'bicubic', 'Antialiasing',false);
imwrite(outImage, 'matlabBicubicDownsamplingWithoutAliasing.png');
Image

In ImageMagick,
I resize this to 120 x 160 using the command snibgo and fmw42 gave me (thank you for that):

Code: Select all

convert image_file -filter catrom -resize 160x120 -background black -gravity center -extent 160x120 destinFileName
Image

Ignoring the black borders (the inner image is still 120 x 120, the same as Matlabs output), we can see that the image is more similar in appearance to Matlabs default method. Matlabs default method uses antialiasing before bicubic interpolation when shrinking an image. Based on this test, I would say that the ImageMagick is using antialiasing before Catrom/bicubic interpolation when shrinking an image too.

@fmw42, this is why I am confused when you say that unlike Matlab, ImageMagick does not perform antialiasing before shrinking images. Maybe I am wrong somewhere with my test above, but I cant see it.

snibgo
Posts: 12024
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Bicubic (Catrom) image downsampling without antialiasing?

Post by snibgo » 2019-08-21T06:43:39-07:00

I don't know Matlab, or what it does in what sequence.

IM doesn't have a anti-alias operation. There is no mechanism to "anti-alias" an image. I don't know what such an operation might do.

IM does have an "-antialias" setting that affects some operations. For example, when drawing a diagonal black line on a white background, the result might be just black on white (the image is aliased, or stepped or jagged) or it might include gray pixels to create a smoother result (antialiased).

"-antialias" has no effect on "-resize", or any other IM operation that shrinks an image.
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 25415
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Bicubic (Catrom) image downsampling without antialiasing?

Post by fmw42 » 2019-08-21T08:51:50-07:00

I suspect that the ImageMagick resize is using a different method that does a better job of dealing with artifacts. Sorry, I do not know what algorithm/code Matlab is using.

See

https://imagemagick.org/Usage/filter/
https://imagemagick.org/Usage/filter/nicolas/

You can control the filter settings with https://imagemagick.org/Usage/filter/#cubic_bc and https://imagemagick.org/Usage/filter/#options

See also https://imagemagick.org/Usage/filter/#compare

schrodingerscat
Posts: 3
Joined: 2019-08-20T09:16:01-07:00
Authentication code: 1152

Re: Bicubic (Catrom) image downsampling without antialiasing?

Post by schrodingerscat » 2019-08-22T02:43:57-07:00

No problem, thanks for the help guys. At the very least I have learnt a bit here! :-)

Post Reply