unsharp parameters: GIMP -> IM

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?".
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

unsharp parameters: GIMP -> IM

Post by GreenKoopa »

Does anyone know how to translate the parameters from GIMP'S Unsharp Mask to IM's -unsharp operation?

IM's unsharp operation has the parameters radius, sigma, gain, and threshold.
http://www.imagemagick.org/script/comma ... hp#unsharp
radius (default=0 to calculate based on sigma, rarely needs to be set)
sigma
gain (default=1.0)
threshold (default=0.05)

GIMP's unsharp has the parameters radius, amount, and threshold.
radius = 0.1-500.0 (default=5.0)
amount = 0.00-10.00 (default=0.50)
threshold = 0-255 (default=0)

Threshold seems obvious, simply divide by 255. But the others?

First I found
http://www.imagemagick.org/Usage/resize/#resize_unsharp
which suggested that
im_radius = gimp_radius
im_sigma = gimp_amount
This doesn't appear to be correct.

Then I found
http://www.imagemagick.org/Usage/blur/#unsharp
which states that
im_sigma = gimp_radius + 1 (or maybe sqrt(gimp_radius))
im_gain = gimp_amount
This appears closer, but still not correct.

I experimented a little but didn't find an obvious relationship. Does anyone already know, or have an idea on the best way to continue experimenting? I am new to sharpening.

I have read that Photoshop and GIMP use the same parameters, but I don't have Photoshop to verify.

GIMP 2.8.6
ImageMagick 6.8.6-3
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: unsharp parameters: GIMP -> IM

Post by Bonzo »

I am now a proud owner of Photoshop 8) although I have done very little with it yet!!

It has the options of Amount, Radius and Threshold for the unsharp mask. That is Percent, pixels and levels.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: unsharp parameters: GIMP -> IM

Post by snibgo »

Hmm. With Gimp Amount=1.00, Threshold 0:

Gimp radius 2 is close to IM 9x3
Gimp radius 4 is close to IM 15x5

Make of that what you will.
snibgo's IM pages: im.snibgo.com
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: unsharp parameters: GIMP -> IM

Post by GreenKoopa »

That supports im_sigma = gimp_radius + 1? But if the range of gimp_radius is 0.1-500.0, then the range of im_sigma would be 1.1-501. What is a generally useful range for im_sigma?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: unsharp parameters: GIMP -> IM

Post by snibgo »

I don't normally use IM larger than 0x2. Larger radii don't change the sharpening of fine detail, but do sharpen (ie increase contrast) at larger levels of detail. 0x5 makes photos look horrible, because it generates spurious detail that distracts from real detail. But increasing to much higher values, eg 300x100, looks okay because we don't really notice the effect at such long wavelengths. However, Gimp at radius 100 looks different to IM 300x100; the fine detail in Gimp looks sharper.
snibgo's IM pages: im.snibgo.com
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: unsharp parameters: GIMP -> IM

Post by GreenKoopa »

