sigmoidized EWA LanczosRadius3 ... and tensor Quadratic

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?".
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: sigmoidized EWA (-distort resize) LanczosRadius3

Post by NicolasRobidoux »

@Anthony: Will do. Need to sort out downsampling a little more.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: sigmoidized EWA (-distort resize) LanczosRadius3

Post by NicolasRobidoux »

Anthony:
Here it is. I used --- to indicate "section titles". Let me know if you want things changed (or change them).
I think it's OK if there are no picture examples. By then, people should be able to do their own tests.
Of course this is still work in progress: In particular, I certainly don't think I have the last word on downsampling. But I think this is a pretty good start. (Comments, from anyone, welcome, of course.)
(Quick note: Could you please spell my first name without an "h"? There are other Ni*o*la* Robidoux into IT, art and music (and at least one top lawyer), and I'd rather not be confused with them if it can be avoided. The most "famous" Nicholas Robidoux (with an "h") has (or had?) an arrest warrant out for violent felonies. It's not me, I swear.)

Methods recommended by Nicolas Robidoux
---------------------------------------

Short answer
------------

If you are going to use one single filter for everything and would
rather avoid complications like color space manipulations and fancy
parameters, just use EWA (Elliptical Weighted Averaging) LanczosSharp

convert input.jpg -filter LanczosSharp -distort resize 500% output.jpg

Quality control
---------------

Don't use the following -colorspace and +-sigmoidal-contrast commands
if you use an 8-bit ImageMagick.

Don't use the following -resize commands unless you use HDRI
ImageMagick (more important when enlarging, less important with low
JPEG quality). Exceptions are indicated below.

The chosen conversions into and out of linear RGB assume sRGB input
and output images. Other color spaces may require tweaks.

These recommendations have not been carefully checked with an active
transparency channel.

Some of the commands may only work perfectly with recent ImageMagick.

Detailed answer
---------------

Downsampling (reducing the image's pixel count, for example producing
thumbnails) and upsampling (enlarging or, more generally, resampling
without downsampling much) produce different artifact mixes. For
this reason, different methods are recommended for each type of
geometrical operation.

Recommended upsampling methods
------------------------------

Sigmoidized EWA LanczosSharp is quite artifact free, if a bit blurry:

convert input.jpg -colorspace RGB +sigmoidal-contrast 8 \
-filter LanczosSharp \
-distort Resize 500% \
-sigmoidal-contrast 8 -colorspace sRGB output.jpg

"Sigmoidization" has to do with the pair of sigmoidal-contrast
commands. Basically, the +sigmoidal-contrast command converts a linear
RGB version of the image to a custom RGB color space with an S-shaped
"gamma curve" which is a little like piecing two sRGB transfer functions to make
an S, and the -sigmoidal-contrast command undoes the conversion. You may
reduce "color bleed" by backing the sigmoidal contrast down from 8 (to 6.5, for
example). You may decrease halos and increase perceptual sharpness by
increasing the sigmoidal contrast (up to 11.5, say). Higher contrasts are
especially recommended with greyscale images, which don't suffer from colour
bleed artifacts.
You may also skip it
altogether, by omitting the two sigmoidal-contrast commands, in which case you
may also want to consider omitting the conversion into and out of linear light by
leaving out the colorspace commands as well.

If you want something sharper, use sigmoidized EWA Lanczos Radius 3:

convert input.jpg -colorspace RGB +sigmoidal-contrast 8 \
-filter Lanczos -define filter:blur=.9264075766146068 \
-distort Resize 500% \
-sigmoidal-contrast 8 -colorspace sRGB output.jpg

The blur parameter can be used to adjust the sharpness of the
scheme. Decreasing blur increases sharpness, at the expense of
increasing jaggies and tonal drift, and vice versa. The value
blur=.9812505644269356 reproduces the above LanczosSharp scheme.
Setting blur=.9264075766146068 scales the EWA disc so it has a radius
equal to exactly 3, hence the name.

If you want something even sharper, use sigmoidized tensor
(orthogonal) Ginseng 3-lobe, a close relative of the standard Lanczos:

convert input.jpg -colorspace RGB +sigmoidal-contrast 8 \
-define filter:filter=Sinc -define filter:window=Jinc -define filter:lobes=3 \
-resize 500% \
-sigmoidal-contrast 8 -colorspace sRGB output.jpg

Unlike the above EWA Lanczoses (and the Mitchell filter), Ginseng is
an interpolatory filter (like tensor (-resize) Lanczos). The name
"Ginseng" is a pun on "Jinc-windowed Sinc".

If you do not want any kind of haloing, use sigmoidized tensor quadratic
B-spline smoothing. It is quite blurry, but it strikes a decent
balance between sharpness and jagginess given that it is a monotone
method. Because it is smoothing, higher values of the contrast are
recommended with this method. In addition, this method does not suffer
from the clipping artifacts that -resize methods with negative lobes
get with an ImageMagick not compiled in HDRI:

