Pages: 1 ... 3 4 [5] 6 7 ... 24   Go Down

Author Topic: A free high quality resampling tool for ImageMagick users  (Read 240448 times)

Bart_van_der_Wolf

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 8913
Re: A free high quality resampling tool for ImageMagick users
« Reply #80 on: July 22, 2014, 02:47:16 pm »

To recap the (current) method choices:
  • For general resizing, but especially upsampling: a blend between a gamma 1 and a gamma 1+2 resampling with the LanczosDetail filter.
  • For down-sampling without sharpening: a gamma 1 resampling with the Lanczos4Sharper filter recipe.
  • For down-sampling with sharpening: a gamma 1 resampling with the LanczosSharp filter.

Bart: I don't really understand the first two list items.

Probably the way I worded it.

First one uses a gamma linearization (conversion to linear light), and a gamma 2 variant of that linearization (1+2 gamma) for actual resampling, and then both are blended in linear light, followed by a gamma conversion from linear light to the original image's gamma. Best used for upsampling, but not too bad for down-sampling either.

Second one uses a linear light resampling with the recipe to simulate a "Lanczos4Sharper" filter. Better for down-sampling, but may need some more sharpening for some images.

Cheers,
Bart
Logged
== If you do what you did, you'll get what you got. ==

NicolasRobidoux

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 280
Re: A free high quality resampling tool for ImageMagick users
« Reply #81 on: July 22, 2014, 02:51:38 pm »

Second one uses a linear light resampling with the recipe to simulate a "Lanczos4Sharper" filter. Better for down-sampling, but may need some more sharpening for some images.
Favor: Could you please refer to it as LanczosSharpest 4 or Lanczos4Sharpest (with or without spaces). With fixed number of lobes, it's constructed as "the one" EWA Jinc-windowed Jinc (a.k.a. EWA Lanczos) deblurred with the minimiser of worst case deviation from the identity when used as a no-op. Hence the "sharpest" (mis)nomer. (Yes, I know there is no "sharpest" EWA lanczos, even if one fixes the number of lobes. But this is what I've been calling those for a long time. What the definition above suggests is that it is the sharpest one that is reasonable to use.)
Also, when I have time, I'll put it into ImageMagick under the name LanczosSharpest so people don't have to type the whole long enchilada with 15 digits numbers and -define filter:lobes=4 etc etc.
« Last Edit: July 22, 2014, 04:10:41 pm by NicolasRobidoux »
Logged

Jack Hogan

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 798
    • Hikes -more than strolls- with my dog
Re: A free high quality resampling tool for ImageMagick users
« Reply #82 on: July 22, 2014, 03:43:53 pm »

P.S. I really don't like the "linear segment" of sRGB: Fine for compressing, not fine for manipulating the image.
I also wondered about why that was a 'default' colorspace in ImageMagick (maybe the answer can be found on their forums). From a scientific viewpoint a straightforward gamma 2.2 / 0.454545 pair would seem easier to analyze than one with a straight line segment in the shadows ...

However, ImageMagick is flexible enough to use any real ICC Profile conversion we would like, or even a synthetic 'Custom' one created in Photoshop.

Much of this is over my head so perhaps I am misunderstanding, but well behaved gamma functions need a linear portion near the origin because otherwise they amplify noise uncontrollably there upon conversion to a different gamma.

As Burger says, 'the tangent to the function at the origin is either horizontal (for gamma >1), diagonal (for gamma =1) or vertical (for gamma <1) with no intermediate values.  For gamma <1 this causes extremely high amplification of small intensity values and thus increased noise in dark image regions.'

So the fact that AdobeRGB does not have a linear portion near the origin is actually a big problem as far as deep shadow noise is concerned when going back and forth liberally to linear space.  I think Adobe's ACE engine linearizes the deep shadows when converting.

Jack
« Last Edit: July 22, 2014, 03:46:46 pm by Jack Hogan »
Logged

Jack Hogan

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 798
    • Hikes -more than strolls- with my dog
Re: A free high quality resampling tool for ImageMagick users
« Reply #83 on: July 22, 2014, 04:03:17 pm »

Jack, that's correct, and given the high quality of the ImageMagick filter implementation, I wouldn't worry about prior Capture sharpening or not, ImageMagick will probably handle it just fine, either way. Of course, proper Capture sharpening doesn't introduce halos either, but that's a different topic.

