convert nearly black/white borders to black
nearblack [-white] [-near dist] [-nb non_black_pixels] [-o outfile] infile
This utility will scan an image and try to set all pixels that are nearly black (or nearly white) around the collar to exactly black (or white). This is often used to "fix up" lossy compressed airphotos so that color pixels can be treated as transparent when mosaicing.
The algorithm processes the image one scanline at a time. A scan "in" is done from either end setting pixels to black (white) until at least "non_black_pixels" pixels that are more than "dist" gray levels away from black (white) have been encountered at which point the scan stops. The nearly black (white) pixels are set to black (white).
Note that this algorithm is only applied to horizontal scanlines, so a photo with an indentation in the top or bottom will not have that indentation identified. The processing is all done in 8bit (Bytes).
If the output file is omitted, the processed results will be written back to the input file - which must support update.