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.

SMARTTRIM


Automatically trims an image around the region of highest detail in the image.

Download Script

last modified: December 15, 2018



USAGE: smarttrim [-m metric] [-f fuzzval] [-b blur] [-s size] [-l lowerthresh] [-u upperthresh] [-g graymode] [-r restrict] [-a area] [-p pad] infile outfile

USAGE: smarttrim [-h|-help]

-m ... metric ........ metric to use to process the image for detail; choices are
...................... std (for standard_deviation), sobel (for sobel type grayscale
...................... edges), canny (for canny type binary edges) and corners
...................... (for morphologic corner detection); default=sobel
-f ... fuzzval ....... fuzz value for trim expressed as percent; 0<=float<=100;
...................... default=10
-b ... blur .......... blur the detail image before normalizing; integer>=0; zero
...................... means no blur; default=20
-s ... size .......... optional size for output; trimming will be performed, then the
...................... centroid located and the image cropped by the size about the
...................... centroid; default is no cropping, only trimming
-l ... lowerthresh ... lower threshold for canny edges (percent); smaller is more edges;
...................... 0<=integer<=100; default=10
-u ... upperthresh ... upper threshold for canny edges (percent); smaller is more edges;
...................... 0<=integer<=100; default=30
-g ... graymode ...... mode of converting to grayscale; grayscale or maximum;
...................... default=grayscale
-r ... restrict ...... restrict to only largest thresholded region using connected
...................... components labeling (ccl); yes or no; default=no
-a ... area .......... area threshold for connected components labeling (ccl) filtering;
...................... integer>=0; only used when keep=yes; default automatically computed
-p ... pad ........... pad crop area; allows up to 4 comma separate values to represent
...................... top,right,bottom,left amount of increase in crop area;
...................... all values are integers>=0; default=no padding

PURPOSE: To automatically trim an image around the region of highest detail in the image. region.

DESCRIPTION: SMARTTRIM automatically trims an image around the region of highest detail in the image. It finds the remaining region left after thresholding of an image extracted using fundamental type details in the image. The detail metric can be: standard deviation or sobel grayscale edges. This is a low to medium intelligent in that it uses fundamental measures of detail. So perhaps it should really method be called a semi-intelligent method. It does not try to do face detection. The script does best with a single region of high detail and the rest of the image low detail. The threshold used is 100-fuzzval in percent. The user may optionally supply a size as WxH. In that case, then centroid of the trimmed region will be located and the image cropped to that size centered around the centroid. This script can also trim some scanned text documents with moderately smooth but complex backgrounds. In this case, -m corners seems to work best.

ARGUMENTS:

-m metric ... METRIC to use to process the image for detail. The choices are std (for standard_deviation), sobel (for sobel type grayscale edges), canny (for canny type binary edges) and corners (for morphologic corner detection). The default=sobel.

-f fuzzval ... FUZZVAL is the fuzz value for trimming expressed as percent. Values are 0<=float<=100; The default=10.

-b blur ... BLUR the detail image before normalizing. Values are integers>=0. A value of zero means no blur. Typical values between about 20 and 50. The larger the blur, generally the more square the trim result will be. The default=20.

-s size ... SIZE is an optional size for output expressed as WxH. If only one is specified, then it will be used for both. When specified, trimming will be performed. Then the centroid will be located and the image cropped by the specified size about the centroid. The default is no cropping, only trimming.

-l lowerthresh LOWERTHRESH is the lower threshold for canny edges (in percent). Smaller values produce more edges. Values are 0<=integers<=100. The default=10.

-u upper UPPERTHRESH is the upper threshold for canny edges (in percent). Smaller values produce more edges. Values are 0<=integers<=100. The default=30.

-g graymode ... GRAYMODE is the mode of converting the detail image to grayscale. The choices are: grayscale (change colorspace to gray) or maximum (separate channels and take the maximum of the channels at each pixels). The default=grayscale. Usually grayscale is better than grayscale. But occasionally the opposite is true.

-r restrict ... RESTRICT to only the largest thresholded region using connected componentslabeling (ccl). Choices are: yes or no. The default=no.

