Pages: 1 [2] 3 4 ... 24   Go Down

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

NicolasRobidoux

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

Yes it's optimized for enlarging. But I'm hoping something like it can make downsampling better too.
This being said, downsampling through linear light is really hard to beat.
P.S. However, with moderate gamma, this could give you a slider that tames the halos just enough to make up for the fact that linear light can introduce pretty brutal ones with a sharpening resampling filter (like LanczosRadius).
« Last Edit: July 19, 2014, 03:48:15 pm by NicolasRobidoux »
Logged

NicolasRobidoux

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 280
Re: A free high quality resampling tool for ImageMagick users
« Reply #21 on: July 19, 2014, 03:30:35 pm »

Suggested variant:
Instead of blending the gamma resample with linear light, negate the linear light version of the input image, use the same gamma, resample the same way, undo the gamma, negate again (to be back in linear light).
Then, use the luminance of the average of the two results instead of the linear light resample's luminance (which is not computed anymore), normalized of course, as alpha (blending parameter).
Why? The core of the method is then black/white symmetric.
-----
I am far from certain that this, or something like it, would give better results than the "blend linear light with gamma" version. The HVS (like "the real world) is >>not<< black/white symmetric.
The other difficulty is that for this to work well we most likely need to set the "white" used by the negation operators to fit the input and resampled images well.
Again courtesy of Fred Weinhaus, here is sample code
convert \( input.png -depth 16 -set colorspace sRGB -colorspace RGB \) \
\( -clone 0 -gamma 3 -filter LanczosRadius -distort Resize 800% -gamma 0.333333333333333 \) \
\( -clone 0 -negate -gamma 3 -filter LanczosRadius -distort Resize 800% -gamma 0.333333333333333 -negate \) \
\( -clone 1 -clone 2 -evaluate-sequence mean -colorspace gray -auto-level \) \
-delete 0 -compose over -composite \
-set colorspace RGB -colorspace sRGB output.png
« Last Edit: July 19, 2014, 04:01:58 pm by NicolasRobidoux »
Logged

NicolasRobidoux

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

Crude as it is, the "positive/negative gamma space blending" works amazingly well in quick testing performed by Fred.

Pushing this one step further for downsampling use:

The gamma used for the "positive" resampling (higher gamma -> less dark halos) and the gamma used for the "negative" resampling (higher gamma -> less light halos) could be parameters.

Users would then, for example, downsample using a sharpening filter (like EWA LanczosRadius or, even LanczosSharpest 4, one of the Robidoux ones, LanczosSharp or Lanczos) and move each gamma up from 1 to reduce the corresponding type of halo. The gammas can be set independently.

The goal would be to stay as close as possible to gamma 1 for both (because this is conducive to accurate midtones) without letting halos mess things up.

At least this is the idea.
« Last Edit: July 19, 2014, 04:07:21 pm by NicolasRobidoux »
Logged

NicolasRobidoux

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 280
Re: A free high quality resampling tool for ImageMagick users
« Reply #23 on: July 19, 2014, 04:12:28 pm »

Great work guys, this is very useful.  A couple of questions:

1) Is the script optimized for enlarging as opposed to shrinking?  I am asking because I see gammas greater than one flying around :-)
2) If one works in AdobeRGB, is it enough to simply change all references of 'sRGB' to 'AdobeRGB' in the script?
3) I sometimes generate a B&W image directly from a Bayer raw file. The resulting full resolution 'grayscale' TIF is still linear (gamma=1).   Assuming one wanted to downsize only, could all the colorspace changing, gamma and cloning be replaced by a single line such as
convert "%~dpnx1" -filter LanczosRadius -distort Resize %Msize% ?
4) If one wanted a TIFF as an output would they simply leave out the -quality line?

Thank you very much!
Jack
This is the kind of questions you'd get answers for at the ImageMagick forums http://imagemagick.org/discourse-server/viewforum.php?f=1
Logged

NicolasRobidoux

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 280
Re: A free high quality resampling tool for ImageMagick users
« Reply #24 on: July 20, 2014, 03:08:47 am »

Samples of the "black/white symmetric" version (compared to resizing through linear RGB with sRGB primaries and L*a*b*) courtesy of Fred Weinhaus: http://www.imagemagick.org/discourse-server/viewtopic.php?f=22&t=25935&p=113483#p113483
« Last Edit: July 20, 2014, 03:22:45 am by NicolasRobidoux »
Logged

Bart_van_der_Wolf

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 8911
Re: A free high quality resampling tool for ImageMagick users
« Reply #25 on: July 20, 2014, 04:33:47 am »

Great work guys, this is very useful.  A couple of questions:

1) Is the script optimized for enlarging as opposed to shrinking?  I am asking because I see gammas greater than one flying around :-)

Hi Jack,

