Pages: 1 ... 11 12 [13] 14 15 ... 24   Go Down

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

Bart_van_der_Wolf

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 8913
Re: A free high quality resampling tool for ImageMagick users
« Reply #240 on: September 19, 2014, 08:12:39 am »

Francisco:
(Untested) I believe that the quick but less dirty answer has to do with inserting -type TrueColor just before the final conversion to JPEG.
http://www.imagemagick.org/script/command-line-options.php?#type

Hi Nicolas,

Indeed, which is what I had already implemented in the unpublished version 1.2.2 of the tool I was working on, before I got side-tracked by some urgent matters.

However, I implement it at the beginning of the IM convert command, in order to make all intermediate operations before writing the output aware of the requirement to maintain 3-channel RGB data, instead of simplifying to single channel gray scale.

Implementing the 'type TrueColor' parameter near the end may be more efficient, but there are also some potential gamma linearization issues caused by how ImageMagick interprets image gamma when it encounters single channel versus multichannel data. If I recall correctly, single channel images are assumed to already be in linear gamma, and multi-channel images are assumed to be in sRGB gamma space, unless specified otherwise first with a '-set colorspace' option. Maybe that behavior has changed in the mean time, I'm not sure.

Attached is the Version 1.2.2 implementation including the '-type TrueColor' addition, sorry for not posting it earlier.

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 #241 on: September 19, 2014, 08:27:53 am »

The downsizings were performed with the following parameters
Original image: JPEG, size 6148 x 3458
Output image size 1200 x 675

PS: Resample: automatic
LR: Resize to 1200 long edge, normal sharpen for screen output
IM+Script: Downsizing optimized, 1200 Long edge, 75% sharpening

The second image contains the center crop from each output file. The order from left to right: IM + Bart's Script; LR; PS.

The output from PS (right) shows some artifacts (rose windows) and is the worse of the three. LR performed a much better job (center) than PS but the image has a darker appearance than the original (similar to PS).

I'm attaching also (third image) a 100% crop of the original image to get the sense of the original details and brigthness in the rose window and bricks.

The output from IM + Bart's script (left) is IMO the better one. What surprised me was not the lack of artifacts (where LR also did a decent job), but how the texture of the bricks looks much better, closer to the original image while the LR and PS version look darker.

Hi Frank,

Yes, this clearly shows the benefits of down-sampling in linear gamma space. It is superior in maintaining tiny luminance differences and will also prevent near single pixel color errors when calculating the weigthed average of multiple pixels. It also shows that the Photoshop recommended (by Adobe) BiCubic Sharper method produces lots of aliasing artifacts, on the fine details of the window details, but even on slanted (e.g. roof) edges which produce clearly visible stair-stepping artifacts.

Thanks for your feedback. As I said before, the more the merrier.

Cheers,
Bart
« Last Edit: September 19, 2014, 08:40:25 am by BartvanderWolf »
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 #242 on: September 19, 2014, 04:57:06 pm »

Attached is the Version 1.2.2 implementation including the '-type TrueColor' addition

Hi Bart, excellent.  Can you summarize the state of the art (what functions are being used for downsampling, sharpening and deconvolving - and why)?

'preciate this a lot!

Jack

PS I also agree that yours looks the best out of the three that Francisco showed
Logged

NicolasRobidoux

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 280
Re: A free high quality resampling tool for ImageMagick users
« Reply #243 on: September 20, 2014, 03:31:25 am »

I agree with Bart that the main differentiator in Francisco's example is almost certainly doing the "local averaging" component of the downsampling in linear light.
Logged

Bart_van_der_Wolf

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 8913
Re: A free high quality resampling tool for ImageMagick users
« Reply #244 on: September 20, 2014, 05:58:30 am »

Hi Bart, excellent.  Can you summarize the state of the art (what functions are being used for downsampling, sharpening and deconvolving - and why)?

Hi Jack,

Sure, this is the executive summary:

All resampling methods that are used in the script, are based on Elliptical Weighted Averaging or EWA (therefore in two dimensions) instead of a two pass orthogonal tensor approach. This has the benefit of producing more 'organic looking' resizing because the resulting resolution is circularly symmetric, rather than having a higher diagonal resolution that is possible in a square pixel grid.
BTW, EWA resampling also has benefits for other types of (asymmetrical or variable) distortion.

