"Sigmoidal" minimization of resampling filter haloing & blur

Discuss digital image processing techniques and algorithms. We encourage its application to ImageMagick but you can discuss any software solutions here.
Post Reply
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

"Sigmoidal" minimization of resampling filter haloing & blur

Post by NicolasRobidoux »

In another thread, I suggested that pushing an image through the inverse of a contrast enhancing sigmoidal transformation (from linear light, although not necessarily) before resampling with a filter that produces pronounced halos (all the filters with significant negative lobes, e.g. lanczos 3) and then undoing the sigmoidal transformation, may be an effective way of removing some of the heavy eyeliner and holy halos without increasing other artifacts, esp. when upsampling.
I then figured a quick and dirty way of doing something basically equivalent by exploiting sRGB (which is "half the inverse of a sigmoidal"): tag the sRGB image as RGB, convert to sRGB (again!), negate, tag as RGB, convert to sRGB (yet again!), resample, and then undo all operations except resampling. Basically, this is doing the resampling through a "colorspace" in which dark and light values are farther from each other.
Here is the code when the input image (the built in ImageMagick rose image) is treated as being sRGB (if not, just convert to sRGB first, for another conversion), using a bleeding edge ImageMagick 7 (results obtained with an HDRI version):

Code: Select all

magick rose: -set colorspace RGB -depth 16 -colorspace sRGB -negate -set colorspace RGB -colorspace sRGB -filter lanczos -resize 800x -colorspace RGB -negate -set colorspace sRGB -colorspace RGB -set colorspace sRGB lanczosKindaSigmoidal.png
This gives
Image
Let's compare with "straight" lanczos, obtained with

Code: Select all

magick rose: -depth 16 -filter lanczos -resize 800x -set colorspace sRGB lanczos.png
Image
and compare with "linear light" lanczos, obtained with

Code: Select all

magick rose: -set colorspace sRGB -depth 16 -colorspace RGB -filter lanczos -resize 800x -colorspace sRGB lanczosLinear.png
Image.
Neat, eh? (To truly appreciate, you must download the images and view them by flipping between them in a decent viewer, like nip2.)

-----

Not sure by any means that there is no zero sum game going on here---I'm particularly concerned with colour drift, which should be controlled by ensuring perfect reversibility of the non-resampling operations---and I would expect that tweaking things so that the amount and "shape" of "sigmoidation" is just right for the given filter would help things, but for a quick and dirty hack, it looks promising.

I also tried it with the curios image from the 16bit840x840images test bank, and it seemed to help there as well.

As Henry HO indirectly mentioned, you can apply only one side of the "stretch the values near black and white" if you only want to reduce one type of halo.

At some point, I'll look into using a "true" sigmoidal transform (as Fred Weinhaus suggested).
Last edited by NicolasRobidoux on 2012-08-08T05:36:41-07:00, edited 6 times in total.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: "Sigmoidal" minimization of resampling filter haloing

Post by anthony »

NicolasRobidoux wrote:\I then figured a quick and dirty way of doing something basically equivalent by exploiting sRGB (which is "half the inverse of a sigmoidal"): tag the sRGB image as RGB, convert to sRGB (again!), negate, tag as RGB, convert to sRGB (yet again!), resample, and then undo all operations except resampling. Basically, this is doing the resampling through a "colorspace" in which very dark and very light are farther from each other.
a sRGB conversion is not a sigmoidal contrast function. they are related but sigmoidal is typically centered on 50%gray rather than on black.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
henrywho
Posts: 188
Joined: 2011-08-17T06:46:40-07:00
Authentication code: 8675308

Re: "Sigmoidal" minimization of resampling filter haloing

Post by henrywho »

The overall image will become darker too, not just halos. Try downsizing a photo of cactus.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: "Sigmoidal" minimization of resampling filter haloing

Post by NicolasRobidoux »

anthony wrote:...
a sRGB conversion is not a sigmoidal contrast function. they are related but sigmoidal is typically centered on 50%gray rather than on black.
This is why I do it twice: First directly, then to the photo negative.
I'm sure the equivalent "inverse of an S curve" is not perfectly symmetrical about 50% gray, but close enough, it appears.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: "Sigmoidal" minimization of resampling filter haloing

