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.

UNROTATE3


Unrotates a rotated image and trims the surrounding border.

Download Script

last modified: June 28, 2022



USAGE: unrotate3 [-m mode] [-f fuzzval] [-c coords] [-b bcolor] [-d discard] [-k kind] [-t trimtype] [-F fuzztrim] [-r rotate] [-g graphic] infile outfile
USAGE: unrotate2 [-h or -help]

-m ... mode ........ mode of unrotate; clockwise (c), counterclockwise (cc),
.................... landscape (l) or portrait (p), aspect (a), smallest (s);
.................... default=smallest
-f ... fuzzval ..... fuzz value for floodfilling the background to separate
.................... image from background; 0<=integer<=100; default=15
-c ... coords ...... pixel coordinate to extract background color and seed
.................... location for flood fill; may be expressed as gravity
.................... value (NorthWest, etc)or as "x,y" value; default is
.................... NorthWest=(0,0)
-b ... bcolor ...... background color to use instead of option -c;
.................... any valid IM color; default is to use option -c
-d ... discard ..... discard any region that has an area smaller than
.................... this size; integer>0; default=1000
-k ... kind ........ kind of unrotate warp; affine (a) or rigid (r);
.................... default=affine
-t ... trimtype .... type of trimming to apply to the output; choices are
.................... outer, inner or none; default=none
-F ... fuzztrim .... fuzz value for trimming the output image;
.................... 0<=integer<=100; default=0
-r ... rotate ...... rotate the output image by 90, 180 or 270;
.................... default=no extra rotation
-g ... graphic ..... save graphic images showing floodfilled mask or vertices
.................... on the image; choices are mask, vertices or both;
.................... default=no graphic; graphic images will be named for
.................... the input with "_mask.gif" and "_vertices.png" appended
-G ... gcolor ...... color for the vertices in the graphic image;
.................... any valid IM color; default=red

PURPOSE: To unrotate a rotated image and trim the surrounding border.

DESCRIPTION: UNROTATE3 attempts to automatically unrotate the image. It assumes that the image contains a relatively constant color background area around the rotated data that is not too similar in color to the color near the boundary of the image data and especially not near the corners. The user is expected to identify the background color or a coordinate within the background region for the algorithm to extract the background color. A fuzz value should be specified when the background color is not uniform. A mask image is then created by a fuzzy flood fill. The extreme X and Y (min and max location) pixels are determined from the mask in order to find the four corners of the image. The dimensions of the image are then found from the four corners and these dimensions are used for the output image. The two sets of four points are then used as control points to do either an Affine warp or a Rigid Affine warp to effectively unrotate the image. For mode=aspect, the aspect ratio of the input image will determine the orientation of the output. This may not be an accurate measure, if the input image is largely padded. When mode=smallest, the process will unrotate by the smaller of the two directions (clockwise or counterclockwise). This will produce the correct result only if the input image was rotated by less than 45 degrees. When kind=affine, a full affine warp will be used, which may cause some skew change. When kind=rigid, a rigid affine warp will be used, which is rotation and scale with no skew. Either may leave a bit of border, which may mitigated with the trim option.

ARGUMENTS:

-m mode ... MODE of orientation of the image. Choices are clockwise (c), counterclockwise(cc), landscape (l), portrait (p), aspect (a) or smallest (s). The default=smallest (of the two rotation directions).

-f fuzzval... FUZZVAL is the fuzz value for flood filling the background in order to separate the image from the background. Values are 0<=integer<=100. The default=15.

-c coords ... COORDS are the pixel coordinates to identify the background color and to seed the location for the flood fill. It may be expressed as a gravity value (NorthWest, etc) or as an "x,y" value. The default is NorthWest, i.e., 0,0.

-b bcolor ... BCOLOR is the background color to use instead of option -c. Any valid IM color is allowed. The default is to use option -c.

-d discard ... DISCARD any region that has an area smaller than this size when identifying the largest white area in the mask representing the image region. That is merge small regions into its surrounding color. Values are integer>0. The default=1000.

-k kind ... KIND of unrotate warp. Choices are: affine (a) or rigid (r). The default=affine.

-t trimtype ... TRIMTYPE is the type of trimming to apply to the output. The choices are outer (0), inner (i) or none (n). The default=outer.

-F fuzztrim ... FUZZTRIM is the fuzz value for trimming the output image. Values are 0<=integer<=100. The default=0.

-r rotate ... ROTATE specifies to rotate the output image by 90, 180 or 270. The default is no extra rotation.

-g graphic ... GRAPHIC saves graphic images showing floodfilled mask or vertices on the image. The choices are: mask (m), vertices (v) or both (b). The default=no graphic. The graphic images will be named for the input with "_mask.gif" and "_vertices.png" appended.

-G gcolor GCOLOR is the color for the vertices in the graphic image. Any valid IM color. The default=red.

LIMITATIONS: This script may not perform well on nearly square images and may be off by increments of 90 degrees even for rectangular images. It is important that the fuzzy floodfill not cut off the corners of the 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


Example 1

Original Image: 20 degree orientation

Arguments:
-m smallest -g both


(unrotated)

(mask)

(vertices)




What the script does is as follows:

  • Reads the input into MPC format
  • Converts the image into a trimmed binary mask by floodfilling and using connected components
  • Finds the extreme X and Y coordinates of the mask by intersecting it with its bounding box
  • The four extreme points define the width and height of the output image
  • The four corners of the output image and the 4 extreme points are used as conjugate control
    points in an Affine warp that unrotates the image.
  • Saves the output