A drawback of EWA resampling, besides the higher cost of having to process more samples, is that a (nearly) No-Op (scale close to 1.0) resampling will be slightly blurry, instead of having no effect on sharpness. Therefore, and for adaptability to different subject matter and viewing conditions, a sharpening parameter is added which allows to modify the behavior of the resampling and windowing filter used.

There are different filter methods available for upsampling and for down-sampling, and they are chosen because they perform better (less artifacting) for the intended scaling. The script is set up to allow choosing either filter method regardless of the fact of upsampling or down-sampling, but that's more for experimentation purposes.

The actual resampling results are a linear gamma space blend between two resampling operations. The resampling operations are performed in two different gamma spaces in order to cope with the different halo under-/overshoot amplitudes. The blending of those in linear gamma space is luminance driven which allows to address the different halo tendencies in darker and lighter tones, and it reduces the risk of clipping.

This is all based on a proposal by Nicolas Robidoux, after experimenting with other temporary contrast adjustment functions based on Sigmoidal tone curves. The approach with resampling in separate gamma spaces allows to better target the suppression of halo under/overshoots in different luminance ranges.

The upsampling operation (labeled as 'generic', because it also does pretty decent down-sampling) is implemented by using the EWA version of 'Lanczos' filtering, also known as 'Jinc' filtering, to make it circularly symmetric when simply scaling both dimensions by the same factor. Some 'deblur' is used, the amount is user adjustable. The deblur is controlled by modification of the filter support and window size.

The down-sampling operation is implemented by using the EWA version of 'Cubic' filtering, with an adjusted parameter choice to use a relatively soft filter version (from the family of Keys filter parameters), which is achieved with the following '-define filter:c=0.1601886205085204 -filter Cubic'. That defines a somewhat blurry filter, but also relatively halo and ringing free, which helps because down-sampling needs to actively avoid aliasing and ringing artifacts. It also blends the results of two resampling operations that were done in different gamma spaces, to allow and control halo generation.

To compensate for the softness/blur of the Cubic filter version, deconvolution sharpening is added. The deconvolution uses a Gaussian blur based weighting, and is implemented as a Dirac Delta function subtracted by a Gaussian blur, with the ImageMagick DoG (difference of Gaussians) function, which produces a 2-D convolution filter kernel.

There is a different down-sampling operation that is used when the choice is made to eliminate all sharpening, e.g. because one wants to use a separate utility for that. That operation only uses the EWA version of the 'Quadratic' filter in linear gamma space, without blending with other gamma space results. That filter produces very clean, essentially halo free, results (thus the possibility to skip blending with non-linear gamma space resampling) with very little aliasing but it's a bit blurry, and will therefore allow significant (e.g. local) sharpening without risk of enhancing existing halos.

As a bonus, an experimental option is added to the script to add a simple separate deconvolution sharpening operation, when zero or 'negative sharpening' (i.e. blur) was used.

All operations are executed in the spatial domain, to circumvent potential issues with limited 16-bit precision calculations that will create artifacts in the (Fourier converted) frequency domain. The future versions of ImageMagick will apparently also be available as already complied binaries that allow floating point Fourier transforms, which may help with faster and more precise calculations. It may also generate some other resampling options.

Cheers,
Bart
« Last Edit: September 20, 2014, 07:56:49 am 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 #245 on: September 20, 2014, 09:52:48 am »

Bart:
It is possible to construct EWA methods that are interpolatory. One example is -filter Triangle -distort Resize (EWA with a "cone" of radius 1, which is also the basis of the downsampling component of the NoHalo method built into GEGL, the new library core of GIMP).
Other examples are obtained by restricting any filter nonzero at the origin to the disc of radius 1, or shrinking a BC-cubic's radius to sqrt(2) instead of 2, fixing things so the zero crossing is at 1. (I've experimented with the unique Keys cubic that satisfies these conditions, and was not impressed with the result.)
One way or another, I have not managed to find/construct a "pure" EWA method that is both interpolatory and strongly antialiasing (at least, more strongly antialiasing than the "cone"). In other words, I don't know a non-contrived high quality EWA filter that reproduces the identity map when the geometrical transformation is "no-op".
« Last Edit: September 23, 2014, 05:13:22 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 #246 on: September 20, 2014, 10:44:11 am »