Post by NicolasRobidoux »

henrywho wrote:The overall image will become darker too, not just halos. Try downsizing a photo of cactus.
This is why I "undo all operations except resampling". If the transformations used to emulate an S curve and its inverse are accurately reversible, there should not be any colour drift when enlarging.
When downsampling, you're basically revisiting the Dalai Lama, so yes there could be bad averaging of nearby colours (which already occur with sRGB). On the other hand, negative lobes wreck havoc with colours anyway, esp. near extreme values, so it could be that this is useful when downsampling too.

If I was to use this with only one sRGB conversion (applied to a colour negative if I wanted to minimize only white halos, and a colour "positive" if I wanted to minimize only dark halos), the "undoing" would also keep colours pretty much in place.
Last edited by NicolasRobidoux on 2012-07-12T06:56:19-07:00, edited 1 time in total.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: "Sigmoidal" minimization of resampling filter haloing

Post by NicolasRobidoux »

(Note that I am wondering if my trick should only be used on a luminance channel, leaving chroma alone. It could also be used to sharpen without introducing horrid halos. Manana.)

@Henry HO:
Let me put together another example that shows a key difference between this and what you do to increase acutance.
The rose image, when enlarged with a negative lobe filter, does not really suffer from "light halos". The halos that hurt are the "dark" ones. So, there is no need to use the whole "S curve", only half of it, the half that affects "darks", which I want to move "farther" from the lighter colours.
Quick n' dirty: Use sRGB conversion once (instead of twice: once for darks, and once, in reverse video, for lights).
Here goes:

Code: Select all

magick rose: -set colorspace RGB -depth 16 -colorspace sRGB -filter lanczos -resize 800x -colorspace RGB -set colorspace sRGB lanczosHalfSigmoidal.png
with result
Image
I do prefer the result with the "full" S curve (the very first image in this thread), but the result is good.

-----

Now, my understanding is that you use a related trick (with -gamma instead of -colorspace) to increase the dark halos. So, let me increase the dark halos way too much without changing overall coloration. (Assuming -gamma is sufficiently accurately invertible, you should be able to use it instead of -colorspace, and then you'd be able to more easily tweak the amount of "damage".)

Code: Select all

magick rose: -set colorspace sRGB -depth 16 -colorspace RGB -set colorspace sRGB -colorspace RGB -filter lanczos -resize 800x -colorspace sRGB -set colorspace RGB -colorspace sRGB lanczosLotsOfHalo.png
with result
Image
As mentioned by Henry, the light halos are also amplified, but way less.
A less exaggerated version of this trick is the linear light image which is the third shown in this thread. (Yes, you read me right: I do not believe that enlarging should be done in linear light when using a negative lobe filter unless you want the extra acutance which haloing brings to the table.)

Final comment: As has been discussed elsewhere, standard -resize methods with negative lobes have artifacts when using non-HDRI ImageMagick that come from over- and undershoots being clipped between the two orthogonal passes. (This does not affect -distort resize methods.) I would guess that, in 16-bit IM (the default), you could minimize these artifacts using this "sigmoidal" trick: If there are less over and undershoots, there is less to clip, and if there is less to clip, clipping between the two orthogonal passes will hurt less.
Last edited by NicolasRobidoux on 2012-07-12T06:50:56-07:00, edited 6 times in total.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: "Sigmoidal" minimization of resampling filter haloing

Post by NicolasRobidoux »

I need to get back to a tall pile of urgent work. But I must say that I'm quite pleased with this fun attempt at making things better. It's definitely promising.
If I had more time, I'd construct an accurately invertible sigmoidal from scratch, and I would tune it so that staircasing is minimized with orthogonal lanczos 3 with a black on white (or white on black) sharp diagonal interface. (Not 100% sure it's possible, but I'd give it a go.)

P.S. I double checked with another image, and my quick and dirty method of "resampling through a synthetic colourspace with very high density darks (like sRGB) and very high density lights (unlike sRGB)" so as to put distance between lights and darks definitely appears to improve the quality of enlargements with negative lobe filters. It's night and day better than going through linear light, and subtly better than enlarging with straight sRGB.
So far, the halos (and, to a lesser extent jaggies) have been mitigated, and I've not seen any bad artifacts corrupting the quality.
I can only imaging what this could do with a carefully chosen and put together "true" sigmoidal.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: "Sigmoidal" minimization of resampling filter haloing

Post by NicolasRobidoux »

The amazing thing is that it does not only reduce halos: It also reduces jaggies, without decreasing sharpness.
Orthogonal lanczos 3 even loses a bit of its "blockiness" with sharp diagonal lines and interfaces.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: "Sigmoidal" minimization of resampling filter haloing

Post by NicolasRobidoux »

Had a second look.
"Sigmoidal" lanczos definitely looks more natural that "straight sRGB" and "linear light" when enlarging.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: "Sigmoidal" minimization of resampling filter haloing

Post by fmw42 »

NicolasRobidoux wrote:Had a second look.
"Sigmoidal" lanczos definitely looks more natural that "straight sRGB" and "linear light" when enlarging.

Is this your "half" sigmoidal approximateion using sRGB or the full proper sigmoidal-contrast?

How does it compare to Lagrangian filter or catmul-rom filter on enlarging; that is if you are concerned about sharpness rather than dark/light halos or ringing?
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: "Sigmoidal" minimization of resampling filter haloing

Post by NicolasRobidoux »

fmw42 wrote:...
Is this your "half" sigmoidal approximateion using sRGB or the full proper sigmoidal-contrast?
It's my "full" poor man's sigmoidal approximation using sRGB :)
fmw42 wrote:...
How does it compare to Lagrangian filter or catmul-rom filter on enlarging; that is if you are concerned about sharpness rather than dark/light halos or ringing?
The point is that the benefits of ANY negative lobe method in terms of sharpness can be had without the excessive haloing by applying in a "synthetic" colourspace in which the darks and lights occupy more of the gamut than in the original colourspace (sRGB, here).
So, it should work just as well with Catmull-Rom or Lagrange, although I've not tried it.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: "Sigmoidal" minimization of resampling filter haloing

