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

NOTCH


Creates and applies a notch filter to an image in the frequency domain to remove dither patterns and other regular noise patterns.

Download Script

last modified: September 26, 2017



USAGE: notch [-r rad] [-t thresh] [-c centrad] [-h horizontal] [-v vertical] [-o outer] [-L left] [-R right] [-U upper] [-B bottom] [-m median] [-d dilate] [-s smooth] [-S] [-K] infile outfile
USAGE: notch [-H or -help]

-r .... rad ............. blurring radius for generating mask from spectrum;
......................... integer>0; default=7
-t .... thresh .......... binarization threshold in percent; 0<=integer<=100;
......................... default=automatic entropy threshold
-c .... centrad ......... circular center mask radius; integer>0; default=2
-h .... horizontal ...... thickness of horizontal center line mask area;
......................... integer>=0; default=0 (none)
-v .... vertical ........ thickness of vertical center line mask area;
......................... integer>=0; default=0 (none)
-o .... outer ........... distance from center sides to create an outer
......................... circular mask; integer>0; default=2
-L .... left ............ thickness of left side mask area;
......................... integer>=0; default=0 (none)
-R .... right ........... thickness of right side mask area;
......................... integer>=0; default=0 (none)
-U .... upper ........... thickness of upper (top) side mask area;
......................... integer>=0; default=0 (none)
-B .... bottom .......... thickness of bottom side mask area;
......................... integer>=0; default=0 (none)
-m .... median .......... radius of median filtering; integer>=0;
......................... default=0 (no median filtering)
-d .... dilate .......... dilation radius; integer>0; default=1
-s .... smooth .......... smoothing distance across black-white transitions;
......................... integer>=0; default=1
-S ...................... show (display) mask image; yes or no; default=0
-K ...................... keep (save) mask image to disk; yes or no; default=no

PURPOSE: To create and apply a notch filter to an image in the frequency domain to remove dither patterns and other regular noise patterns.

DESCRIPTION: NOTCH creates a notch filter image from the frequency domain spectrum of an image and then uses it as a mask image in the frequency domain to remove dither patterns and other regular noise patterns. This avoids manually drawing the mask from the spectrum, but may not do quite as good a job. Certain parameters must be estimated from viewing the spectrum image. The key ones include: 1) a binarization threshold (usually found automatically), 2) a radius near the center of the image to preserve low frequency components, 3) the thickness of a horizontal and/or a vertical linear mask from edge to edge crossing the center of the image, again to preseve low frequencies in one or the other (x or y) direction.

The spectrum image is the basis of the notch filter. It will show distinct bright "star-like" patterns and short to medium repeated line patterns that represent some kind of dither or regular noise patterns in the original image. We want to eliminate these artifacts without suppressing any frequency domain signals, which also will be brightest at the very center and spread out around it. Thus we want an ultimate mask that is white everywhere and black where these star-like and linear patterns appear in the spectrum. So we will do some processing to try to extract these bright patterns in the spectrum and then mask out the areas of signal that we want to preserve and then invert or negate the result to form the final mask.

The order of processing is as follows:

First use the spectrum script to generate a spectrum image and view it to estimate the key parameters. Do this before running the notch script.

Then run the notch script which will do the following:

  1. Generates a grayscale spectrum from the fourier transform of the image.
  2. Blurs the spectrum using the rad parameter and subtracts the result from the original spectrum.
  3. Thresholds the result of step two. This will form the basic filter, which will still show bright star and linear patterns along with some extraneous noise on a black background. Note that this is opposite of what we want and the last step will invert the mask.
  4. Applies a small cirular mask using the centrad parameter to preserve the area near the center of the spectrum so that low frequencies are maintained. The size of the circular mask can be from 2 pixels minimum to as large a value possible that does not reach the closest bright star or linear pattern.
  5. Optionally applies a linear mask along the center row and/or column of a specified thickness given by the horizontal and vertical parameters, but only if one sees bright but somewhat fuzzy regions that extend from one side of the image to the other and passing through the center. If they are uniformly bright and of constant thickness and do not reach the center, then do not mask them as they are part of the noise that we want to remove.
  6. Optionally applies similar linear masks for the outer edges of the spectrum if one sees bright areas extending along the sides. The thichknesses of these linear masks are given by the left, right, upper and bottom parameters. This is not usually necessary, but can be done to try to improve the result.
  7. Optionally applies a circular mask in the outer regions of the spectrum, to preserve the high frequency components. Again this is usually not needed, but can be done to try to improve the result. The radius is given by the outer parameter subtracted from the half-width of the spectrum image. The outer parameter can be simply estimated as the pixel distance inward from the center of one side of the image before it reaches the outermost star or linear pattern associated with the dither that we want to remove.
  8. Optionally applies a median filter to remove some of the spurious tiny bright dots that are not part of the star or linear dither patterns. If the star and linear patterns are too small or thin, then this is not a good idea as it will remove the patterns that we are trying to identify as being associated with the dither.
  9. Dilates the bright patterns that remain typically by one pixel radius to make them a little bigger.
  10. Smoothes (ramps) the transition from white to black in the mask so that ringing artifacts are not introduced in the resulting filtered image. A one pixel smoothing transition is usually sufficient.
  11. Invert black and white so that the areas that are associated with the dither pattern (the bright-stars and linear patterns) become black and the rest becomes white (apart from some spurious remaining black noise that was not removed).