Indeed, however, if one allows the ImageMagick installer to do it (see third attachment), that is already taken care of as it prefixes the new Path to Imagemagick to the existing startup file. The drawback of using that option is that new installations will keep prefixing new paths to the PATH environment variable, so one needs to occasionally edit the older locations out (always tricky, so don't make mistakes).

The drawback of your suggestion, is that one needs to edit the batch script file(s) one uses to point to the correct path, each time a new version is installed. Also, the path is expanded each time the script is run until restating the computer (could be solved by saving the old path as a variable (e.g. SET OLDPATH=%PATH%) at the start, and restoring it at the end of the script). So, it's up to the users how they want to tackle this.

Thanks Bart, I'll look into your instructions.  I copy ImageMagick into a homonymous folder without installing it to keep things simple.  New versions simply get copied on top of it. 

The path command as I added it to your batch file appears to behave like a temporary environment variable, so every time the Command window closes (at the end of each conversion) it is forgotten.  Seems to work for me.

Jack
« Last Edit: July 22, 2014, 04:52:12 pm by Jack Hogan »
Logged

Jack Hogan

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 798
    • Hikes -more than strolls- with my dog
Re: A free high quality resampling tool for ImageMagick users
« Reply #84 on: July 22, 2014, 04:09:04 pm »

I agree the sharpening is still a bit heavy handed

Yes

To recap the (current) method choices:
  • For down-sampling with sharpening: a gamma 1 resampling with the LanczosSharp filter.

Bart/Nicolas,

Is the sharpening in the LanczosSharp filter a separate routine, or does the image just come out sharper off the filter?  If the former, what kind of sharpening does it apply?

Thanks,
Jack
Logged

Bart_van_der_Wolf

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 8913
Re: A free high quality resampling tool for ImageMagick users
« Reply #85 on: July 22, 2014, 04:11:49 pm »

Thanks Bart, I'll look into your instructions.  I copy ImageMagick into an homonymous folder without installing it to keep things simple.  New versions simply get copied on top of it.

Yes, that's one other possibility.

Quote
The path command as I added it to your batch file appears to behave like a temporary environment variable, so every time the Command window closes (at the end of each conversion) it is forgotten.

Ah, yes of course. It is probably only effective for the current console session, sweet. I'll see what I can do with the script to implement that, need a bit of time to think of all possible consequences.

Cheers,
Bart
« Last Edit: July 22, 2014, 06:30:48 pm by BartvanderWolf »
Logged
== If you do what you did, you'll get what you got. ==

NicolasRobidoux

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 280
Re: A free high quality resampling tool for ImageMagick users
« Reply #86 on: July 22, 2014, 04:18:03 pm »

Is the sharpening in the LanczosSharp filter a separate routine, or does the image just come out sharper off the filter?  If the former, what kind of sharpening does it apply?
The EWA Lanczoses are resampling filters with varying sharpness characteristics.
Without any additional sharpening, from sharpest to less sharp:
EWA LanczosSharpest 4 (lobes), EWA LanczosRadius (3 lobes by default), EWA LanczosSharp (3 lobes by default), EWA Lanczos (3 lobes by default).
They are all sharper than, say downsampling with box filtering (a rather low bar :) ). EWA LanczosRadius is about as sharp as the standard tensor (orthogonal) Lanczos Sinc-windowed Sinc 3 lobe (what most people think of when they hear "Lanczos resampling filter").
Bart suggested using EWA LanczosSharp when additional sharpening is applied because it is not a strongly sharpening filter in and by itself. Sharpening after resizing with, say, EWA LanczosSharpest 4, is like putting a big nail through the above mentioned baseball bat. After EWA Lanczos, sticking on a hardened piece of gum (presumably to leave one's teeth imprint on the intended victim, without sullying one's dentures).
« Last Edit: July 22, 2014, 04:55:03 pm by NicolasRobidoux »
Logged

Bart_van_der_Wolf

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 8913
Re: A free high quality resampling tool for ImageMagick users
« Reply #87 on: July 22, 2014, 04:18:39 pm »

Yes

But, but, it's already down to 65% ... ;)
Just kidding, I need to sit down and do a proper PSF analysis, and encode that. It's on the ToDo list.

Quote
Is the sharpening in the LanczosSharp filter a separate routine, or does the image just come out sharper off the filter?  If the former, what kind of sharpening does it apply?

It's the latter, it comes out sharper, without additional help (but with some other drawbacks). As usual, it's a balancing act between various types of artifacts. At a small image scale, the drawbacks may be imperceptible.

Cheers,
Bart
Logged
== If you do what you did, you'll get what you got. ==

Jack Hogan

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 798
    • Hikes -more than strolls- with my dog
Re: A free high quality resampling tool for ImageMagick users
« Reply #88 on: July 22, 2014, 04:51:20 pm »