Post by fmw42 »

The point is that the benefits of ANY negative lobe method in terms of sharpness can be had without the excessive haloing by applying in a "synthetic" colourspace in which the darks and lights occupy more of the gamut than in the original colourspace (sRGB, here).
So, it should work just as well with Catmull-Rom or Lagrange, although I've not tried it.
Actually I was wondering if a comparison between your sigmoidal process with straight Catmlull-Rom or Lagrange was relevant or mixing apples and oranges. But I suspect the latter (not really relevant comparison). I guess I was interested more in the resulting sharpness than haloing.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: "Sigmoidal" minimization of resampling filter haloing

Post by NicolasRobidoux »

Apples and oranges. Although eventually one will want to compare all fruits.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: "Sigmoidal" minimization of resampling filter haloing

Post by NicolasRobidoux »

Anybody who wonders why I don't use IM's sigmoidal-contrast and instead cheat my way with colorspace and negate: the current default IM implementation of sigmoidal-contrast is not such that +sigmoidal-contrast is accurately undone by -sigmoidal-contrast. And I need that to avoid global color drift, since I use these two functions to convert into and out of a synthetic "sigmoidal" colour space.

If I set two "#if 1"s in enhance.c (ImageMagick 7) to "#if 0"s so as to turn on the "Scaled sigmoidal formula" and the "expanded form of the above", I get something which appears not to give colour drift. So, sigmoidal-contrast does the job IF I revert it to an earlier version. (HDRI bleeding edge ImageMagick 7, always.) Then it works beautifully, it's much faster, and I can tune the amount of contrast (or the midpoint).
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: "Sigmoidal" minimization of resampling filter haloing

Post by fmw42 »

If I set two "#if 1"s in enhance.c (ImageMagick 7) to "#if 0"s so as to turn on the "Scaled sigmoidal formula" and the "expanded form of the above", I get something which appears not to give colour drift. So, sigmoidal-contrast does the job IF I revert it to an earlier version. (HDRI bleeding edge ImageMagick 7, always.) Then it works beautifully, it's much faster, and I can tune the amount of contrast (or the midpoint).
Are you suggesting there is a bug in -sigmoidal-contrast in either IM 6 or 7?
Post Reply