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

CORNERS


Detects corner structures in an image.

Download Script

last modified: January 27, 2017



USAGE: corners [-m method] [-t threshold] [-s smooth] [-r region] [-c color] [-d dilate] [-p print] infile outfile1 [outfile2]
USAGE: corners [-h|-help]

-m ... method ...... method of corner detection; choices are: morphology (m),
.................... harris (h) or shi-tomasi (s); default=morphology
-t ... threshold ... threshold level (in percent) for choosing the strongest
.................... corners; 0<=float<=100; default=50
-s ... smooth ...... smoothing to apply noise suppression; 0<=integer<=5;
.................... number of applications of -enhance; default=0
-r ... region ...... region is either the morphology structure element size
.................... for morphology method or the sigma for the gaussian
.................... weighting of the neighborhood derivatives in the
.................... harris and shi-tomasi methods; float>=0; default=1
-c ... color ....... color of color points to be drawn on the first output
.................... image; any valid opaque IM color is allowed; default=red
-d ... dilate ...... dilation amount for enlarging the corner point drawn
.................... on the output image(s); integer>=0; default=1
-p ... print ....... print a list of the corner points to the terminal;
.................... yes or no; default=no

The first output image will show the corner points overlaid on the input image. The second output will show the corner points as white on a black background.

PURPOSE: To detect corner structures in an image.

DESCRIPTION: CORNERS detects corner structures in an image. Any of three methods may be used. The first is a morphology-based corner detector. The second is the Harris corner detector. The third is the Shi-Tomasi corner detector. The latter two require Imagemagick compiled in HDRI mode. The morphology corner detector uses two sets of morphology close operations using asymmetric structuring elements. The Harris and Shi-Tomasi corner detectors use a Gaussian weighted average of the squares and cross products of the X and Y Sobel directional derivatives. Note that the image is first converted to grayscale for the corner detection process.

ARGUMENTS:

-m method ... METHOD of corner detection. The choices are: morphology (m), harris (h) or shi-tomasi (s). The default=morphology. The harris and shi-tomasi methods require Imagemagick compile in HDRI mode.

-t threshold ... THRESHOLD level (in percent) for choosing the strongest corners. Values are 0<=floats<=100. The default=50. Different values are needed for each method. The optimal threshold will also depend upon and smoothing applied and upon the region size. More smoothing or larger regions permit lower thresholds.

-s smooth ... SMOOTH is the amount of smoothing to apply to the image as a preprocessing step to suppress noise. Values are 0<=integers<=5. The value represents the number of applications of -enhance to apply. The default=0.

-r region ... REGION is either the morphology structure element size for the morphology method or the sigma value for the gaussian weighting of the neighborhood of derivatives in the harris and shi-tomasi methods. Values are floats>=0. The default=1. Values for method=morphology are typically integers.

-c color ... COLOR of the corner points to be drawn on the first output image. Any valid opaque IM color is allowed. The default=red.

-d dilate ... DILATE is the amount of dilation for enlarging the corner points drawn on the output image(s). Values are integer>=0. The default=1.

-p print ... PRINT a list of the x,y corner points to the terminal. The choices are: yes (y) or no (n). The default=no.

REQUIREMENTS: The Harris and the Shi-Tomasi methods require that Imagemagick be compile in HDRI mode. All methods require IM 6.8.9.10 or higher due to the use of -connected-components.

REFERENCES Morphology:
http://www.site.uottawa.ca/~laganier/publications/coin.pdf

REFERENCES Harris:
http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_feature2d/py_features_harris/py_features_harris.html
http://www.math.harvard.edu/archive/21b_fall_04/exhibits/2dmatrices/

REFERENCES Shi-Tomasi:
http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_feature2d/py_shi_tomasi/py_shi_tomasi.html
http://www.math.harvard.edu/archive/21b_fall_04/exhibits/2dmatrices/

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


Example 1

Original Image
(source)

Arguments:
-m morphology -t 95 -d 3



Example 2

Original Image
(source)

Arguments:
-m morphology -t 95
-m harris -t 95
-m shi-tomasi -t 95

(animation)



Example 3

Original Image

Arguments:
-m morphology -t 2 -d 2
-m harris -t 1 -d 2
-m shi-tomasi -t 1 -d 2

(animation)



Example 4

Original Image
(source)

Arguments:
-m morphology -t 45
-m harris -t 45
-m shi-tomasi -t 45

(animation)



Example 5

Original Image

Arguments:
-m morphology -t 25 -r 3
-m harris -t 15 -r 1
-m shi-tomasi -t 5 -r 1

(animation)



What the script does for m=morphology is as follows:

  • Reads the input and converts to grayscale
  • Does two different asymmetric morphology closes each on the grayscale image
  • Does a compose difference between the two
  • Stretches to full dynamic range and thresholds
  • Uses connected-components to find each corner region and
    its centroid
  • Draws the points onto the input image and save the result to the output

  • convert -quiet -regard-warnings "$infile" +write $dir/tmpI.mpc \
    -alpha off -colorspace gray +repage $dir/tmpG.mpc
  • if [ "$threshold" != "" ]; then
    thresholding="-threshold $threshold%"
    else
    thresholding=""
    fi
  • if [ "$dilate" != "0" ]; then
    dilate=`convert xc: -format "%[fx:$dilate + 0.5]" info:`
    dilating="-morphology dilate disk:$dilate"
    else
    dilating=""
    fi
  • convert \
    \( $dir/tmpG.mpc -morphology dilate plus:$region -morphology erode diamond:$region \) \
    \( $dir/tmpG.mpc -morphology dilate cross:$region -morphology erode square:$region \) \
    -compose difference -composite -auto-level $thresholding $dilating \
    $dir/tmpC.miff
  • data=`convert $dir/tmpC.miff \
    -define connected-components:verbose=true \
    -connected-components 8 null: | tail -n +2 | \
    sed -n 's/^.*[:][ ]*[^ ]*[ ]*\([^ ]*\)[ ]*[^ ]*[ ]*\([^ ]*\)[ ]*$/\1_\2/p'`
  • i=1
    points=""
    for item in $data; do
    coords=`echo "$item" | cut -d_ -f1`
    colour=`echo "$item" | cut -d_ -f2`
    if [ "$colour" != "srgb(0,0,0)" -a "$colour" != "gray(0)" -a "$colour" != "black" ]; then
    [ "$print" = "yes" ] && echo "pt=$i coords=$coords"
    points="$points point $coords"
    i=$((i+1))
    fi
    done
  • convert $dir/tmpI.mpc \
    \( -clone 0 -fill "$color" -colorize 100 \) \
    \( -clone 0 -evaluate set 0 -fill white \
    -draw "$points" -alpha off $dilating \) \
    -compose over -composite "$outfile1"