Different images between MATLAB and ImageMagick

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
filippogambarota
Posts: 2
Joined: 2019-09-19T02:02:12-07:00
Authentication code: 1152

Different images between MATLAB and ImageMagick

Post by filippogambarota » 2019-09-20T01:06:35-07:00

Hi,
I'm using ImageMagick 7.0.8-64 Q16 for Windows and MATLAB R2019. I'm doing the same operation (resizing with bicubic interpolation method) in both programs.

ImageMagick code:

Code: Select all

magick start.png -resize 400x200! -interpolate Catrom -quality 100 start_magick_100.png
MATLAB code

Code: Select all

start_matlab = imresize(start, [200 400], "bicubic")
imwrite(start_matlab, "start_matlab.png", "png")
Now, the difference is not visible but reading again both images in MATLAB and ImageJ they are a little bit different in terms of pixels count. I mean that the pixel count is the same but summing the image matrix in MATLAB give me a different result.
How can it be possible? Do IM and Matlab could have different functions even for these simple operations?

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

Re: Different images between MATLAB and ImageMagick

Post by snibgo » 2019-09-20T04:02:25-07:00

"-interpolate" is a setting. If you want it to affect the "-resize" operation, you need to put that before "-resize", not after. But "-interpolate" has no effect on "-resize".

"-filter" does have an effect on "-resize". For example, "-filter Cubic -resize 400x200!".

When IM makes each output pixel from a number of input pixels, it uses an EWA (Elliptical Weighted Average) resampling algorithm. I don't know if MATLAB does this, or if it uses the identical algorithm at identical precision.

In general, I would be surprised if two image processors gave exactly the same result for a non-trivial operation. The question is, how different are they?
snibgo's IM pages: im.snibgo.com

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

Re: Different images between MATLAB and ImageMagick

Post by fmw42 » 2019-09-20T08:53:08-07:00

You can avoid the EWA and use bilinear or spline interpolation by using

Code: Select all

-filter point -interpolate xxx
see

Code: Select all

convert -list interpolate
for options


Or better just use

Code: Select all

-filter Catrom
which is the same as Keys cubic convolution (bi-cubic)

Post Reply