pnmnlfilt
Apply nonlinear filters to portable bitmap images
TLDR
Apply the "alpha trimmed mean" filter with the specified alpha and radius values onto the PNM image
Apply the "optimal estimation smoothing" filter with the specified noise threshold and radius onto the PNM image
Apply the "edge enhancement" filter with the specified alpha and radius onto the PNM image
SYNOPSIS
pnmnlfilt [-width W] [-height H] [-weight W] [-all] [-percent P] [-median | -mode | -hilow | -average | -min | -max | -range | -sdev | -total | -gauss | -user filterfile] [pnmfile]
PARAMETERS
-width W
Sets the width of the filter window to W pixels. The default width is 1.
-height H
Sets the height of the filter window to H pixels. The default height is 1.
-weight W
Assigns a weight to the center pixel within the filter window. The default weight is 1. A higher weight makes the center pixel's original value more influential in the resulting calculation.
-all
If the input is a PPM (color) image, this option causes pnmnlfilt to apply the filter to each of the three color components (Red, Green, Blue) separately. The output will also be a PPM image. Without this option, for a PPM input, pnmnlfilt extracts the grayscale equivalent of the image, processes it, and outputs a PGM (grayscale) image.
-percent P
Specifies the percentile to use for filtering. P is a floating-point number between 0 and 100. This is typically used with percentile-based operations like median filtering (which is 50%).
-median
Performs a median filter. Each pixel is replaced by the median value of all pixels within its defined window. This is the default operation if no other filter type is specified.
-mode
Performs a mode filter. Each pixel is replaced by the most frequently occurring value among the pixels within its window.
-hilow
Replaces the pixel value with the average of the lowest and highest pixel values found within its window.
-average
Replaces the pixel value with the arithmetic mean (average) of all pixel values within its window.
-min
Performs an erosion operation; each pixel is replaced by the minimum value found within its window.
-max
Performs a dilation operation; each pixel is replaced by the maximum value found within its window.
-range
Replaces the pixel value with the range (maximum - minimum) of pixel values within its window.
-sdev
Replaces the pixel value with the standard deviation of the pixel values within its window.
-total
Replaces the pixel value with the sum of all pixel values within its window.
-gauss
Replaces the pixel value with a Gaussian-weighted average of the pixel values within its window, providing a smoothing effect.
-user filterfile
Allows the user to define a custom filter. The filter characteristics, including type, window dimensions, and optional pixel weights, are specified in the provided filterfile.
DESCRIPTION
pnmnlfilt applies a non-linear filter to a Portable Anymap (PNM) image, which includes PGM (Portable Graymap) and PPM (Portable Pixmap) formats. This command is highly effective for noise reduction, particularly useful for preserving image edges while removing speckle or salt-and-pepper noise, a task where linear filters often fall short by blurring features. It operates by evaluating a defined neighborhood (or 'window') of pixels around each target pixel. The target pixel's value is then replaced with a value derived from this neighborhood using a non-linear function.
Common non-linear operations supported include median filtering (default), where the pixel value is replaced by the median of its neighbors, or mode filtering, which replaces it with the most frequent value. Users can control the filter's behavior by specifying the width and height of the neighborhood window, and by choosing from various filtering algorithms. The input image can be PGM or PPM; if a PPM image is processed without the -all option, it will be converted to grayscale and output as a PGM. This tool is a fundamental component of the Netpbm suite for versatile image manipulation and processing.
CAVEATS
Processing large images or using wide filter windows can be computationally intensive and consume significant memory, potentially leading to slow execution times. Users should be aware that if a PPM (color) image is provided as input and the -all option is not specified, pnmnlfilt will convert the image to grayscale, apply the filter, and output a PGM (grayscale) image. This might lead to unexpected color loss if not anticipated.
EXAMPLES
To apply a 5x5 median filter to a grayscale image:pnmnlfilt -width 5 -height 5 input.pgm > output.pgm
To apply a 3x3 mode filter to a color image while preserving its colors:pnmnlfilt -width 3 -height 3 -mode -all color_input.ppm > color_output.ppm
To apply a 7x7 minimum filter (erosion) to reduce noise in a binary-like image:pnmnlfilt -width 7 -height 7 -min binary.pbm > eroded.pbm
HISTORY
Part of the Netpbm suite, pnmnlfilt descends from Jef Poskanzer's Pbmplus package, which was originally developed in the late 1980s. It provides fundamental non-linear filtering capabilities, a core technique in image processing for noise reduction and edge preservation, integrating seamlessly into the comprehensive Netpbm toolkit. Its continued development as part of Netpbm reflects the ongoing need for robust, low-level image manipulation tools in a Unix-like environment.