New convex_hull method

Announcements pertaining to ImageMagick, or ImageMagick related software. This list is moderated. No discussions here, instead post to the users group instead.
Locked
User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

New convex_hull method

Post by fmw42 »

IM 7.0.10.1 introduces the convex-hull string format.

The convex hull is the is the smallest area convex polygon containing the set of points inside it. The convex hull 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 convex-hull polygon containing all the non-black pixels within it.

The syntax is:

Code: Select all

magick <-fuzz XX%> <-define convex-hull:background-color> -format "%[convex-hull]" info:
where the string format is sensitive to an optional -fuzz value and to an optional background color specified by the define. The command above will find the background color automatically according to the fuzz value unless the background color is defined. The fuzz value defaults to 0 when not specified.

For example, consider the following white rectangle on a black background:

Image

The convex hull set of points may be computed as:

Code: Select all

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 may also be drawn as a polygon (filled or unfilled) using -draw as follows:

Code: Select all

magick rect.png -set option:hull "%[convex-hull]" -fill none -stroke red -strokewidth 1 -draw "polygon %[hull]" kdf_hull.png
Image


Here is an example using a non-convex shaped image on a black background:

Image

Code: Select all

magick kdf_black.png -set option:hull "%[convex-hull]" -fill none -stroke red -strokewidth 1 -draw "polygon %[hull]" kdf_hull.png
Image


Finally, here is an example with a non-constant, non-black background using -fuzz to determine the background color:

Image

Code: Select all

magick barn_rot20.png -fuzz 30% -set option:hull "%[convex-hull]" -fill none -stroke red -strokewidth 1 -draw "polygon %[hull]" barn_rot20_hull.png
Image

Locked