-a area ... AREA is the area threshold for connected components labeling (ccl) filtering. All regions with area less than this value will be removed. Values are integers>=0. Too small a value may cause the script to break with too many regions found. Too large an area will result in no areas found. Used only when keep=yes. The default is automatically computed from image dimensions.

-p pad ... PAD crop area. It allows up to 4 comma separate values to represent top,right,bottom,left amount of increase in crop area. All values are integers>=0. The default is no padding. Any unspecified values after the last value specified will be set to the last specified value. No skips are allowed. Pad cannot be used in combination with size. The argument -s size takes precedent.

REQUIREMENTS: Requires IM 6.6.9.1 or higher for metric=std. Requires IM 6.8.9.0 or higher for metric=canny. But due to a bug in IM 7 for the -canny operator, results will not be correct until IM 7.0.6.1 or higher, when the bug was fixed. Requires IM 6.5.9.3 or higher for metric=corners. Requires IM 6.6.1.8 or higher for metric=sobel.

CREDITS: Many concepts suggested by Alan Gibson were incorporated in this work. See http://im.snibgo.com/crop2det.htm

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 --- Comparison Of Metrics For Fuzzvals 1,2,5,10,15 And Given Graymode and Restrict Options

Original
(original)

Arguments:
-g grayscale -r no
-g grayscale -r no
-g grayscale -r no
-g grayscale -r no
(-f fuzzvals of 1, 2, 5, 10, 15 left to right)

sobel

std

canny

corners

 

Arguments:
-g maximum -r no
-g maximum -r no
-g maximum -r no
-g maximum -r no
(-f fuzzvals of 1, 2, 5, 10, 15 left to right)

sobel

std

canny

corners

 

Arguments:
-g grayscale -r yes
-g grayscale -r yes
-g grayscale -r yes
-g grayscale -r yes
(-f fuzzvals of 1, 2, 5, 10, 15 left to right)

sobel

std

canny

corners

 

Arguments:
-g maximum -r yes
-g maximum -r yes
-g maximum -r yes
-g maximum -r yes
(-f fuzzvals of 1, 2, 5, 10, 15 left to right)

sobel

std

canny

corners



Example 2 --- Comparison Of Metrics For Fuzzvals 1,2,5,10,15 And Given Graymode and Restrict Options

Original
(original)

Arguments:
-g grayscale -r no
-g grayscale -r no
-g grayscale -r no
-g grayscale -r no
(-f fuzzvals of 1, 2, 5, 10, 15 left to right)

sobel

std

canny

corners

 

Arguments:
-g maximum -r no
-g maximum -r no
-g maximum -r no
-g maximum -r no
(-f fuzzvals of 1, 2, 5, 10, 15 left to right)

sobel

std

canny

corners

 

Arguments:
-g grayscale -r yes
-g grayscale -r yes
-g grayscale -r yes
-g grayscale -r yes
(-f fuzzvals of 1, 2, 5, 10, 15 left to right)

sobel

std

canny

corners

 

Arguments:
-g maximum -r yes
-g maximum -r yes
-g maximum -r yes
-g maximum -r yes
(-f fuzzvals of 1, 2, 5, 10, 15 left to right)

sobel

std

canny

corners



Example 3 --- Comparison Of Metrics For Fuzzvals 1,2,5,10,15 And Given Graymode and Restrict Options

Original
(original)

Arguments:
-g grayscale -r no
-g grayscale -r no
-g grayscale -r no
-g grayscale -r no
(-f fuzzvals of 1, 2, 5, 10, 15 left to right)

sobel

std

canny

corners

 

Arguments:
-g maximum -r no
-g maximum -r no
-g maximum -r no
-g maximum -r no
(-f fuzzvals of 1, 2, 5, 10, 15 left to right)

sobel

std

canny

corners

 

Arguments:
-g grayscale -r yes
-g grayscale -r yes
-g grayscale -r yes
-g grayscale -r yes
(-f fuzzvals of 1, 2, 5, 10, 15 left to right)

sobel

std

canny

corners

 

Arguments:
-g maximum -r yes
-g maximum -r yes
-g maximum -r yes
-g maximum -r yes
(-f fuzzvals of 1, 2, 5, 10, 15 left to right)