Thanks for the info, the metaphors and the spell check* guys.

Jack

*I was going for homonymous, but anonymous works just as well :)
Logged

Bart_van_der_Wolf

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 8913
Re: A free high quality resampling tool for ImageMagick users
« Reply #91 on: July 23, 2014, 03:03:19 am »

Thanks for the info, the metaphors and the spell check* guys.

Jack

*I was going for homonymous, but anonymous works just as well :)

Fixed in the quoted post, sorry about the wrong guess.

Cheers,
Bart
Logged
== If you do what you did, you'll get what you got. ==

Bart_van_der_Wolf

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 8913
Re: A free high quality resampling tool for ImageMagick users
« Reply #92 on: July 23, 2014, 09:52:37 am »

First: I really like the upsampling result that results from the blend in linear light of a linear gamma resample with a linear gamma with additional gamma 2 resample. It is not perfect in the sense of being totally halo free, but the over and undershoots are at least balanced and relatively mild. Also aliasing is not excessive.

Second: I'm working on the down-sampling side of the coin which also produces good results with the above procedure, but could potentially be better because we need not use such a sharpness preserving filter, as long as we can restore most of the sharpness with e.g. a controlled sharpness restoration operation. After all, downsampling quickly produces over-sampled data which could be sharpened the same, regardless of percentage of down-sampling (especially below 50% of the original size).

Third: In this quest for sharpening, I looked at the Point Spread Function (PSF) of data that was downsampled in linear light, which is known to produce correct blends of RGB colors, but has the nasty habit of producing larger undershoot halos than overshoot halos, which was the reason for the original solution proposed by Nicolas Robidoux. The main issue appears to be the Lanczos windowed Sinc filtering which, while preserving sharpness, generates a huge undershoot in linear light space. That also makes it almost impossible to sharpen further without making matters worse.

This led me to a desire to revisit the downsampling+sharpening procedure, so bare with me for a while as I attempt to tackle the situation. I'm making good progress I think, I've found the Gaussian filter combined with an EWA -distort Resize, to produce no halos in linear light space but is also a bit soft, as expected. However, due to it being so well behaved, it does allow to add a decent amount of sharpening without creating excessive halos. There are some, but not all that visible in normal 100 zoom web conditions, and they are symmetrical (Nicolas will like that).

The down-sampling with standard sharpening code in Windows Batch script dialect would then become something like:
convert ( "input.png" -depth 16 -set colorspace sRGB -colorspace RGB ) ^
 -filter Gaussian -distort Resize 800x800 ^
 -set colorspace RGB -colorspace sRGB ^
 -define convolve:scale=^130%%,100 -morphology Convolve DoG:0,0,0.5413815663895142 ^
 "output.png"

The Point Spread Function of the unsharpened (rather soft) version predicts zero haloing (but also a soft image), remove the -convolve code line for that. The Point Spread Function of the sharpened version (see code) predicts some haloing, depending on how the details align with the pixel grid, but generally creates very decent/crisp results with an amount of 130% High-pass filtering. Boosting the amount further will do even more detail enhancement, but may also lead to more clipping (something I try to find a remedy for). Sharpening will also make some aliasing more visible, but that will probably not be too visible in common non-synthetic images.

Cheers,
Bart
« Last Edit: July 23, 2014, 10:03:04 am by BartvanderWolf »
Logged
== If you do what you did, you'll get what you got. ==

Bart_van_der_Wolf

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 8913
Re: A free high quality resampling tool for ImageMagick users
« Reply #93 on: July 23, 2014, 10:40:59 am »

Attached a few examples of what the current state of affairs does to some samples that are available online:
1. A demonstration image with lots of nasty brick detail (but also other structures and surfaces) that either renders as mush or breaks up in aliasing artifacts, can be found here.

2. Another demonstration image also used as a target for some upsampling discussions here on LuLa can be found here

3. An image from the PhotoCD sampler disk, the Lighthouse in Maine, by photographer Alan Fink.

All were downsampled with the same settings (maximum 800 pixels, optimized algorithm, with standard (@130%) sharpening), as explained earlier.

Cheers,
Bart
« Last Edit: July 23, 2014, 12:08:35 pm by BartvanderWolf »
Logged
== If you do what you did, you'll get what you got. ==

Bart_van_der_Wolf

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 8913
Re: A free high quality resampling tool for ImageMagick users
« Reply #94 on: July 23, 2014, 10:46:24 am »

And here (attached) is the updated version (1.1.1) of the full Windows Batch script file.