Indeed, it does best with enlarging, but it's not all that bad for shrinking either. Afterall, the first implementation, is a blend between a linear gamma and a gamma 3 (= even darker brighter looking) image, which will be approx. gamma 1/2.2 reverted after the blend. As Nicolas said, it's hard to beat linear light for downsampling though.

Quote
2) If one works in AdobeRGB, is it enough to simply change all references of 'sRGB' to 'AdobeRGB' in the script?

To clarify, 'RGB' is used to linearize gamma (assuming sRGB source), and 'sRGB' is used to apply a gamma adjustment (assuming linear gamma source). A linearization followed by a non-linearization should therefore produce the exact same result as one started with. Only the pixel values that were changed in linear light will be different in the result.

For specific profile conversions, such as from Adobe RGB to something else and back, one must use the  '-profile'  option. That option allows to convert between two profiles (by specifying two ICC or ICM profiles), or assign a profile (by specifying a single profile) before saving the new image.

Quote
3) I sometimes generate a B&W image directly from a Bayer raw file. The resulting full resolution 'grayscale' TIF is still linear (gamma=1).   Assuming one wanted to downsize only, could all the colorspace changing, gamma and cloning be replaced by a single line such as
convert "%~dpnx1" -filter LanczosRadius -distort Resize %Msize% ?

When starting with a (adjusted for Bayer CFA transmission differences) linear gamma image, one can indeed skip the prior 'RGB' linearization step, and directly execute the '-distort Resize' option with an amount. Different filters are available, all with different characteristics with regards to sharpness or blurriness and types of artifacts that are suppressed or boosted. The 'LanczosRadius' generally produces excellent results, so that's why it is used for the moment.

Quote
4) If one wanted a TIFF as an output would they simply leave out the -quality line?

Yes, I've added the -quality line to specifically maximize the compression quality of JPEGs and PNGs.

Cheers,
Bart
« Last Edit: July 28, 2014, 03:06:57 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 #26 on: July 20, 2014, 05:14:22 am »

...
Indeed, it does best with enlarging, but it's not all that bad for shrinking either. Afterall, the first implementation, is a blend between a linear gamma and a gamma 3 (= even darker looking) image, which will be approx. gamma 1/2.2 reverted after the blend.
...
There are basically three stages of colorspace conversions:
Preprocessing: Convert from whatever colorspace you are using to something that can be interpreted as linear RGB (or linear w.r.t. some "primaries", like XYZ) or linear grayscale.
If the input is sRGB, this is done with
convert input -set colorspace sRGB -colorspace RGB
If the input is not a built in IM colorspace, you must use http://www.imagemagick.org/Usage/formats/#color_profile. If the input is Adobe RGB and the profile is imbedded, I think all you need to do is
convert input -profile
and then you are in business. I've posted on the ImageMagick Forums to make sure: http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=25945. Waiting for an educated reply.
Processing (resampling): Besides applying a linear filter, the resampling stage contains colorspace operations. Every single colorspace operation performed here must be undone (in reverse order) so as to accurately return to linear RGB. So, if one applies -gamma 3 before a resizing operation (-distort Resize 800%, for example), one must apply -gamma .33333333333333 after. If one applies -negate before, one must apply -negate after. Not doing so will lead to unacceptable color (or intensity, if in grayscale) drifts. In the colorspace department, what you "do", you must "undo" in a round trip. (The blending is a different story, but what is blended is supposed to be two linear light versions of enlarging the same image. We are staying put in linear RGB in the blending stage.)
Post-processing: Then, you need to return from linear RGB to Adobe RGB (if this is what you want your output image to be). Again, the ImageMagick Forum should illuminate how this is done.
« Last Edit: July 20, 2014, 06:01:47 am by NicolasRobidoux »
Logged

Jack Hogan

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 797
    • Hikes -more than strolls- with my dog
Re: A free high quality resampling tool for ImageMagick users
« Reply #27 on: July 20, 2014, 05:56:41 am »

This is the kind of questions you'd get answers for at the ImageMagick forums http://imagemagick.org/discourse-server/viewforum.php?f=1

Ok, thank you.
Logged

Jack Hogan

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 797
    • Hikes -more than strolls- with my dog
Re: A free high quality resampling tool for ImageMagick users
« Reply #28 on: July 20, 2014, 05:59:53 am »

Hi Jack,

Indeed, it does best with enlarging, but it's not all that bad for shrinking either. Afterall, the first implementation, is a blend between a linear gamma and a gamma 3 (= even darker looking) image, which will be approx. gamma 1/2.2 reverted after the blend. As Nicolas said, it's hard to beat linear light for downsampling though.

To clarify, 'RGB' is used to linearize gamma (assuming sRGB source), and 'sRGB' is used to apply a gamma adjustment (assuming linear gamma source). A linearization followed by a non-linearization should therefore produce the exact same result as one started with. Only the pixel values that were changed in linear light will be different in the result.