sobel

std

canny

corners



Example 4 --- Comparison Of Metrics For Fuzzvals 1,2,5,10,15 And Given Graymode and Restrict Options

Original
(original)

Arguments:
-g grayscale -r no
-g grayscale -r no
-g grayscale -r no
-g grayscale -r no
(-f fuzzvals of 1, 2, 5, 10, 15 left to right)

sobel

std

canny

corners

 

Arguments:
-g maximum -r no
-g maximum -r no
-g maximum -r no
-g maximum -r no
(-f fuzzvals of 1, 2, 5, 10, 15 left to right)

sobel

std

canny

corners

 

Arguments:
-g grayscale -r yes
-g grayscale -r yes
-g grayscale -r yes
-g grayscale -r yes
(-f fuzzvals of 1, 2, 5, 10, 15 left to right)

sobel

std

canny

corners

 

Arguments:
-g maximum -r yes
-g maximum -r yes
-g maximum -r yes
-g maximum -r yes
(-f fuzzvals of 1, 2, 5, 10, 15 left to right)

sobel

std

canny

corners



Example 5 --- Comparison Of Blur Values

Original
(original)

Arguments:
-g grayscale -r no -f 2
(-b blur 20, 30, 40 left to right)



Example 6 --- Comparison Of Different Crop Sizes

Arguments:
-m sobel -g grayscale -r no -f 5 -b 20
(-s sizes automatic, 50x50, 100x50, 200x100, 200x150, 200x200, 250x150 left to right)



Example 7 --- Trim Of Text Scan

Original
(original)

Arguments:
-m corners -g grayscale -r no -f 10



Example 8 --- Trim Of Text Scan

Original
(original)

Arguments:
-m corners -g grayscale -r no -f 8



Example 9 --- Trim Of Text Scan

Original
(original)

Arguments:
-m corners -g grayscale -r no -f 30



Example 10 --- Trim Of Text Scan

Original
(original)

Arguments:
-m corners -g grayscale -r no -f 55



What the script does is as follows for the case of -r no:

  • Reads the input
  • Applies the desired metric, fuzzval, graymode and blur
  • Equalizes the image and thresholds it according to 100-fuzzval
  • Extracts the the trim coordinates
  • Crops the input according to the trim coordinates
  • Writes the output

This is equivalent to the following IM commands

  • if [ "$metric" = "std" ]; then
    dproc="-statistic standard_deviation 3x3 -colorspace gray"
    elif [ "$metric" = "sobel" ]; then
    dproc="-define convolve:scale=50%! -bias 50% -write mpr:img +delete \
    mpr:img -morphology Convolve Sobel:0 +write mpr:dx +delete \
    mpr:img -morphology Convolve Sobel:90 +write mpr:dy +delete \
    mpr:dx mpr:dy -solarize 50% -level 50,0% -evaluate Pow 2 \
    -compose plus -composite -evaluate Pow 0.5"
    elif [ "$metric" = "canny" ]; then
    dproc="-canny 0x1+${lowerthresh}%+${upperthresh}%"
    elif [ "$metric" = "corners" ]; then
    dproc="-write mpr:img +delete \
    mpr:img -morphology dilate plus:1 -morphology erode diamond:1 -write mpr:plus +delete \
    mpr:img -morphology dilate cross:1 -morphology erode square:1 -write mpr:cross +delete \
    mpr:plus mpr:cross -compose difference -composite"
    fi
  • if [ "$graymode" = "grayscale" ]; then
    gproc="-colorspace gray"
    elif [ "$graymode" = "maximum" ]; then
    gproc="-separate -evaluate-sequence max"
    fi
  • if [ "$blur" != "0" ]; then
    bproc="-blur 0x$blur"
    else
    bproc=""
    fi
  • thresh=`convert xc: -format "%[fx:100-$fuzzval]" info:`
  • cropvals=`convert "$infile" -alpha off \
    $dproc $gproc $bproc -equalize -threshold $thresh% \
    -format "%@" info:`
  • convert "$infile" -crop $cropvals +repage "$outfile"