Improving sigmoidal image resampling (enlargement)

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

Improving sigmoidal image resampling (enlargement)

Post by NicolasRobidoux » 2014-06-15T06:47:07-07:00

I am revisiting sigmoidal resampling (http://www.imagemagick.org/Usage/resize ... _sigmoidal and http://www.imagemagick.org/Usage/filter ... upsampling).
Heuristics and some experimentation suggest that:
  • Sigmoidization works much much better when used with an image converted to a color space with channels monotonic with respect to luminance (like linear RGB).
  • An HDRI (floating point, with no clamping at intermediate states) toolchain should be used. (I think that ImageMagick 6 always clamps when applying sigmoidal contrast enhancement, which would be bad news with images with extreme color values, but not ImageMagick 7. I will need to check the source code.)
  • (Important and new) The sigmoidal curve should not be symmetrical about the black and white points. Specifically, the sigmoid's "inflexion point" (called "threshold" in http://www.imagemagick.org/Usage/color_mods/#sigmoidal), near which the "synthetic color space" transformation is approximately affine, should be closer to white than black, so that the effect of applying the inverse sigmoidal curve resemble gamma correction somewhat.
  • (Important and new) A strongly antialiasing filter should be used (if the image is sharp; otherwise it does not matter).
  • There is a sweet spot in the strength of the contrast enhancement, and this sweet spot depends on the resampling filter kernel.
Putting this together, an example of recommended command (assuming the source image is sRGB; otherwise, insert appropriate color space conversions at the beginning of the toolchain) would be:

Code: Select all

magick input.png -colorspace RGB +sigmoidal-contrast 6.5,66% -define filter:blur=1.02 -filter Lanczos -distort Resize 500% -sigmoidal-contrast 6.5,66% -colorspace sRGB output.png
In the above, the EWA (Elliptical Weighted Averaging) Jinc-windowed Jinc 3-lobe filter, "stretched" by a factor of 1.02 to make it slightly more blurry, is used (maybe this stretching is not necessary?). The amount of sigmoidization is 6.5, and the inflexion point is located about twice as far from black than from white (66%).
-----
I'd love to hear what people have to say about results obtained with the above command or with variants (in particular, "6.5,66%" was settled on rather quickly, and I suspect that some people won't like the use of a 3-lobe filter, as opposed to, say, an EWA method with a Keys cubic filter kernel that sits somewhere between Robidoux http://www.imagemagick.org/Usage/filter/#robidoux and RobidouxSoft viewtopic.php?f=22&t=19823&start=30#p109820). I am particularly interested in results obtained by performing sigmoidized resampling with a tool other than ImageMagick, as part of a 16 bit or float toolchain that avoids clamping until the last minute, both upsampling and downsampling images that are fairly close to "raw" and obtained with a fairly sharp demosaicing method (with no extra sharpening applied, however).
Note that I do not expect sigmoidization to work well with heavily JPEG compressed images, but I think that with the right resampling filter kernel it may do OK with noise.

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

Re: Improving sigmoidal image resampling (enlargement)

Post by NicolasRobidoux » 2014-06-16T02:18:34-07:00


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

Re: Improving sigmoidal image resampling (enlargement)

Post by NicolasRobidoux » 2014-06-28T07:01:34-07:00


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

Re: Improving sigmoidal image resampling (enlargement)

Post by NicolasRobidoux » 2014-06-28T10:07:51-07:00

Not sure, but I think that just converting to a gamma space in order to work in a "smooth" perceptual space so as to minimize "dark halos", which with photographic material are more bothersome than the "light halos" may give better results with less tweaking than sigmoidizing. Something like

Code: Select all

magick input.png \
-set colorspace sRGB -colorspace RGB -gamma 4 \
-distort Resize 250% \
-gamma .25 -colorspace sRGB \
robidoux.gamma4.png
or, if you are enlarging more than something like 400% and you don't want jaggies

Code: Select all

magick input.png \
-set colorspace sRGB -colorspace RGB -gamma 4 \
-filter Lanczos -define filter:lobes=4 -define filter:blur=0.88451002338585141 -distort Resize 500% \
-gamma .25 -colorspace sRGB \
lanczosSharpest4.gamma4.png
Warning With very large gamma, I would not be surprised if you get highly undesirable results if you don't use HDRI, or at least Q16. I've not checked though: I've only used HDRI IM7.

I've only checked on slightly blurry but fairly noise free photographs, and this seems promising. I think that with line art or pixel art this is not the best one can do, but digital photographs are different.
(Fred: Did you suggest just that a long long time ago? This of course is doing http://www.imagemagick.org/Usage/resize/#resize_gamma but in the "opposite direction": resample with gamma = 1 for radical downsampling, but resample with gamma > 1.8 for radical upsampling, at least with filters with significant negative lobes.)
Last edited by NicolasRobidoux on 2014-06-28T10:29:20-07:00, edited 6 times in total.

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

Re: Improving sigmoidal image resampling (enlargement)

Post by fmw42 » 2014-06-28T10:14:17-07:00

I do not recall that it was my suggestion. Most likely not. Perhaps henrywho?

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

Re: Improving sigmoidal image resampling (enlargement)

Post by fmw42 » 2014-06-28T10:19:28-07:00

Have you considered using a more symmetric "soft knee" curve rather than just gamma. see http://www.fmwconcepts.com/imagemagick/ ... /index.php.

Does it make sense to do both gamma>1 and gamma<1 in the same command, each with its own resize rather than processing the image twice and blending? Does that make any sense?

Sigmoidal-contrast can be make to look more like gamma, by using 0 and 100 as the midpoint, if you want two separate operations. See http://www.fmwconcepts.com/imagemagick/ ... /index.php

Sigmoidization seems to make more sense to me, though as you said before, you may want it to be asymmetric.

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

Re: Improving sigmoidal image resampling (enlargement)

Post by NicolasRobidoux » 2014-06-28T10:26:22-07:00

fmw42 wrote:Have you considered using a more symmetric "soft knee" curve rather than just gamma. see http://www.fmwconcepts.com/imagemagick/ ... /index.php.

Does it make sense to do both gamma>1 and gamma<1 in the same command, each with its own resize rather than processing the image twice and blending? Does that make any sense?

Sigmoidization seems to make more sense to me, though as you said before, you may want it to be asymmetric.
In the early days of sigmoidization, I was blending an sRGB and "negated sRGB" version of the image "on the run". I think this is basically what you are talking about in "... with the same command".

And maybe the sigmoidal curve is not the best choice. Indeed, I worry that the kink introduces moire (we already know it introduces strange colors, sometimes). So maybe a "soft knee" would do. Although we also want the effect to be noticeable /o\

P.S. Sigmoidization makes more sense to me too. But I'm trying to make math and the HSV play nice, and it's quite easy to displease one or the other.

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

Re: Improving sigmoidal image resampling (enlargement)

Post by NicolasRobidoux » 2014-06-28T10:31:46-07:00

fmw42 wrote:I do not recall that it was my suggestion. Most likely not. Perhaps henrywho?
Assuming I did not come up with this idea myself (at least within this circle), I'll ask him if this turns out to work well and he does not "show up".
P.S. I don't think it was henriwho. Unless someone sets me straight, I'll assume it was me.
Last edited by NicolasRobidoux on 2014-06-29T00:57:10-07:00, edited 1 time in total.

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

Re: Improving sigmoidal image resampling (enlargement)

Post by fmw42 » 2014-06-28T10:32:21-07:00

On second thought, soft knee may not be what you want. It would seem that if you want two separate operations, that using sigmoidal-contrast with 0 and 100 might be best, since it is linear for much of the curve and then bends over at the high end or the low end. That way you can control the degree of help for dark and light halos.

I just worry that two steps (gamma or sigmoidal) might add more blur (softness) that one can avoid with just one sigmoidal-contrast.

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

Re: Improving sigmoidal image resampling (enlargement)

Post by NicolasRobidoux » 2014-06-28T12:36:24-07:00

fmw42 wrote:...
I just worry that two steps (gamma or sigmoidal) might add more blur (softness) that one can avoid with just one sigmoidal-contrast.
If you blend two sharp versions of the same image (and of course they are perfectly aligned), the result should still be sharp. With less artificial acutance (because you are hopefully suppressing the dark and light halos). But still sharp, because it you do it right the "sharpness" you lose should actually be aliasing (staircasing, for example).
At least that's my hunch.
Unfortunately, carefully testing hunches is so bloody time consuming...

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

Re: Improving sigmoidal image resampling (enlargement)

Post by NicolasRobidoux » 2014-06-28T12:41:43-07:00

I think that using gamma > 1.8 to suppress the dark halos (hopefully without messing up midtones), linear to suppress the light halos (and preserve colors), and then blending the two putting more of the high gamma, may be a good way to get a good upsampling result. But maybe skipping the linear part does not really hurt.
The nice thing about the above is that if you are warping you can locally change the blend depending on whether you downsample or not. More downsampling -> more linear. Less downsampling -> more gamma.
Of course roughly the same can be achieved by ramping down the gamma as you move toward heavy downsampling.

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

Re: Improving sigmoidal image resampling (enlargement)

Post by fmw42 » 2014-06-28T13:59:15-07:00

Nicolas,

If you can give me an image and a command line (and any variations), I can script a loop to vary whatever parameters you want. Then I can display the images or send them to you for review. I do not have a critical enough eye to tell fine detailed differences.

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

Re: Improving sigmoidal image resampling (enlargement)

Post by NicolasRobidoux » 2014-06-29T00:44:27-07:00

fmw42 wrote:...
If you can give me an image and a command line (and any variations), I can script a loop to vary whatever parameters you want...
Thank you Fred.
I will think.
Cheers

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

Re: Improving sigmoidal image resampling (enlargement)

Post by NicolasRobidoux » 2014-06-29T07:42:00-07:00

I put a first sample result in a P.S. of viewtopic.php?f=22&t=21804&p=89751#p89751

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

Re: Improving sigmoidal image resampling (enlargement)

Post by NicolasRobidoux » 2014-06-29T08:49:25-07:00

Second sample here, with EWA Lanczos and gamma 4: viewtopic.php?f=22&t=21804&p=89728#p89728
ImageMagick (IM 7 running in HDRI (floating point) mode) command:

Code: Select all

magick input_small.png -set colorspace sRGB -colorspace RGB -gamma 4 -filter lanczos -distort Resize 800% -gamma .25 -colorspace sRGB ewalanczos.gamma4.png
P.S. Third sample: viewtopic.php?f=22&t=21804&p=89728#p89746, this time with EWA LanczosSharpest 4

Code: Select all

magick input_small.png -set colorspace sRGB -colorspace RGB -gamma 4 -filter Lanczos -define filter:lobes=4 -define filter:blur=0.88451002338585141 -distort Resize 800% -gamma .25 -colorspace sRGB ewalanczossharpest4.gamma4.png

Post Reply