ARGUMENTS:

-r ... RAD is the radius of the blur used to compute the difference image (image-blurredimage) from the spectrum image. This difference image will then be thresholded. Values are integers>0. The default=7, which seems to be rather robust.

-t ... THRESH is the value in percent used to threshold the difference image. Values are integers between 0 and 100. The default is to compute the threshold automatically using an entropy thresholding technique.

-c ... CENTRAD is the radius of a circular mask applied at the center of thresholded image, which is used to preserve the low frequency signal content in the spectrum. Values are integers>0. The default=2, but can made larger to preserve higher frequencies. But the radius should not be so large as to cover any of the star-like or linear patterns that appear in the spectrum due to the dither-like patterns that we want to remove.

-h ... HORIZONTAL is the thickness of a rectangular mask extending across the spectrum along the horizontal center line. This is used to preserve signal content along this center line, if one sees a narrow bright fuzzy area that extends completely accross this center line. If one sees a bright, constant intensity area of uniform thickness that does not cross the center, then that is likely part of the dither-like pattern that we want to remove and so should not be masked. Values are positive integers. The default is 0 to indicate no masking along this region.

-v ... VERTICAL is the thickness of a rectangular mask extending across the spectrum along the vertical center line. This is used to preserve signal content along this center line, if one sees a narrow bright fuzzy area that extends completely accross this center line. If one sees a bright, constant intensity area of uniform thickness that does not cross the center, then that is likely part of the dither-like pattern that we want to remove and so should not be masked. Values are positive integers. The default is 0 to indicate no masking along this region.

-o ... OUTER is a parameter that determines an outer circular mask use to preserve the high frequency signal content in the outer regions of the spectrum. The radius of the circular mask is given by the half-width of the spectrum image minus the outer parameter. The outer parameter can be estimated simply by the pixel distance from the center of any side of the spectrum inward such that the circular mask will not cover any star-like or linear patterns due to the dither pattern, but may cover any other extraneous bright areas along the outer sides or corners of the spectrum. Values are integers>=0. The default=0 indicating no masking.

-L ... LEFT is the thickness of a rectangular mask extending across the left side of the spectrum, which may be used to cover any extraneous bright areas along the left side that are not inclusive of any star-like or strong linear patterns associated with the dither. This may be used to preserve the high frequency components of the image signal in this region. Values are positive integers. The default is 0 to indicate no masking along this region.

-R ... RIGHT is the thickness of a rectangular mask extending across the right side of the spectrum, which may be used to cover any extraneous bright areas along the right side that are not inclusive of any star-like or strong linear patterns associated with the dither. This may be used to preserve the high frequency components of the image signal in this region. Values are positive integers. The default is 0 to indicate no masking along this region.

-U ... UPPER is the thickness of a rectangular mask extending across the upper (top) side of the spectrum, which may be used to cover any extraneous bright areas along the right side that are not inclusive of any star-like or strong linear patterns associated with the dither. This may be used to preserve the high frequency components of the image signal in this region. Values are positive integers. The default is 0 to indicate no masking along this region.

-B ... BOTTOM is the thickness of a rectangular mask extending across the bottom side of the spectrum, which may be used to cover any extraneous bright areas along the right side that are not inclusive of any star-like or strong linear patterns associated with the dither. This may be used to preserve the high frequency components of the image signal in this region. Values are positive integers. The default is 0 to indicate no masking along this region.