I did some testing for GIMP Radius = 0.5 - 6 and Amount = 1 - 6. I have determined that GIMP Amount is exactly the same parameter as IM Gain. IM Sigma is GIMP Radius plus not quite one, and IM radius is twice that. I find it strange that GIMP calls a parameter radius when pixels 2*(radius+1) distant are changed. I also noticed that GIMP has unnecessarily huge rounding errors (it's 8-bit) for large Amounts/Gains.

So this documentation is incorrect
http://www.imagemagick.org/Usage/resize/#resize_unsharp
The "GIMP" default for unsharp is equivalent to "-unsharp 5x0.5"
really should read
The "GIMP" default (radius=5, amount=0.5, threshold=0) for unsharp is equivalent to "-unsharp 12x6+0.5+0"
and this is correct (other than ignoring that GIMP sets a hard radius at twice sigma)
http://www.imagemagick.org/Usage/blur/#unsharp
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: unsharp parameters: GIMP -> IM

Post by anthony »

I have made the suggested correction. Should appear in a couple of hours
The "GIMP" default (radius=5, amount=0.5, threshold=0) for unsharp is equivalent to "-unsharp 12x6+0.5+0", and this is correct (other than ignoring that GIMP sets a hard radius at twice sigma). However remeber you really do not need to specify the kernel radius in ImageMagick, so a value of "-unsharp 0x6+0.5+0" will work better.
However how did you arrive at this?
was it.. gimp_radius = 5, ==> IM_sigma = gimp_radius+1 ==> IM kernal radius = IM_sigma*2

NOTE: sigma*2 for the kernal radius (size fo the convolution kernel used) is a fairly good estimate, and generally good enough. However if set to zero, IM calculates the kernel size so that the kernel still have a greater than Q-level quantum limit effect on the image, at the extreme edge of the kernel. As such it is variable and based on sigma and the Quality of your IM. The function is a standard 'Graphics Gem' (from the books of the same name).
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: unsharp parameters: GIMP -> IM

Post by fmw42 »

Greenkoopa wrote:The "GIMP" default for unsharp is equivalent to "-unsharp 5x0.5"
I have my doubts that GIMP would use an equivalent value such as 5x0.5. If one is using a Gaussian filter, then the radius usually should be about r=3*sigma. Otherwise if smaller, it gets truncated before it reaches near zero and loses effectiveness. Your value of 5x0.5 is way too large a radius and thus is very time wasteful. A value for r > about 1.5, in this case would probably not be noticeably different than 0x0.5 (where r=0 means have IM automatically compute the radius, which is something like r=2*sigma+1).

Note r=2*sigma covers 95% and r=3*sigma covers 99.7% of the area of the Gaussian distribution. see http://en.wikipedia.org/wiki/Normal_distribution
Last edited by fmw42 on 2013-07-28T21:52:05-07:00, edited 3 times in total.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: unsharp parameters: GIMP -> IM

Post by anthony »

As I said that that code has been replaced (It also came from a much older forum topic!)

As a side note. The Gaussian Resize Filter (default gaussian sigma = 0.5) uses a 'Support Window Radius' of 2.0
Which is 4 times the sigma. That is more for 'ease of use' however. Very old versions of IM used a radius of 1.25 which caused sharp 'ringing' effects in it convolutions, and then 1.5 or 3*radius whcih had practically un-noticable effects.

You can see a graph of a 1.25 support radius (2.5 times sigma) in...
http://www.imagemagick.org/Usage/filter/#support
It is a noticeable cut-off, and did produce noticeable ringing effects.

Now resize filters differ from 'normal' convolution in that the 'lookups' are not grid aligned, whcih is why a mathematical function, or large (1000+ entry) lookup tables, are used for resize and image distortions. Convolution (like unsharp) is grid aligned, so a 'kernel' or array of sampling weights, is pre-calculated. But the same cut-off (kernel radius), factors still applies. Users either specify that radius, or let IM calculate a radius that is limited by a 'negligible integer effect' for that radius.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: unsharp parameters: GIMP -> IM

Post by fmw42 »

Greenkoopa wrote:I find it strange that GIMP calls a parameter radius when pixels 2*(radius+1) distant are changed.
I suspect that you/GIMP are using radius to mean different things. I suspect that should really be 2*(sigma+1).

My comment above that IM uses something like 2*sigma+1 may be 2*(sigma+1). I do not know, one would need to check the IM code to verify.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: unsharp parameters: GIMP -> IM

Post by anthony »

fmw42 wrote:My comment above that IM uses something like 2*sigma+1 may be 2*(sigma+1). I do not know, one would need to check the IM code to verify.

IM calculates, maximum radius that will provide at least a single quantum effect for each convolution element. That is that every element has at least a gaussian_weighting_ from_sigma * QuantumRange_for_integer >= 1.0

This is not a linear function due to the dependence of sigma, but is typically larger than 3

You can check it using the 'show kernel' define in morphology.
for 2-pass 1-D blurs, on a IM Q16 using a sigma of 5 we get...

Code: Select all

convert null: -define showkernel=1 -morphology Convolve Blur:0x5 null:

Code: Select all

Kernel "Blur" of size 41x1+20+0 with values from 0 to 0.0796737
Forming a output range from 0 to 1 (Normalized)
...(kernel values)...
Or a kernel size of 41, which means the radius used was (41-1)/2 => 20 or 4 times the sigma
(actually you could just look at 'origin offset' into the kernel ;) )

For a sigma of 10 I get a radius of 38 (a little less than 4 times)
and sigma 100 => radius 333

I did say this was not a linear calculation, which is why it is calculated using a very fast 'trial and error' function.

Note this would be much smaller for a IM Q8, and much larger for a Q32 or Q64.
For HDRI it would be the same as Q16.


For 2-D kernels (whcih may be what unsharp uses?) the calculated radius is a bit different (slight formula difference)

Code: Select all

convert null: -define showkernel=1 -morphology Convolve Gaussian:0x5 null:

Code: Select all

