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.

QUADCORNERS2


Finds the four corners of a binary quadrilateral by finding maximum curvature on the convex hull outline.

Download Script

last modified: December 18, 2023



USAGE: quadcorners2 [-L lower] [-U upper] [-a area] [-o openM] [-c closeM] [-b blursize] [-f filtersize] [-s skip] [-i images] [-k kind] [-C color] [-r radius] [-t textoffset] infile
USAGE: quadcorners2 [-help]

-p ... pad .......... border pad amount for input image to help in getting
..................... good Hough lines; integer>=0; default=0 (no padding)
-L ... lower ........ lower color threshold for extracting quadrilateral; comma
..................... separated triplet in the form of either 8-bit grayscale or
..................... percent color values (for srgb color); if only 1 value provided,
..................... it will be duplicated three times; default=50% (mid gray)
-U ... upper ........ upper color threshold for extracting quadrilateral; comma
..................... separated triplet in the form of either 8-bit grayscale or
..................... percent color values (for srgb color); if only 1 value provided,
..................... it will be duplicated three times; default=100% (white)
-a ... area ......... area threshold in pixels for removing small regions from the
..................... thresholded image; integer>=0; default=0 (skip connected
..................... components filtering)
-o ... openM ........ morphology open diamond size for preprocessing to remove all
..................... white regions outside the main quadrilateral in the threshold
..................... image; integer>0; default is no morphology open
-c ... closeM ....... morphology close disk size for preprocessing to remove all
..................... black regions inside the main white quadrilateral in the
..................... threshold image; integer>0; default is no morphology close
-b ... blursize ..... size of 1D blur (smoothing) filter (pixels); integer>0; default=5
-f ... filtersize ... size of 1D derivative filter (pixels); integer>0; default=5
-s ... skip ......... skip (in pixels) as the minimum distance between peaks
..................... in the polar image; integer>0; default=50 (must be larger
..................... than the filtersize)
-i ... images ....... keep ancillary processing images (especially for debugging);
..................... choices are: view (v) or save (s); default is neither
-k ... kind ......... kind of ancillary processing images; choices are: threshold (t),
..................... cleaned (c), (convex) hull (h), (Hough) lines (l),
..................... final (corners) (f) or all (a)
-C ... color ........ color used when drawing on various images; any IM color value
..................... is allowed; default=red
-r ... radius ....... radius for circle used to mark corners; integer>0; default=2
-t ... textoffset ... text offset of TL, TR, BR, BL annotation relative to corner;
..................... +X+Y format, integers for X and Y; default="+0-15"

PURPOSE: To optionally threshold an image if not already binary and find the four corners of the binary quadrilateral from the maximum curvature of the outline obtained from the convex hull.

DESCRIPTION: QUADCORNERS2 optionally thresholds an image, if not already binary, and finds the four corners of a quadrilateral in the binary image. The process is 1) optionally color threshold the image; 2) optionally apply morphology to clean and smooth the outline of the image; 3) optionally apply connected components processing to remove small regions; 4) get a white filled convex hull on black background image 5) convert the convex hull image to polar coordinates and scale to 1 row; 6) extract the reverse polar coefficients; 7) convert the 1D polar image heights to 1D x and y images using the reverse coefficents; 8) apply 1D first and second order derivative to the 1D x and y images; 9) use the x and y derivatives to compute a 1D curvature image; 10) find the x locations in the 1D curvature image where the curvature is maximum skipping enough between peaks; 11) look up the x and y coordinates associated with peaks locations; 12) sort the top 4 peak x,y coordinates in TL, TR, BR, BL order (i.e., clockwise from TL)

The input image should be a binary quadrilateral. If not, then use the color-threshold, morphology and area to threshold into a clean binary quadrilateral.

Any saved ancillary images will be name for the input_X.png, where X is the kind of ancillary image.

The 4 corner X,Y coordinates of the quadrilateral (in the padded input) will be listed to the terminal in clockwise order starting at the top-left corner

ARGUMENTS:

-p pad ... PAD is the border pad amount for input image to help in getting good Hough lines; integer>=0; default=0 (no padding)

-L lower ... LOWER color threshold for extracting quadrilateral; comma separated triplet in the form of either 8-bit grayscale or percent color values (for srgb color); if only 1 value provided, it will be duplicated three times; default=50% (mid-gray)

-U upper ... UPPER color threshold for extracting quadrilateral; comma separated triplet in the form of either 8-bit grayscale or percent color values (for srgb color); if only 1 value provided, it will be duplicated three times; default=100% (white)

-a area ... AREA threshold in pixels for removing small regions from the thresholded image; integer>=0; default=0 (skip connected components filtering)

-o openM ... OPENM is the morphology open diamond size for preprocessing to remove all white regions outside the main quadrilateral in the threshold image; integer>0; default is no morphology open

-c closeM ... CLOSEM is the morphology close disk size for preprocessing to remove all black regions inside the main white quadrilateral in the threshold image; integer>0; default is no morphology close

-b blursize ... BLURSIZE is the (half) size of the 1D blur smoothing filter (in pixels); integer>0; default=5

-f filtersize ... FILTERSIZE is the (half) size of the 1D derivative filters (in pixels); integer>0; default=5

-s skip ... SKIP (in pixels) is the minimum distance between peaks in the polar image; integer>0; default=50 (must be larger than the filtersize)

-i images ... IMAGES specifies whether to keep ancillary processing images (especially for debugging); choices are: view (v) or save (s); default is neither

-k kind ... KIND of ancillary processing images; choices are: threshold (t), cleaned (c), (convex) hull (h), polar (p), final (corners) (f), or all (a)

-C color ... COLOR used when drawing on various images; any IM color value is allowed; default=red

-r radius ... RADIUS for circle used to mark corner points; integer>0; default=2

-t textoffset ... text offset of TL, TR, BR, BL annotation relative to corner points; +X+Y format, integers for X and Y; default="+0-15"

REQUIREMENTS: Requires IM 7 due to the use of -color-threshold and the convex hull

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: Binary Rectangle Input

Original Image

arguments:
-i save -k final



Example 2: Binary Quadrilateral Input

Original Image

arguments:
-p 50 -i save -k final



Example 3: Quadrilateral in Non-Binary Image

Original Image

arguments:
-L 75% -i save -k all

Thresholded Image

Cleaned Image

Convex Hull Image:

Polar Image

Final Corners Image



What the script does is as follows:

  • Optionally thresholds the image
  • Optionally morphology cleans the image
  • Optionally removes small areas with connected components processing
  • Gets the convex hull
  • Converts to polar coordinates
  • Scales the polar coordinate image to 1 row
  • Computes the X and Y first and second derivatives of the 1D polar image using finite differences
  • Computes the curvature from the X and Y derivatives
  • Finds the 4 largest and properly spaced maxima in the curvature image
  • Converts those 4 polar coordinates back to Cartesian coordinates
  • Optionally outputs various images including one marking the corners
  • Prints the intersection x,y coordinates TL, TR, BR and BL