convert input.jpg -colorspace RGB +sigmoidal-contrast 9.5 \
-filter Quadratic \
-resize 500% \
-sigmoidal-contrast 9.5 -colorspace sRGB output.jpg

If you want neither halos nor jaggies and are willing to live with
more than a bit of blur, use sigmoidized EWA Cubic B-spline
smoothing:

convert input.jpg -colorspace RGB +sigmoidal-contrast 10.5 \
-filter Cubic \
-distort resize 500% \
-sigmoidal-contrast 10.5 -colorspace sRGB output.jpg

Recommended methods for downsampling
------------------------------------------------

Basically, the same methods are recommended when downsampling, except
that sigmoidization should be used lightly (with a contrast parameter
under 6.5), if at all. In the following, it is omitted, and resizing is
performed through the generally safer linear RGB. In some situations
(8-bit toolchain, dark halos, heavily processed images), it may be preferable
to process the sRGB pixel values directly, without converting into and out of
linear RGB.

First, try linear light EWA LanczosSharp:

convert input.jpg -colorspace RGB \
-filter LanczosSharp \
-distort Resize 20% \
-colorspace sRGB output.jpg

For sharper results, try linear light Ginseng

convert {input} -colorspace RGB \
-define filter:window=Jinc -define filter:lobes=3 \
-resize 500% \
-colorspace sRGB {output}

Also
for sharper results, use linear light EWA Lanczos Radius 3:

convert input.jpg -colorspace RGB \
-filter Lanczos -define filter:blur=.9264075766146068 -distort Resize 20% \
-colorspace sRGB output.jpg

You can increase the perceptual sharpness of the image by decreasing
the blur value. For example, blur=0.88549061701764, called EWA
LanczosSharpest 3 in the ImageMagick Forums, works well. On the other
hand, you can decrease moire by increasing the blur value, or by
increasing the number of lobes (-define filter:lobes=5, for
example). And you can tighten halos by setting the number of lobes to
2 (-filter Lanczos2Sharp and -filter Lanczos2 do this in one fell swoop).

If you want even tighter halos, use linear light EWA Robidoux (which
is the -distort Resize default):

convert input.jpg -colorspace RGB \
-distort Resize 20% \
-colorspace sRGB output.jpg

The Robidoux filter is a member of the Keys family of BC-splines. You
can dial the sharpness of the EWA filter by adjusting the B parameter.
For example,

convert input.jpg -colorspace RGB \
-filter Cubic -define filter:B=0 \
-distort Resize 20% \
-colorspace sRGB output.jpg

gives EWA Catmull-Rom, a very very sharp method directly obtained with

convert input.jpg -colorspace RGB \
-filter Catrom \
-distort Resize 20% \
-colorspace sRGB output.jpg

EWA Robidoux corresponds to B=0.37821575509399867. EWA Mitchell and
RobidouxSharp are between the two, and EWA Cubic is much much
blurrier and smoother. Actually, if you are using EWA Cubic because
it is halo free, you probably should switch to tensor Quadratic
B-spline smoothing (see above).

Adjusting the blur (between about 0.7 and 1.0) or B parameters
(between 0.0 and 1.0) should obviate the need for USM sharpening. You may
also want to try sigmoidizing.

Feedback
-------------

N.R. reads positive and negative comments with interest. Sending
NicolasRobidoux a message through the ImageMagick Forums system
is a good way to point him to a relevant post.

Thanks
------

N. R. thanks John Cupitt, Henry HO, Anthony Thyssen, Adam Turcotte,
Dane Vandeputte, Luiz E. Vasconcellos and Fred Weinhaus for
useful comments.
Last edited by NicolasRobidoux on 2012-09-18T17:49:02-07:00, edited 37 times in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: sigmoidized EWA (-distort resize) LanczosRadius3

Post by fmw42 »

You might mention Lagrange as about on par or perhaps some better (according to henrywho) as catrom.

Anthony, I might suggest that EWA Lanczos Radius 3 become a named filter.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: sigmoidized EWA (-distort resize) LanczosRadius3

Post by NicolasRobidoux »

@Fred: Thank you for commenting.
fmw42 wrote:You might mention Lagrange as about on par or perhaps some better (according to henrywho) as catrom.
I thought about it, and decided that I wouldn't include Lagrange until I have stronger evidence that it is better than EWA filters with more lobes/small blur.
The EWA Keys cubics fill a niche untouched by the EWA Lanczoses (and tensor Ginseng/Lanczos). They are not as good as the previously listed methods IMHO, but they are cheap and "harmless" (with the right choice of parameters). In addition, there are quantitative results in Adam Turcotte's to-be-published thesis that they are extremely good in some situations, and a panel liked EWA Robidoux more than, for example, tensor Mitchell and EWA LanczosSharp to produce thumbnails from JPEGs of widely varying quality without going through linear light. (EWA LanczosSharp was the runner up. A number of other methods were not liked as much as those two.) Finally, I did a numerical comparison with the other EWA BC-splines and found that they are extremely close to being exact on linears (in a numerical analysis sense), surprisingly so.
Lots of weak evidence, in other words.
As of today, the evidence supporting Lagrange is flimsier. (At least, the evidence I have.)
Last edited by NicolasRobidoux on 2012-09-14T12:34:44-07:00, edited 4 times in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: sigmoidized EWA (-distort resize) LanczosRadius3