-m ... MEDIAN is the amount or radius of a median filter applied to try to remove extraneous bright specks that are associated with the signal and not bright star-like patterns associate with the dither pattern. Values are integers>=0. The default=0 (no median filtering). If the dither star and linear patterns are too small or thin, then it is not a good idea to apply the median filtering as it will remove these star-like and linear patterns as well.

-d ... DILATE is the radius of the dilation that is used to make the star-like and linear patterns larger and stronger. Values are integers>=0. The default=1

-s ... SMOOTH is the smoothing distance to ramp the transition from white to black in the final mask. This is used to avoid introducing ringing artifacts in the recovered image. Values are integers>=0. The default=1.

-S ... Show (display) the mask image. Values are yes or no. The default=no. If yes, then the X11 display system must be enabled.

-k ... Keep (save) the mask image to disk. Values are yes or no. The default=no. If yes, then the resulting mask image will start with the same name used for the output (less the suffix) with '_mask.gif' appended.

REQUIREMENTS: IM IM 6.5.4-3 or higher due to the use -fft. Also requires the FFTW delegate library to compute the Fourier Transform. Q8 IM compilation is not generally recommended and may not carry enough precision for the Fourier Transform.

See http://www.fmwconcepts.com/imagemagick/fourier_transforms/fourier.html for more details about the Fourier Transform with ImageMagick.

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


Clown Image
(source)

Original Image

Spectrum

Argument:
-h 10 -v 5 -c 5

Generated Mask



Clown Image
(source)

Original Image

Spectrum

Argument:
(added median filter)
-h 10 -v 5 -c 5 -m 1

Generated Mask



Dunes Image

Original Image

Spectrum

Argument:
-h 7 -v 3 -c 20 -d 2

Generated Mask



Fingerprint Image
(need to use manual threshold rather than automatic)

Original Image

Spectrum

Argument:
-t 10 -c 5

Generated Mask



Medieval_Cut Image

Original Image

Spectrum

Argument:
-v 3 -c 10

Generated Mask



Twig Image
(source)

Original Image

Spectrum

Argument:
-c 10 -d 2

Generated Mask



What the script does is as follows for safecolor=none:

  • Generates a grayscale spectrum from the fourier transform of the image.
  • Blurs the spectrum using the rad parameter and subtracts the result
    from the original spectrum.
  • Thresholds the result of step two. This will form the basic filter,
    which will still show bright star and linear patterns along with some
    extraneous noise on a black background. Note that this is opposite of
    what we want and the last step will invert the mask.
  • Applies a small cirular mask using the centrad parameter to preserve
    the area near the center of the spectrum so that low frequencies are
    maintained. The size of the circular mask can be from 2 pixels minimum
    to as large a value possible that does not reach the closest bright
    star or linear pattern.
  • Optionally applies a linear mask along the center row and/or column
    of a specified thickness given by the horizontal and vertical parameters,
    but only if one sees bright but somewhat fuzzy regions that extend from
    one side of the image to the other and passing through the center. If they
    are uniformly bright and of constant thickness and do not reach the center,
    then do not mask them as they are part of the noise that we want to remove.
  • Optionally applies similar linear masks for the outer edges of the
    spectrum if one sees bright areas extending along the sides. The thichknesses
    of these linear masks are given by the left, right, upper and bottom parameters.
    This is not usually necessary, but can be done to try to improve the result.
  • Optionally applies a circular mask in the outer regions of the spectrum,
    to preserve the high frequency components. Again this is usually not needed,
    but can be done to try to improve the result. The radius is given by the
    outer parameter subtracted from the half-width of the spectrum image. The
    outer parameter can be simply estimated as the pixel distance inward from
    the center of one side of the image before it reaches the outermost star
    or linear pattern associated with the dither that we want to remove.
  • Optionally applies a median filter to remove some of the spurious tiny
    bright dots that are not part of the star or linear dither patterns. If the
    star and linear patterns are too small or thin, then this is not a good idea
    as it will remove the patterns that we are trying to identify as being
    associated with the dither.
  • Dilates the bright patterns that remain typically by one pixel radius
    to make them a little bigger.
  • Smoothes (ramps) the transition from white to black in the mask so that
    ringing artifacts are not introduced in the resulting filtered image. A one
    pixel smoothing transition is usually sufficient.
  • Invert black and white so that the areas that are associated with the
    dither pattern (the bright-stars and linear patterns) become black and the
    rest becomes white (apart from some spurious remaining black noise that was
    not removed).