Kernel "Gaussian" of size 35x35+17+17 with values from 0 to 0.006372
....
So a sigma of 5 => radius of 17 or just over 3 times sigma
sigma of 10 => radius of 30
sigma of 100 => radius is 112


So using a radius = sigma*3 is selling yourself a little short in terms of accuracy, but it does make processing faster and is not really noticeable except by looking at actual numbers around a black-white boundary.

using radius = sigma*2 would probably be noticeable in flicker compares, but is still not really a major difference. It is however enormously faster to process, especially with 2-D convolutions which I believe unsharp uses. I don't think the convolution is separatable (1-d 2-pass) unless you just blur and do the subtraction separate to the convolution.

I do not remember how IM unsharp does it.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: unsharp parameters: GIMP -> IM

Post by fmw42 »

Thanks for the good information about what IM does. Just for curiosity I tried using sigma=0.5

Code: Select all

convert null: -define showkernel=1 -morphology Convolve Blur:0x0.5 null:
Kernel "Blur" of size 5x1+2+0 with values from 0 to 0.691892
Thus (5-1)/2=2 for the radius.

And for 2D

Code: Select all

convert null: -define showkernel=1 -morphology Convolve Gaussian:0x0.5 null:
Kernel "Gaussian" of size 5x5+2+2 with values from 0 to 0.618694
Thus again (5-1)/2=2 for the radius.

So both give 4*sigma, for this case.

So from Anthony's analysis and mine, it looks like for 2D (which I believe is what -unsharp uses) for small sigma (around 1), the radius is about 4*sigma. For moderate sigma around 5-10, radius is about 3*sigma. For very large sigma, radius is closer to 1*sigma. However, the latter surprises me as that means that it does not roll off to near zero, so it won't be as effective as it it could be for larger ratio. But I guess by that size it is getting impractically slow anyway.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: unsharp parameters: GIMP -> IM

Post by anthony »

fmw42 wrote:So from Anthony's analysis and mine, it looks like for 2D (which I believe is what -unsharp uses) for small sigma (around 1), the radius is about 4*sigma. For moderate sigma around 5-10, radius is about 3*sigma. For very large sigma, radius is closer to 1*sigma.
Note he reason the radius gets smaller for large sigma is actullay because you have more and more elements that the weighting is to be distributed across. So the weighting gets smaller, That means the quantum effects from a weighting (even though calculations are done using floating point) gets more pronounced, and in the end you have diminishing returns on larger and larger radii. Basically very complex non-linear effects.

However enough of this... getting back on topic.

IM unsharp does not need a radius, just the sigma, gain, and threshold, and what GIMP calls radius is really related (somehow) to sigma.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: unsharp parameters: GIMP -> IM

Post by GreenKoopa »

GreenKoopa wrote: I did some testing for GIMP Radius = 0.5 - 6 and Amount = 1 - 6. I have determined that GIMP Amount is exactly the same parameter as IM Gain. IM Sigma is GIMP Radius plus not quite one, and IM radius is twice that. I find it strange that GIMP calls a parameter radius when pixels 2*(radius+1) distant are changed. I also noticed that GIMP has unnecessarily huge rounding errors (it's 8-bit) for large Amounts/Gains.
anthony wrote: However how did you arrive at this?
was it.. gimp_radius = 5, ==> IM_sigma = gimp_radius+1 ==> IM kernal radius = IM_sigma*2
Seemed to be. I created an image that was one edge, with the left half dark and the right half light. I sharpened this test image using GIMP. I then took the middle row and graphed the pixel values. It is easy to see the radius, simply count how far out pixels changed. With the IM radius set, I varied sigma until the graphs matched. It was sigma = GIMP Radius + almost 1, which was half the previously noted radius. I ran tests through the ranges noted above.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: unsharp parameters: GIMP -> IM

Post by anthony »

GreenKoopa wrote: Seemed to be. I created an image that was one edge, with the left half dark and the right half light. I sharpened this test image using GIMP. I then took the middle row and graphed the pixel values. It is easy to see the radius, simply count how far out pixels changed. With the IM radius set, I varied sigma until the graphs matched. It was sigma = GIMP Radius + almost 1, which was half the previously noted radius. I ran tests through the ranges noted above.
Fair enough method...

I wonder however if Gimp is using a 2-pass blur and subtraction method instead of a all-in-one convolve.

It would make some sense as a threshold option needs a comparison with original image to determine if pixel is to change or not. That is whether this is a edge to sharpen or just a 'wrinkle' that you don't want to sharpen.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply