Whilst I almost always limit my commentary here to the G’MIC command line tool, and which of its commands might do what, on the subject of background detection and removal, almost every suggestion I could make entail some sort of interactive dialog. The reason for that is plain:
Homage To Women, Mico Kaufman.
Downtownstreets, photographer, Market Street Park, Lowell, Massachusetts
Intelligent folk such as you or I intuitively know what pixels in the image make up the statue and what make up the “not-statue” (background); automation has no such intuition. Automation only has an array of pixels, here comprising of so-called “red”, “green” and “blue” components. There are no indicators identifying statue pixels, brick walkway pixels, window glass pixels, and so forth. If I had a collection of images such as Homage to Women and the task to separate background from foreground, I would despair at coming up with any numerical rule set that could be trusted in automation to distinguish a background pixel from a foreground. Instead, I would resort to some intelligently guided interactive tool:
-
Use GIMP’s AI Technology To Remove Backgrounds Easily Intelligent scissors are native to GIMP.
-
Gimp Tutorial: Remove Background From Image Using GMIC In Gimp G’MIC interactive approach. By designating enough foreground regions (green points) and background regions (red points), you can — to a fairly high degree, if not to perfection — separate out arbitrarily complex backgrounds from similarly complex foregrounds, using the “Contours ⇒ Extract Foreground [Interactive]” filter in the G’MIC-Qt plug-in.
These approaches require judgement to directly or indirectly detect an edge separating background from foreground.
But if hundreds of images are in play, then an interactive background-foreground approach loses appeal. Automation might come to the rescue — perhaps. If the “background pixels” exhibit some identifiable numerical property — all the same or nearly the same color, or all with low local variance, which blurred backgrounds manifest — and if such “continuous regions” very nearly encompass all other regions, then G’MIC’s label
command can likely tag background pixels (probably assisted by a separate “detection filter”). Pixels, so tagged, provide a basis making a background mask. The background mask cuts the image. A rough approach follows:
cutplainbkgnd.gmic
cutplainbkgnd:
-foreach {
nm={n}
-name. current
# Force an alpha channel
-to_rgba[current]
# Assume ≈constant color
# cartoon-like images.
# Background gets zero label
+label[current] 30,1,1
# Not zero? Opaque. Otherwise clear
-neq. 0
-mul. 255
-name. mask
# Replace alpha channel with mask
-shared[current] {s#$current-1}
-fill. [mask]
# Cleanup pipeline
-keep[current]
-name. $nm
}
$ gmic -command cutplainbkgnd.gmic -input_glob *.png cutplainbkgnd -output cutback_.png
Example:
gmic -command cutplainbkgnd.gmic -input Downloads/rocketship.png +cutplainbkgnd. display_rgba. d , _parse_cli_images 1 append x o /dev/shm/pipeline.jpg,65
cutplainbkgnd.gmic
automates well for flat color images with all-encompassing backgrounds. Photographs are orders of magnitude more nuanced. For such, a batch process is likely to be not very reliable.