Fred's ImageMagick Scripts



    Licensing:

    Copyright © Fred Weinhaus

    My scripts are available free of charge for non-commercial (non-profit) 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

    Please read the Pointers For Use on my home page to properly install and customize my scripts.

LAPLACIAN


Applies a laplacian filter to an image.

Download Script

last modified: December 15, 2018



USAGE: laplacian [-f filter] [t thresh] [-m mix] infile outfile
USAGE: laplacian [-h or -help]

-f ...... filter ... filter=1 to 5; default=1
-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=50
-h ................. get help information
-help .............. get help information

PURPOSE: To apply a Laplacian filter to an image.

DESCRIPTION: LAPLACIAN generates an output image which is a user defined mix or blend of the original image and a Laplacian convolution filtered version of the image. This is achieved by forming a single convolution kernel whose weights depend upon a mixing of the Laplacian coefficients and the identity kernel. The script applies one of three different 3x3 Laplacian filters to an image to extract or enhance the edges in the image. It is a type of high pass filter which is constructed from the sum of the x and y second derivatives.

The basic blended high pass filtering formula is F = (1-m)*I + m*L, where I is the original image, L is the Laplacian 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 Laplacian filtered image. For intermediate value of m, we get a blend of the image and the Laplacian high pass filtered image. Now, we can consider both I and L as a convolution of some kernel with the original image, namely I = i x I and L = l 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, l is one of the three Laplacian 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*l. 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. Also note, that the filters are actually the negatives of the true laplacian filters so that when mixed with the image they produce sharpening rather than blurring. For example, applying Laplacian 3 is equivalent the identity kernel minus the 3x3 average convolution kernel.

ARGUMENTS:

-f filter is the form of the filter. Three different 3x3 filters, one 5x5 filter and one 7x7 filters are provided in order of the increasing edge strength they produce. Values for filter are 1 to 5. The default is filter=1.

Laplacian 1
0 -1 0
-1 4 -1
0 -1 0

Laplacian 2
-2 1 -2
1 4 1
-2 1 -2

Laplacian 3
-1 -1 -1
-1 8 -1
-1 -1 -1

Laplacian 4
-4 -1 0 -1 -4
-1 2 3 2 -1
0 3 4 3 0
-1 2 3 2 -1
-4 -1 0 -1 -4

Laplacian 5
-10 -5 -2 -1 -2 -5 -10
-5 0 3 4 3 0 -5
-2 3 6 7 6 3 -2
-1 4 7 8 7 4 -1
-2 3 6 7 6 3 -2
-5 0 3 4 3 0 -5
-10 -5 -2 -1 -2 -5 -10

-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


Laplacian Of A True Color Image

Original Image

Laplacian 1

Laplacian 2

Laplacian 3

Laplacian 4

Laplacian 5

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

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

2D Kernel
-2 1 -2
1 4 1
-2 1 -2

2D Kernel
-1 -1 -1
-1 8 -1
-1 -1 -1

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

2D Kernel
-10 -5 -2 -1 -2 -5 -10
-5 0 3 4 3 0 -5
-2 3 6 7 6 3 -2
-1 4 7 8 7 4 -1
-2 3 6 7 6 3 -2
-5 0 3 4 3 0 -5
-10 -5 -2 -1 -2 -5 -10



Laplacian Of A Grayscale Image

Original Image

Laplacian 1

Laplacian 2

Laplacian 3

Laplacian 4

Laplacian 5

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

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

2D Kernel
-2 1 -2
1 4 1
-2 1 -2

2D Kernel
-1 -1 -1
-1 8 -1
-1 -1 -1

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

2D Kernel
-10 -5 -2 -1 -2 -5 -10
-5 0 3 4 3 0 -5
-2 3 6 7 6 3 -2
-1 4 7 8 7 4 -1
-2 3 6 7 6 3 -2
-5 0 3 4 3 0 -5
-10 -5 -2 -1 -2 -5 -10



Laplacian Of A Binary Image

Original Image

Laplacian 1

Laplacian 2

Laplacian 3

Laplacian 4

Laplacian 5

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

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

2D Kernel
-2 1 -2
1 4 1
-2 1 -2

2D Kernel
-1 -1 -1
-1 8 -1
-1 -1 -1

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

2D Kernel
-10 -5 -2 -1 -2 -5 -10
-5 0 3 4 3 0 -5
-2 3 6 7 6 3 -2
-1 4 7 8 7 4 -1
-2 3 6 7 6 3 -2
-5 0 3 4 3 0 -5
-10 -5 -2 -1 -2 -5 -10



Laplacian Blending

Original Image

Arguments:
-f 1 -m 25

Arguments:
-f 1 -m 50

Arguments:
-f 1 -m 70

Original Image

Arguments:
-f 3 -m 10

Arguments:
-f 3 -m 25

Arguments:
-f 3 -m 50



Laplacian Thresholding

Original Image

Arguments:
-f 3 -t 70

Arguments:
-f 3 -t 40



What the script does is as follows:

  • Computes a convolution filter to blend the image with either one
    of 3 laplacian 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 Laplacian 3 filter:

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