The convex hull is the smallest area convex polygon containing the set of points inside it. It may be visualized as the shape enclosed by a rubber band stretched around the set of points.
The convex-hull string format returns a list of x,y coordinates of the vertices of the polygon containing all non-black pixels within it.
Convex Hull Format & Options
$ magick [-fuzz value%] [-background background-color] -format "%[convex-hull]" info:
Use %[convex-hull:extreme-points] to return only the top, right, bottom, and left extreme points. The string format is sensitive to an optional -fuzz value and an optional background color specified by the define. The command automatically finds the background color according to the fuzz value unless the background color is explicitly defined. The fuzz value defaults to 0 when not specified.
Example 1: White Rectangle
Consider the following white rectangle on a black background:
The convex hull set of points is computed as follows:
$ magick rect.png -format "%[convex-hull]" info:
190,121 193,122 281,154 377,189 378,190 377,193 345,281 310,377 309,378 306,377 218,345 122,310 121,309 122,306 154,218 189,122 190,121
The convex hull can also be drawn as a polygon (filled or unfilled) using -draw:
$ magick rect.png -set option:hull "%[convex-hull]" -fill none -stroke red -strokewidth 1 -draw "polygon %[hull]" blocks_hull.png
Example 2: Non-Convex Shapes
Here is an example using a non-convex shaped image on a black background:
$ magick blocks_black.png -set option:hull "%[convex-hull]" -fill none -stroke red -strokewidth 1 -draw "polygon %[hull]" blocks_hull.png
Example 3: Non-Constant Background
Finally, here is an example with a non-constant, non-black background using -fuzz to determine the background color:
Highlighting the convex hull of the image foreground object:
$ magick barn_rot20.png -fuzz 10% -set hull "%[convex-hull]" -fill none -stroke red -strokewidth 1 -draw "polygon %[hull]" barn_rot20_hull.png
Minimum Bounding Box
Similar to the convex hull of foreground objects, the minimum bounding box provides bounding geometry. Use %[minimum-bounding-box] to return the points making up the bounding box. Additionally, properties such as minimum-bounding-box:area, minimum-bounding-box:width, minimum-bounding-box:height, and minimum-bounding-box:angle are set.
$ magick barn_rot20.png -fuzz 10% -background gray25 -set MBR "%[minimum-bounding-box]" -fill none -stroke red -strokewidth 1 -draw "polygon %[MBR]" barn-mbr.png
Unrotate an Image
Use %[minimum-bounding-box:unrotate] to correct for rotation in an image. Unless you specify orientation, the unrotate angle properly aligns the image for angles <= 45°; for angles > 45°, it aligns with the incorrect coordinate axis.
Set the correct image orientation using -define minimum-bounding-box:orientation={landscape,portrait} to ensure the proper unrotate angle is returned. Like convex-hull, the string format is sensitive to optional -fuzz and -background values.
Since the barn image above is rotated by 20 degrees, here is how we automatically correct for rotation and restore its original landscape orientation:
$ magick barn_rotate20.jpg -fuzz 15% -set option:angle "%[minimum-bounding-box:unrotate]" -background gray -rotate "%[angle]" -trim barn_unrotate.jpg