Cheers,
Bart
Logged
== If you do what you did, you'll get what you got. ==

Jack Hogan

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 798
    • Hikes -more than strolls- with my dog
Re: A free high quality resampling tool for ImageMagick users
« Reply #95 on: July 23, 2014, 01:17:39 pm »

I'm making good progress I think, I've found the Gaussian filter combined with an EWA -distort Resize, to produce no halos in linear light space but is also a bit soft, as expected. However, due to it being so well behaved, it does allow to add a decent amount of sharpening without creating excessive halos. There are some, but not all that visible in normal 100 zoom web conditions, and they are symmetrical (Nicolas will like that).

The down-sampling with standard sharpening code in Windows Batch script dialect would then become something like:
convert ( "input.png" -depth 16 -set colorspace sRGB -colorspace RGB ) ^
 -filter Gaussian -distort Resize 800x800 ^
 -set colorspace RGB -colorspace sRGB ^
 -define convolve:scale=^130%%,100 -morphology Convolve DoG:0,0,0.5413815663895142 ^
 "output.png"

The Point Spread Function of the unsharpened (rather soft) version predicts zero haloing (but also a soft image), remove the -convolve code line for that. The Point Spread Function of the sharpened version (see code) predicts some haloing, depending on how the details align with the pixel grid, but generally creates very decent/crisp results with an amount of 130% High-pass filtering. Boosting the amount further will do even more detail enhancement, but may also lead to more clipping (something I try to find a remedy for). Sharpening will also make some aliasing more visible, but that will probably not be too visible in common non-synthetic images.

Hi Bart,

I am a bit out of my league here thinking in the frequency domain, but it seems like a bit of a pity to choose a well-behaved-yet-weaker filter to perform the initial filtering because it's well behaved, and then apply a stronger poorly behaved one in the reconstruction phase to make up for it. 

Does ImageMagick allow to perform some form of decent real deconvolution (such as LR) in-line?   If so, wouldn't it be better to use a gaussian deconvolution operator in the -define line instead of DoG to undo the gaussian filtering?  Or perhaps in addition to a weaker DoG?

As you may have guessed I am allergic to artificially amplified acutance (and alliteration) :)

Jack


« Last Edit: July 23, 2014, 01:42:18 pm by Jack Hogan »
Logged

NicolasRobidoux

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 280
Re: A free high quality resampling tool for ImageMagick users
« Reply #96 on: July 23, 2014, 02:46:20 pm »

Bart:
Resampling with a sharpening filter through linear light will only give "correct colors" across the board if there is no clipping (unless the clipping happens to be perfectly matched on both sides, a measure 0 event).
Again, I think you know this, but look at this:
Suppose you have a grey impulse on a black background. Resampling with any sharpening filter in any colorspace (does not matter which provided values past black are clipped at some point) will produce undershoots which will be clipped sooner or later. The overshoot of the gray, however, will not be clipped unless the chosen grey is nearly white.
Because of the clipping, the average value has just gone up a nudge.
(Things are a bit more complicated than this, but this gives the drift.)
Conclusion: A method that suitably compresses halos near black and white has the potential of more accurately preserving colors than resampling through linear light, at least in some situations.
It's a balancing game.
My line of attack on this has been to have a very smooth color transformation from linear to "perceptual" and back (gamma "passes" this criterion) so that locally (in colorspace) the gamma space is "almost linear" (meaning that locally it corresponds to an affine transformation applied to linear light). Hopefully, this means mid-tones are accurately resampled (as they are in linear light) but also that tight patterns with components near extreme values don't drift too much either.
(Not totally sure this is intelligibly written. Hopefully enough.)
« Last Edit: July 24, 2014, 08:34:14 am by NicolasRobidoux »
Logged

Bart_van_der_Wolf

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 8913
Re: A free high quality resampling tool for ImageMagick users
« Reply #98 on: July 24, 2014, 12:03:14 pm »

Hi Bart,

I am a bit out of my league here thinking in the frequency domain, but it seems like a bit of a pity to choose a well-behaved-yet-weaker filter to perform the initial filtering because it's well behaved, and then apply a stronger poorly behaved one in the reconstruction phase to make up for it.

Hi Jack,

It isn't as grim as you may have been led to believe. With down-sampling we effectively discard a number of pixels, which is an unrecoverable in most situations. We will also introduce some blur, due to the weighted averaging of pixels before creating new ones. Some filters are used to control this weighted averaging. All of these filters have benefits and drawbacks, so we need to find a nice (fit for purpose) filter with the most pleasing properties. We can basically fit the trade-offs into the following diagram.