Post by fmw42 »

Nicolas wrote:I thought about it, and decided that I wouldn't include Lagrange until I have stronger evidence that it is better than EWA filters with more lobes/small blur.
No problem.

(By the way, my (last) name is not always spelled correctly either. On the other hand, there is another Fred Weinhaus with the exact same spelling)
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: sigmoidized EWA (-distort resize) LanczosRadius3

Post by NicolasRobidoux »

@Anthony:
If LanczosRadius3 (or LanczosRadiusN) and Ginseng could be added as "named" filters, I'd add them to the opening "no fuss, no muss" section, and I'd simplify the code.
If you don't like the name LanczosRadius3 (which describes how to build the scheme, not what it's for), and notwithstanding that I've used the name for the maximally sharpening EWA Lanczoses, an appropriate name for the LanczosRadius3 could be LanczosSharpest, B/C I really don't recommend deblurring more than that unless you are downsampling a lot and you care about nothing else but perceptual sharpness. I'll never recommend that the maximally sharpening ones be included.
Also: Still editing the blurb a little bit. I think I'll add a short section about best methods that avoid all haloing. Plain -resize or -distort Resize Cubic is definitely too blurry.
henrywho
Posts: 188
Joined: 2011-08-17T06:46:40-07:00
Authentication code: 8675308

Re: sigmoidized EWA (-distort resize) LanczosRadius3

Post by henrywho »

fmw42 wrote:You might mention Lagrange as about on par or perhaps some better (according to henrywho) as catrom.
For web-size downsampling of natural photos, EWA Lagrange appears similar to EWA Cubic @c=0.483 ~ 0.485

But for artificial ones like rings_lg_orig.png, EWA Lagrange behaves quite a bit differently than EWA Cubic
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: sigmoidized EWA (-distort resize) LanczosRadius3

Post by NicolasRobidoux »

Interesting. Thank you Henry.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: sigmoidized EWA (-distort resize) LanczosRadius3

Post by NicolasRobidoux »

NicolasRobidoux wrote:...
Also: Still editing the blurb a little bit. I think I'll add a short section about best methods that avoid all haloing. Plain -resize or -distort Resize Cubic is definitely too blurry.
@Anthony:
Actually, done editing for a while. I just discovered that sigmoidization works really well with Keys splines, even Cubic (which has no negative lobe), so my "less halo using less lobes" recommendations when enlarging are completely changed.
Also, the Quadratic methods, which I like on paper, are not "top" in practice?
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: sigmoidized EWA (-distort resize) LanczosRadius3

Post by NicolasRobidoux »

@Anthony: I actually think that I'm truly done.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: sigmoidized EWA (-distort resize) LanczosRadius3

Post by fmw42 »

Nicolas wrote: I just discovered that sigmoidization works really well with Keys splines,
I would be interested to hear more about your findings, since I have not had time to get back to my thoughts at viewtopic.php?f=1&t=21703&p=89577#p89577
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: sigmoidized EWA (-distort resize) LanczosRadius3

Post by NicolasRobidoux »

@Fred:
I'm going to need to leave these topics for a while and earn a living, for one thing. But you can pick your favorite image and enlargement ratio and try it (with an HDRI IM, please, and preferably IM7), for example

Code: Select all

magick input.png -colorspace RGB +sigmoidal-contrast 11 \
-filter Cubic -define filter:B=.5 \
-resize 500% \
-sigmoidal-contrast 11 -colorspace sRGB output.png
You can vary the contrast value and/or B, and see for yourself what happens. I still prefer the 3-lobe methods I recommend above, but the point here is that sigmoidization works well with other filters, and actually, it even works well if B=1 (which gives cubic B-spline smoothing), in which case there is no overshoot or undershoot, and my primary original motivation for creating sigmoidization goes out the window.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: sigmoidized EWA (-distort resize) LanczosRadius3

Post by fmw42 »

Nicolas,

Thanks for the reply. I understand.

Fred
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: sigmoidized EWA (-distort resize) LanczosRadius3

Post by NicolasRobidoux »

Same code with -distort Resize works well too.
henrywho
Posts: 188
Joined: 2011-08-17T06:46:40-07:00
Authentication code: 8675308

Re: sigmoidized EWA (-distort resize) LanczosRadius3

Post by henrywho »

NicolasRobidoux wrote:Interesting. Thank you Henry.
And some time ago I have tested downsizing of black and white texts with EWA lanczos3, catrom and lagrange. I remember that EWA lagrange produces dark halos more than white halos. Perhaps it's the reason I like it better.
Post Reply