Okay this has been an interesting discussion, (which I have not been able to join in due to the weekend and family commitments). It is the first such serious and deep discussion on the convolve operator on the forums, and as such is a major test of its abilities in ImageMagick.
First
-negate is a special type of
-level operator, Just swap the arguments to produce a negative result!
Now HDRI does require compilation, and is used for highly mathematical situations. Basically it avoids the effects of 'clipping' negative values or very large values, as well as 'rounding' effects of storing very small numbers in integers. Both of which can be demonstrated using -level on non-HDRI imagemagick.
All the above work has mostly been to avoid such effects from being produced by
-convolve .
So lets look at convolve. First it finds the 'average' value of any kernel supplied, and scales that kernel by that average. Then it adds any defined 'bias' to the result of each convolution. That is it will only normalize 'positive' kernels correctly!
The sobel kernel is
which when added up produces a average of zero, so no scaling is applied to the kernel. this is why dividing by 1/4 or doing something else works for this kernel, but not for other kernels. The largest positive result will be 4*white
We need that to be white.
However so as to avoid negatives you need a 50% bias, so really you want bias + maximum posible to be white. That is maximum posible must equal 50%!
Add bias and scale kernel (which as average = 0 does not automatically get scaled!) so 1+2+1 * scale => 0.5 OR divide by 8 (not 4!)
Code: Select all
-bias 50% -convolve '-0.125,0.0,0.125, -0.25,0.0,0.25, -0.125,0.0,0.125'
After this -solarize 50% will perform the equivelent of a abs() around the bias value
and a level
with negation will re-stretch the image. That is we want '50%' (the bias) to be black, and 0% (maximum distance from bias) to be white...
An alternative to this is of course to do a difference image against gray, and multiply by 2.
Code: Select all
\( +clone -fill gray50 -colorize 100% \) -compose difference -composite -evaluate multiply 2
OR use FX operator (slow)
So for non-HDRI you get the best most accurate result using the following...
Code: Select all
convert image.png \
-bias 50% -convolve '-0.125,0.0,0.125, -0.25,0.0,0.25, -0.125,0.0,0.125' \
-solarise 50% -level 50%,0% result.png
Of course anything that works for non-HDRI will work with HDRI as well, HDRI will just be a little more accurate due removing 'rounding' effects.
NOTE Their is proposals for the re-development of convolve as part of morphology
operators. the current 'auto scaling' of convolve currently gets in the way of morphology.
Question: convolve currently automatically scales!
But should convolve automatically scale and bias 'zero' kernels, such a sobel. That is should it adjust the kernel and bias so that minimum posible => black
and maximum posible => white?
Or should we remove all scale handling, and give this control back to the user?
as a scaling parameters (with posible automatic switches).
QUESTION; convolve does not understand the special nature of 'alpha' as a transparency mask. Should it be a posibility? Perhaps a 'channel' control flag?