Bart:
It is possible to construct EWA methods that are interpolatory. One example is -filter Triangle -distort Resize (EWA with a "cone" of radius 1, which is also the basis of the downsampling component of the NoHalo method built into GEGL, the new library core of GIMP).
Other examples are obtained by restricting any filter nonzero at the origin to the disc of radius 1, or shrinking a BC-cubic's radius to sqrt(2) instead of 2, fixing things so the zero crossing is at 1. (I've experimented with the unique Keys cubic that satisfies these conditions, and was not impressed with the result.)
One way or another, I have not managed to find/construct an EWA method that is both interpolatory and strongly antialiasing (at least, more strongly antialiasing than the "cone").

Nicolas:
To make sure, I'm not criticizing the near No-Op blurring of EWA methods, it may even improve the look of certain image content that is almost aligned with the pixel grid and would fade in and out of perfect alignment (e.g. slightly rotated or tilted image content with a regular structure, like the noise example here).

Trade-offs are inevitable, and if one needs to fine tune for a very specific scenario, it's usually possible to find some optimization that does slightly better than a generic solution, but it's not so easy to find good compromises that do not cause unwanted side effects like aliasing. That's why I think that some sort of sharpening operation will allow to improve results that try to avoid artifacts.

Cheers,
Bart
« Last Edit: September 20, 2014, 10:47:24 am 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 #247 on: September 20, 2014, 12:01:58 pm »

...
This is all based on a proposal by Nicolas Robidoux, after experimenting with other temporary contrast adjustment functions based on Sigmoidal tone curves.
...
Quick comment: "Sigmoidization", the ancestor of "luminance weighted gamma resampling/sharpening", is basically dead. (It probably is useful for single channel, e.g. grayscale, images, but otherwise it is not recommended.) Main theoretical reason: http://www.luminous-landscape.com/forum/index.php?topic=77949.msg745796#msg745796
« Last Edit: September 20, 2014, 12:06:38 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 #248 on: September 21, 2014, 04:55:07 am »

Hi Jack,

Sure, this is the executive summary: ...

The down-sampling operation is implemented by using the EWA version of 'Cubic' filtering, with an adjusted parameter choice to use a relatively soft filter version (from the family of Keys filter parameters), which is achieved with the following '-define filter:c=0.1601886205085204 -filter Cubic'. That defines a somewhat blurry filter, but also relatively halo and ringing free, which helps because down-sampling needs to actively avoid aliasing and ringing artifacts. It also blends the results of two resampling operations that were done in different gamma spaces, to allow and control halo generation.

To compensate for the softness/blur of the Cubic filter version, deconvolution sharpening is added. The deconvolution uses a Gaussian blur based weighting, and is implemented as a Dirac Delta function subtracted by a Gaussian blur, with the ImageMagick DoG (difference of Gaussians) function, which produces a 2-D convolution filter kernel.

There is a different down-sampling operation that is used when the choice is made to eliminate all sharpening, e.g. because one wants to use a separate utility for that. That operation only uses the EWA version of the 'Quadratic' filter in linear gamma space, without blending with other gamma space results. That filter produces very clean, essentially halo free, results (thus the possibility to skip blending with non-linear gamma space resampling) with very little aliasing but it's a bit blurry, and will therefore allow significant (e.g. local) sharpening without risk of enhancing existing halos.

As a bonus, an experimental option is added to the script to add a simple separate deconvolution sharpening operation, when zero or 'negative sharpening' (i.e. blur) was used.

All operations are executed in the spatial domain, to circumvent potential issues with limited 16-bit precision calculations that will create artifacts in the (Fourier converted) frequency domain. The future versions of ImageMagick will apparently also be available as already complied binaries that allow floating point Fourier transforms, which may help with faster and more precise calculations. It may also generate some other resampling options.

Cheers,
Bart

Wow Bart, excellent effort and explanation!  What happened to Lanczos for downsampling?  And is b=1 in the cubic -filter with the specified c?
« Last Edit: September 21, 2014, 05:14:45 am by Jack Hogan »
Logged

Bart_van_der_Wolf

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 8913
Re: A free high quality resampling tool for ImageMagick users
« Reply #249 on: September 21, 2014, 07:18:51 am »


Wow Bart, excellent effort and explanation!  What happened to Lanczos for downsampling?

Thanks Jack,

Lanczos (or its EWA circular equivalent Jinc implementation) is still a pretty good filter for discrete (quantized) image source data. That's why it is the basis for upsampling. However, it also creates a bit of ringing (depending on edge/detail contrast, which is lower for upsampling but potentially higher for down-sampling). While that may not be really noticeable on most down-sampled images that are intended to be displayed relatively small, I also anticipated a requirement for post down-sample sharpening which would make it more visible. Also, linear gamma space down-sampling will preserve those ringing artifacts very well.

