Fred's ImageMagick Scripts



    Licensing:

    Copyright © Fred Weinhaus

    My scripts are available free of charge for non-commercial use, ONLY.

    For use of my scripts in commercial (for-profit) environments or non-free applications, please contact me (Fred Weinhaus) for licensing arrangements. My email address is fmw at alink dot net.

    If you: 1) redistribute, 2) incorporate any of these scripts into other free applications or 3) reprogram them in another scripting language, then you must contact me for permission, especially if the result might be used in a commercial or for-profit environment.

    Usage, whether stated or not in the script, is restricted to the above licensing arrangements. It is also subject, in a subordinate manner, to the ImageMagick license, which can be found at: http://www.imagemagick.org/script/license.php

DERIVATIVE


Applies a first directional derivative filter to an image.

Download Script

last modified: September 26, 2017



USAGE: derivative [-w width] [-a angle] [t thresh] [-m mix] infile outfile
USAGE: derivative [-h or -help]

-w ...... width .... width of filter; width=3, 5, 7; default=3
-a ...... angle .... angle=0 to 360 (clockwise from x axis);
.................... default=0
-t ...... thresh ... threshold percent for binarization;
.................... thresh=integer 0 to 100; default="" (none)
-m ...... mix ...... mixing percent with original image;
.................... mix=integer 0 to 100; default=100
-h ................. get help information
-help .............. get help information

PURPOSE: To apply a first directional derivative filter to an image.

DESCRIPTION: DERIVATIVE generates an output image which is a user defined mix or blend of the original image and the first directional derivative convolution of the image. The directional derivative is generated by combining the individual x and y component derivative kernels at the desired angle. Then the resulting directional derivative kernel is optionally mixed with the the identity kernel representing the original image.

The basic blended high pass filtering formula is F = (1-m)*I + m*D, where I is the original image, D is the directional derivative high pass filtered image and m = mix/100. When m=0, we get only the original image and when m=1, we get only the high pass derivative filtered image. For intermediate value of m, we get a blend of the image and the derivative high pass filtered image. Now, we can consider both I and D as a convolution of some kernel with the original image, namely I = i x I and D = d x I, where x means convolution. Note that a convolution is simply a weighted average of all the pixels in some neighborhood of a give pixel. Usually an odd sized neighborhood, such as 3x3, etc is used to prevent having the resulting image be shifted a fractional pixel. The convolution kernel values are simply the weights for the average. So here, i is the identity kernel, which is all zeroes, except the center of the kernel which has a value of 1. Similarly, d is one of the directional derivative kernels. They are different forms of a high pass filter. Thus we can consider the final filtered image, F = f x I, where f = (1-m)*i + m*d. Consequently, we only have to do one convolution using the convolution kernel, f. Note, that all pure high pass filter convolution kernels will have weights that sum to 0.

ARGUMENTS:

-w width determines the size and form of the filter. Three different size filters are allowed. So width=3, 5 or 7. The default is width=3.

width=3
DX
-1 0 1
-1 0 1
-1 0 1

DY
1 1 1
0 0 0
-1 -1 -1


width=5
DX
-2 -1 0 1 2
-2 -1 0 1 2
-2 -1 0 1 2
-2 -1 0 1 2
-2 -1 0 1 2

DY
2 2 2 2 2
1 1 1 1 1
0 0 0 0 0
-1 -1 -1 -1 -1
-2 -2 -2 -2 -2


width=7
DX
-3 -2 -1 0 1 2 3
-3 -2 -1 0 1 2 3
-3 -2 -1 0 1 2 3
-3 -2 -1 0 1 2 3
-3 -2 -1 0 1 2 3
-3 -2 -1 0 1 2 3
-3 -2 -1 0 1 2 3

DY
3 3 3 3 3 3 3
2 2 2 2 2 2 2
1 1 1 1 1 1 1
0 0 0 0 0 0 0
-1 -1 -1 -1 -1 -1 -1
-2 -2 -2 -2 -2 -2 -2
-3 -3 -3 -3 -3 -3 -3

-a angle is the orientation of the directional derivative. Values are integers ranging from 0 to 360 degrees clockwise from the x axis. Results for values of 0 and 360 will be identical. The default=0.

-t thresh is the thresholding percentage used to create a binary Laplacian edge image. Values range from 0 to 100. A higher value will result in fewer edges in the resulting image.

-m mix is the percentage mixing factor used to blend the Laplacian with the original image. A value of mix=0, results in the original image. A value of mix=100 results in a pure Laplacian filtered image.

CAVEAT: No guarantee that this script will work on all platforms, nor that trapping of inconsistent parameters is complete and foolproof. Use At Your Own Risk.


EXAMPLES


Directional Derivatives Of A Binary Image By Width

Original Image

Arguments:
-w 3 -a 0

Arguments:
-w 5 -a 0

Arguments:
-w 7 -a 0

2D Kernel
0 0 0
0 1 0
0 0 0

2D Kernel
-1 0 1
-1 0 1
-1 0 1

2D Kernel
-2 -1 0 1 2
-2 -1 0 1 2
-2 -1 0 1 2
-2 -1 0 1 2
-2 -1 0 1 2

2D Kernel
-3 -2 -1 0 1 2 3
-3 -2 -1 0 1 2 3
-3 -2 -1 0 1 2 3
-3 -2 -1 0 1 2 3
-3 -2 -1 0 1 2 3
-3 -2 -1 0 1 2 3
-3 -2 -1 0 1 2 3



Directional Derivatives Of A Binary Image By Angle

Arguments:
-w 3 -a 0

Arguments:
-w 3 -a 45

Arguments:
-w 3 -a 90

2D Kernel
-1 0 1
-1 0 1
-1 0 1

2D Kernel
-1.414 -.707 0
-.707 0 .707
0 .707 1.414

2D Kernel
-1 -1 -1
0 0 0
1 1 1

Arguments:
-w 3 -a 135

Arguments:
-w 3 -a 180

Arguments:
-w 3 -a 225

2D Kernel
0 -.707 -1.414
.707 0 -.707
1.414 .707 0

2D Kernel
1 0 -1
1 0 -1
1 0 -1

2D Kernel
1.414 .707 0
.707 0 -.707
0 -.707 -1.414

Arguments:
-w 3 -a 270

Arguments:
-w 3 -a 315

Arguments:
-w 3 -a 360

2D Kernel
1 1 1
0 0 0
-1 -1 -1

2D Kernel
0 .707 1.414
-.707 0 .707
-1.414 -.707 0

2D Kernel
-1 0 1
-1 0 1
-1 0 1



What the script does is as follows:

  • Computes a convolution filter to blend the image with a first
    directional derivative high pass filtered version of the image.
  • Performs a convolution of the filter kernel with the image.

This is equivalent to the following IM commands for
the case of the 3x3 directional derivative at angle=0 filter:

  • kern1D="-1,0,1,-1,0,1,-1,0,1"
  • convert $infile -convolve "$kern1D" $outfile