For specific profile conversions, such as from Adobe RGB to something else and back, one must use the  '-profile'  option. That option allows to convert between two profiles (by specifying two ICC or ICM profiles), or assign a profile (by specifying a single profile) before saving the new image.

When starting with a (adjusted for Bayer CFA transmission differences) linear gamma image, one can indeed skip the prior 'RGB' linearization step, and directly execute the '-distort Resize' option with an amount. Different filters are available, all with different characteristics with regards to sharpness or blurriness and types of artifacts that are suppressed or boosted. The 'LanczosRadius' generally produces excellent results, so that's why it is used for the moment.

Yes, I've added the -quality line to specifically maximize the compression quality of JPEGs and PNGs.

Cheers,
Bart

Excellent, thank you Bart.  I have an unrelated question for you but I'll start a separate thread on it.
Jack
Logged

Bart_van_der_Wolf

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 8911
Re: A free high quality resampling tool for ImageMagick users
« Reply #29 on: July 20, 2014, 06:59:50 am »

Again courtesy of Fred Weinhaus, here is sample code
convert \( input.png -depth 16 -set colorspace sRGB -colorspace RGB \) \
\( -clone 0 -gamma 3 -filter LanczosRadius -distort Resize 800% -gamma 0.333333333333333 \) \
\( -clone 0 -negate -gamma 3 -filter LanczosRadius -distort Resize 800% -gamma 0.333333333333333 -negate \) \
\( -clone 1 -clone 2 -evaluate-sequence mean -colorspace gray -auto-level \) \
-delete 0 -compose over -composite \
-set colorspace RGB -colorspace sRGB output.png

And here is the Windows batch file version:
convert ( input.png -depth 16 -set colorspace sRGB -colorspace RGB ) ^
 ( -clone 0 -gamma 3 -filter LanczosRadius -distort Resize 800%% -gamma 0.333333333333333 ) ^
 ( -clone 0 -negate -gamma 3 -filter LanczosRadius -distort Resize 800%% -gamma 0.333333333333333 -negate ) ^
 ( -clone 1 -clone 2 -evaluate-sequence mean -colorspace gray -auto-level ) ^
 -delete 0 -compose over -composite ^
 -set colorspace RGB -colorspace sRGB output.png

However, I find that this (Gamma 3 , Gamma 3) resampled version produces much more visible aliasing that the previous (Linear , Gamma 3) resampled version, both blended in linear light. I know that the goal is to reduce haloing, but we should be careful about the trade-off with other artifacts.

Try the attached top left 150x150 pixel crop of a 1000x1000 pixel Rings (zoneplate) target. The previous version already introduces some aliasing, but on most normal images this would be even less noticeable. This new version produces much more visible aliasing because the lighter aliases are amplified. Perceptually that would be much easier to see, so I wonder if this introduces a higher risk of visibility in average 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 #30 on: July 20, 2014, 07:13:58 am »

..
Try the attached top left 150x150 pixel crop of a 1000x1000 pixel Rings (zoneplate) target. The previous version already introduces some aliasing, but on most normal images this would be even less noticeable. This new version produces much more visible aliasing because the lighter aliases are amplified. Perceptually that would be much easier to see, so I wonder if this introduces a higher risk of visibility in average images.
...
Indeed, the aliasing is ugly.
I totally agree that artifacts need to be "balanced".
(Note that gamma 3 in reverse video is quite strong. Gamma 3, in and by itself, is quite strong.)
Is this an enlargement? Downsample? Details?
-----
I must say that the "gamma 3/linear" blend, that does not use reverse video gamma 3 instead of linear light, has a lot of things going for it that suggest that it will be more foolproof. Your test points in this direction.
« Last Edit: July 20, 2014, 07:18:03 am by NicolasRobidoux »
Logged

Bart_van_der_Wolf

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 8911
Re: A free high quality resampling tool for ImageMagick users
« Reply #31 on: July 20, 2014, 07:48:22 am »

Is this an enlargement? Downsample? Details?

Nicolas, it's a crop from the attached image, and converted from GIF to PNG.
It's also a nice test for display gamma, because viewing an LCD at an angle can change the perceived gamma.

A warning for Photoshop users, if you want to explore in PS you need to first convert this indexed color GIF to RGB, because resizing the GIF in PS produces ugly artifacts, as I mentioned on my (old and needs an update to correct some bug fixed IM filter example) web page about the perils of down-sampling.

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 #32 on: July 20, 2014, 07:49:40 am »

Did you just resize to the same exact size to check for awful nonsense?
P.S. This is what I missed: input image = ?, operation = ?.
« Last Edit: July 20, 2014, 07:51:11 am by NicolasRobidoux »
Logged