Therefore I picked up on the observations made by Nicolas Robidoux about the usability of the Keys Cubic family of filters that satisfy the 2C+B=1 relationship. Given the 'need' (IMHO) for sharpening after resampling, I opted for a relatively softer variant of the filter family than those near the Robidoux/Mitchell region (near B=C=1/3), because they exhibit more blocking/ringing risk. I verified that the relatively soft 'filter:c=0.1601886205085204' parameter, which was nominated for becoming a named filter (simpler and more understandable to encode) would produce a useful deconvolution sharpening setting.

Quote
And is b=1 in the cubic -filter with the specified c?

According to the ImageMagick documentation:  If one of the 'b' or 'c' settings has not been defined or set, its value will be calculated from the other value on the assumption that you are wanting a 'Keys' family filter (along the dotted line in the Mitchell-Netravali Survey diagram).

This still allows to specify any other 'b' value in addition to that 'c' value if that benefits specific image content. That's another benefit of using Keys Cubics instead of Lanczos, it's highly tweakable to image content, and it's also relatively efficient to code at a lower level (it's also used in some OpenGL / GPU oriented software implementations, see example 24-7).

Changing the 'c' (or 'b') parameter, does require to re-evaluate the currently used deconvolution settings because they are specifically optimized for the current 'RobidouxSoft' setting.

Cheers,
Bart

P.S. I may seem that I'm overly concerned about ringing and blocking in down-sampled image content, but I've seen and shot enough images that created problems for down-sampling to want to avoid that.
« Last Edit: September 21, 2014, 07:47:35 am by BartvanderWolf »
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 #250 on: September 21, 2014, 04:17:14 pm »

P.S. I may seem that I'm overly concerned about ringing and blocking in down-sampled image content, but I've seen and shot enough images that created problems for down-sampling to want to avoid that.

Thank you Bart, I am 100% with you there.

Jack
Logged

NicolasRobidoux

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 280
Re: A free high quality resampling tool for ImageMagick users
« Reply #251 on: September 23, 2014, 03:07:01 am »

I'd be curious to see the result of downsampling Franks' church image with the "generic" scheme. It would give a hint as to whether Frank got better results mostly because of downsampling through linear light is to perform the same reduction with the "generic" scheme instead of the one targeted at downsampling. (Of course, the "generic" scheme does "half" of the downsampling through linear light, so this only sort of hints at what is going on...)
« Last Edit: September 23, 2014, 05:28:48 am by NicolasRobidoux »
Logged

NicolasRobidoux

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 280
Re: A free high quality resampling tool for ImageMagick users
« Reply #252 on: September 23, 2014, 03:29:03 am »

Jim: I am certain that you are not the first to want to interface ImageMagick and Matlab.
If google does not settle the question, my guess is that if you post a query at http://imagemagick.org/discourse-server/viewforum.php?f=1, you'll get bites.
« Last Edit: September 23, 2014, 03:30:42 am by NicolasRobidoux »
Logged

fdisilvestro

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 1852
    • Frank Disilvestro
Re: A free high quality resampling tool for ImageMagick users
« Reply #253 on: September 23, 2014, 05:37:12 am »

I'd be curious to see the result of downsampling Franks' church image with the "generic" scheme. It would give a hint as to whether Frank got better results mostly because of downsampling through linear light is to perform the same reduction with the "generic" scheme instead of the one targeted at downsampling. (Of course, the "generic" scheme does "half" of the downsampling through linear light, so this only sort of hints at what is going on...)

Hi Nicolas,

The attached image shows the comparison between the downsampling-optimized (left) vs the generic scheme (right). The generic scheme gives similar results to LR. The optimized scheme does make a difference.

Regards,

Bart_van_der_Wolf

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 8913
Re: A free high quality resampling tool for ImageMagick users
« Reply #254 on: September 23, 2014, 05:54:18 am »

I'd be curious to see the result of downsampling Franks' church image with the "generic" scheme.

If Frank is reading this, what Nicolas would like to see is a comparison with the script:
1. Choose the same output size as before and Down-sample as you did.
2. Choose the same output size as before and 'Upsample' down like before.

By selecting 'Up' instead of 'Down', the 'generic' EWA Lanczos filter is used, instead of the EWA Keys Cubic filter.