We want to strike some balance between blurring, aliasing, blocking and ringing. Optimizing for one (reducing its prominence), will create more of the other artifacts, so many solutions tend to be optimally balanced somewhere in the middle of all. One may prefer a certain trade-off for certain types of image content (e.g. line drawings versus real photographic images).

Based on Nicolas' particular disgust for dark halo overshoots (ringing), and the general consensus that if we were to tolerate some (to enhance the sensation of sharpness, actually similar to what our eyes do) it should be balanced between over- and undershoot amplitude.

I've been going through most of the available filters in ImageMagick, and looked specifically for those that had relatively symmetrical (low) halo behavior in linear light (because of color mixing/blending benefits), but at the same time exhibited little tendency to generate aliasing artifacts (very likely to occur in downsampling). That takes care of two of the four possible artifact quadrant choices, and leaves blur versus blocking to avoid.

Blur is beneficial in reducing blocking (and noise), but can also be restored to a certain degree with deconvolution. The more advanced the Deconvolution process is, the better the restoration of original detail will be. When noise is restored less than signal is, we get a better Signal-to-Noise ratio.

Quote
Does ImageMagick allow to perform some form of decent real deconvolution (such as LR) in-line?   If so, wouldn't it be better to use a gaussian deconvolution operator in the -define line instead of DoG to undo the gaussian filtering?  Or perhaps in addition to a weaker DoG?

ImageMagic does have a (de)convolution function, but it is (currently) not as advanced as a more involved programs can offer. The 'Lightroom/ACR' version of deconvolution is not all that spectacular (it generate lot of artifacts very quickly), but there are other solutions that can do a spectacular job (e.g. FocusMagic).

ImageMagick's Difference of Gaussians (DoG) function can be used to generate a high-pass filter, but it is used as a single pass deconvolution kernel in the spatial domain, the way I use it. In the way I implemented that line of code, the two Gaussians now become an 'Impulse function', from which a Gaussian is subtracted, effectively producing a High-Pass filtered (high-spatial frequency) version of the image, that is then added to the original image.

Quote
As you may have guessed I am allergic to artificially amplified acutance (and alliteration) :)

And so am I, but a High-Pass filtering can be a very intelligent enhancer of actual detail, provided that the correct dimensions are chosen (!!!). It's the latter that can be achieved with adding a modest amount of the recovered High spatial frequency content, and using a properly dimensioned Gaussian component in the High-pass filter. It can also be (ab)used to add more of the high spatial frequency detail back than is recoverable in a single convolution pass. That is where taste and viewing conditions come into play.

To allow to follow one's own preferences, and allow to do more for certain viewing conditions, or less for specific further processing requirements (e.g. more advanced Deconvolution), I've added a user definable 'Amount' setting in the next release version of the script, so one can pick one's own amount of poison.

I've also found a better (slightly sharper but with less aliasing) filter than Gaussian. It is ....., drum roll ..., an non-standard as yet filter coined RobidouxSoft. It is soft, but not as soft as some others, it produces less aliasing artifacts than many others, has relatively symmetrical halo performance in linear light down-sampling, and as such is IMHO one of the best performers if one also allows for a sharpening step to follow its use. And even with sharpening, aliasing performance is not bad (of course depending on image source).

Cheers,
Bart
« Last Edit: July 24, 2014, 04:08:39 pm by BartvanderWolf »
Logged
== If you do what you did, you'll get what you got. ==

Bart_van_der_Wolf

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 8913
Re: A free high quality resampling tool for ImageMagick users
« Reply #99 on: July 24, 2014, 12:40:39 pm »

http://tonykuyper.wordpress.com/2013/05/11/choosing-sides-part-2-using-luminosity-masks-to-sharpen-images/ which I found in http://www.dpreview.com/forums/post/54065214

Hi Nicolas,

Yes, Tony Kuyper addresses the perceptual effects of sharpening. We'll get to that. I'm currently more interested in avoiding clipping, similar to a way I routinely do it in Photoshop with a sharpened version of the image in a layer on top of the unsharpened image.


I then blend the amount of the sharpened layer in proportion to the original layer's contrast. The higher/lower the brightness already is, the sharper the image already looks, the less sharpening it requires (maybe perceptually dark tone tolerate more). The extreme contrast that can lead to clipping is tapered of as it approaches the clipping points (White / Black). Now to find a way to simulate that in ImageMagick.

Cheers,
Bart
Logged
== If you do what you did, you'll get what you got. ==
Pages: 1 ... 3 4 [5] 6 7 ... 24   Go Up