Bart_van_der_Wolf

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 8911
Re: A free high quality resampling tool for ImageMagick users
« Reply #33 on: July 20, 2014, 08:05:12 am »

I must say that the "gamma 3/linear" blend, that does not use reverse video gamma 3 instead of linear light, has a lot of things going for it that suggest that it will be more foolproof. Your test points in this direction.

Yes, it will be interesting to see what the ImageMagick audience appreciates more.

The differences between both methods on up-sampled 'normal' images (the few that I have tried) are hardly perceptible.

Down-sampling is worse with the double Gamma 3 approach, although one could create an other optimization specifically for down-sampling, and maybe just switch to linear light for the downsizing with a good filter, potentially followed by a bit of sharpening (High-pass convolution is accurately tweakable in floating point precision, yet quick because it requires only a small support size appropriate for down-sampled detail).

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 #34 on: July 20, 2014, 08:07:31 am »

My current inclination is this:

Use the linear/gamma blend, increasing gamma from 1 until dark halos are bearable (without reaching the point where light halos become unberable). This approach works when downsampling too. In this case, however, it's quite likely that gamma will stay near 1 quite often <- guess.
Question: With a sharpening filter (LanczosRadius, for example), is the gamma blended result (with 3 or some other, lesser, value) ever better than the linear light result? (Or course taste enters here, since halo is a personal taste/distaste.)
« Last Edit: July 20, 2014, 08:17:40 am by NicolasRobidoux »
Logged

Bart_van_der_Wolf

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 8911
Re: A free high quality resampling tool for ImageMagick users
« Reply #35 on: July 20, 2014, 08:10:38 am »

Did you just resize to the same exact size to check for awful nonsense?
P.S. This is what I missed: input image = ?, operation = ?.

I'm not sure I understand.
 
ImageMagic handles GIFs correctly when downsampling, so it should not matter if the GIF is used as source image, or a PNG conversion (the PNG crop was only a file format conversion, no resizing).

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 #36 on: July 20, 2014, 08:12:33 am »

Bart: You showed a rings image obtained, if I understand, with the "blend of gamma 3 straight and through negation".
I imagine you resized some input image. What were the dimensions of this input image? In other words, what you showed (both in full and cropped form), is the output of what operation? Your last answer states that what you showed us is the result of downsampling. What ratio/percentage reduction in dimensions?
« Last Edit: July 20, 2014, 08:16:23 am by NicolasRobidoux »
Logged

NicolasRobidoux

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 280
Re: A free high quality resampling tool for ImageMagick users
« Reply #37 on: July 20, 2014, 08:21:27 am »

Bart:
I would guess that we both know that it is unlikely that downsampling more than a trivial amount through something else than linear light should be the basis of a general recommendation.
This being said, when the filter sharpens strongly, it is my guess that deviating a little may sometimes bring benefits.
Now that's a strong statement.
Logged

Bart_van_der_Wolf

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 8911
Re: A free high quality resampling tool for ImageMagick users
« Reply #38 on: July 20, 2014, 08:27:36 am »

Bart: You showed a rings image obtained, if I understand, with the "blend of gamma 3 straight and through negation".

The only full rings (GIF) image I attached in this thread (Reply #31) is an original Computer Generated Image (CGI), no resampling was involved. The CropTL.png I posted was a simple crop of the top-left corner of that GIF and saved as PNG.

Quote
I imagine you resized some input image. What were the dimensions of this input image?

No, just a CGI original, original size. It's the same GIF as linked to in the Down-sampling webpage.

Quote
In other words, what you showed (both in full and cropped form), is the output of what operation?

CGI generated, original size. It was inspired by this, but I'm not sure (don't remember) if I used exactly that approach or something I made in Mathematica.

Cheers,
Bart
« Last Edit: July 20, 2014, 08:45:57 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 #39 on: July 20, 2014, 08:32:00 am »

So, what you were basically saying was: "Put this rings image through downsampling with the new methods and see what happens? You'll see that the "gamma 3 negative" version is worse than the other."?
P.S. Just checked by downsampling to 12.5%. Linear light clearly wins, followed by blending the result through gamma 3 with linear light, followed by blending the result through gamma 3 with the result of resampling through negated gamma 3. The last two positions are reversed, however, if one considers overall tone preservation (instead of aliasing). Which makes me wonder if rounding error is part of the problem.
P.S. 2 If using blending with linear light but using gamma 2 instead of 3 as intermediate gamma space, the result of downsampling is visibly less aliased than when resampling through linear light. Maybe I overdid it with gamma 3? Or is this particular to how this Rings test image was constructed?
« Last Edit: July 20, 2014, 10:32:05 am by NicolasRobidoux »
Logged
Pages: 1 [2] 3 4 ... 24   Go Up