The optimized Down-sampling method with a sharpening amount of 50 will probably produce slightly sharper results than the generic method with sharpening 'amount' of 50. That can also produce a slightly higher risk of micro-stair-stepping jaggies on very fine lines that are smaller than 1 pixel wide, but also slightly crisper results.

Both methods use linear gamma resizing/blending, so I do not expect too much of a difference from that, it's more to do with the difference in filter weights.   

Cheers,
Bart

P.S. I see that Frank already responded.
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 #255 on: September 23, 2014, 06:10:08 am »

There is a big difference between "generic" and "downsample" in the way they use nonlinear light (namely gamma 3):
"Downsample" prefilters/resamples (only) in linear light and uses a pair of gamma spaces to deconvolve the downsampled result.
"Generic" prefilters/resamples with a pair of gamma spaces (and, if deconvolution is added to the result of the dowmsamping, again uses a pair of gamma spaces).
IMHO this is a more significant difference than using different EWA weights (and support): It is a "structural" difference, as opposed to "parametric".
(Whether MHO is correct could of course be settled using the same filter in both "downsample" and "generic", comparing the result of only using linear light in the resample stage with the result of blending two resampling results.)
-----
In summary: "generic" resamples using luminance-weighted gamma blending, and "downsample" sharpens a linear light result using luminance-weighted gamma blending.
« Last Edit: September 23, 2014, 06:19:54 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 #256 on: September 23, 2014, 06:19:04 am »

There is a big difference between "generic" and "downsample" in the way they use nonlinear light (namely gamma 3):
"Downsample" prefilters/resamples (only) in linear light and uses a pair of gamma spaces to deconvolve the downsampled result.
"Generic" prefilters/resamples with a pair of gamma spaces (and, if deconvolution is added to the result of the dowmsamping, again uses a pair of gamma spaces).
IMHO this is a more significant difference than using different EWA weights (and support): It is a "structural" difference, as opposed to "parametric".
(Whether MHO is correct could of course be settled using the same filter in both "downsample" and "generic", comparing the result of only using linear light in the resample stage with the result of blending two resampling results.)
-----
In summary: "generic" resamples using luminance-weighted gamma blending, and "downsample" sharpens a linear light result using luminance-weighted gamma blending.

Nicolas, you are correct. It's the suppression of Lanczos/Jinc ringing (under-shoot) with a Gamma blend that seems to produce most of the difference for downsampling. The Down-sampled Sharpening gamma blend just produces more symmetrical edge sharpening.

Cheers,
Bart
« Last Edit: September 23, 2014, 06:26:45 am 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 #257 on: September 23, 2014, 06:58:17 am »

Of course, none of this hair splitting would matter if the results were not as good as they appear to be.
(Bart: My apologies for being in a "pedantic mood". Has to do with sleep deprivation, I think. Much celebration lately.)
P.S. Come to think of it, I'm generally in a pedantic mood. Ah well.
« Last Edit: September 23, 2014, 07:03:14 am by NicolasRobidoux »
Logged

NicolasRobidoux

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 280
Re: A free high quality resampling tool for ImageMagick users
« Reply #258 on: September 23, 2014, 07:11:33 am »

Thank you Frank. Very interesting comparison.
Logged

fdisilvestro

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 1852
    • Frank Disilvestro
Re: A free high quality resampling tool for ImageMagick users
« Reply #259 on: September 23, 2014, 08:26:24 am »

Hi, I have another image for comparison between the optimized-downsampling (Bart's Script) method and Photoshop Automatic resize mode. The first attachment is the complete image from the Melbourne convention centre downsampled (optimized scheme) to 1200 pixels wide.

The second image contains the comparison in the following order:

- The top section is the output from Photoshop resizing, shown at 300%
- The middle section is the output from the optimized-downsampling script, also at 300%
- The bottom section is the full size image, shown at 50% (which is not exactly the same size as the other two, but close for comparison purposes)

A few areas for comparison:

- Area "A", top of the handrails for the stairs, looks darker in the PS resized image.
- Area "B", in the PS resize, detail has been lost completely with the PS resizing.
- Area "C", shows a pronounced halo in the PS resized image.

Following Bart's instructions to have the script available as an option in the "send to" submenu (windows) makes this option something really easy to use. I'll have to study a little bit of the scripting language to be able to send an entire folder or group of images at once.

Regards,
Pages: 1 ... 11 12 [13] 14 15 ... 24   Go Up