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.

QUADCORNERS1


Finds the four corners of a binary quadrilateral using the intersection of Hough lines.

Download Script

last modified: December 18, 2023



USAGE: quadcorners1 [-p pad] [-L lower] [-U upper] [-a area] [-o openM] [-c closeM] [-h hthresh] [-m maxiter] [-i images] [-k kind] [-C color] [-r radius] [-t textoffset] infile

-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
-h ... hthresh ...... Hough line length threshold either in pixels or percentage of
..................... smallest input dimension; integer>=0; default=20%
-m ... maxiter ...... maximum stopping number of iterations; integer>0; default=0
..................... (no iterations)
-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 using the intersection of Hough lines from the outline of the convex hull.

DESCRIPTION: QUADCORNERS optionally thresholds an image, if not already binary, and finds the four corners of a quadrilateral in the binary image. The process is 1) pad the input with black; 2) optionally color threshold the image, 3) optionally apply morphology to clean and smooth the outline of the image; 3) optionally apply connected components processing to remove small regions; 4) get the convex hull of the binary image; 5) get 4 Hough lines from the convex hull; 6) sort the lines so that they are listed in clockwise order with the top line first; 7) get the intersections of successive lines in pairs; 8) get the 4 corner x,y coordinates

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

The hthresh and pad arguments are the critical ones for getting only 4 Hough lines. Unfortunately, they are rather sensitive. If too many lines, add padding and/or increase the hthresh. If too few lines, remove padding and/or decrease the hthresh.

Alternately, set the maximum number of iterations larger than 0 to try to converge to 4 lines. The hthresh parameter will be used to set the initial starting number of lines when iterating and will be adjusted accordingly during iteration. Try adding padding if the convex hull is too close to the ends of image and there is no convergence.

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

-h hthresh ... HOUGH line length threshold either in pixels or percentage of the smallest input dimension; integer>0; default=20%

-m maxiter ...... MAXIMUM stopping number of INTERATIONS. Values are integers>=0. The default=0 (no iterations)

-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), (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 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:
(iterate):
-L 75% -m 10 -i save -k all

Thresholded Image

Cleaned Image

Convex Hull Image:

Hough Lines 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
  • Gets 4 lines from the sides of the convex hull
  • Sorts the lines clockwise from the top line
  • Intersects successive pairs of lines
  • Optionally outputs various images including one marking the corners
  • Prints the intersection x,y coordinates TL, TR, BR and BL