Use pgmmorphconv to Dilate, Erode and Filter PGM Images
You will finish with a working Netpbm command that applies dilation, erosion, opening, closing or a morphological gradient to a PGM image. The examples use the locally installed Netpbm 11.5.2 package and write the result to standard output, so the original image remains untouched.
The route
Jump straight to the step you need, or tick off Done means at the end.
Allow about fifteen minutes. You need a shell, the pgmmorphconv command and a PGM image to process. You also need a PBM template: this is a small black-and-white image that describes which neighbouring pixels take part in the operation. These examples inspect and transform images only. They do not overwrite an input unless you redirect output over it yourself.
1. Check the installed command
Confirm the executable and package version before relying on an option in a script:
$ command -v pgmmorphconv
/usr/bin/pgmmorphconv
$ dpkg-query -W -f='${Package} ${Version}\n' netpbm
netpbm 2:11.05.02-1.1build1
$ pgmmorphconv --version
pgmmorphconv: Using libnetpbm from Netpbm Version: Netpbm 11.5.2
The installed command accepts a template file followed by an optional PGM input file. If the input file is omitted, it reads PGM data from standard input. It writes the transformed PGM image to standard output. No elevated privileges are needed.
Checkpoint
If command -v finds nothing, stop and install the package through your normal system change process. Do not copy a binary from another host and assume its template or format behaviour is identical.
2. Create a small template
A template must be a PBM image with an odd number of rows and an odd number of columns. The centre pixel is aligned with the input pixel currently being calculated, so an odd size gives the operation an unambiguous centre. It must contain at least one white pixel. In PBM text format, 1 is white and 0 is black.
This 3 by 3 template selects the pixel itself and its four direct neighbours:
$ cat > cross.pbm <<'EOF'
P1
3 3
0 1 0
1 1 1
0 1 0
EOF
The command above changes state by creating a file, but it is easy to undo:
$ rm -- cross.pbm
Do not use an even-sized template or an all-black template. The former has no single centre; the latter has no selected input pixels. Keep the template beside your script and review it before processing a large image.
3. Run the default dilation
Start with a named output file rather than replacing the input:
$ pgmmorphconv cross.pbm input.pgm > dilated.pgm
$ printf 'exit status: %s\n' "$?"
exit status: 0
$ file dilated.pgm
dilated.pgm: Netpbm image data, size 5 x 5, rawbits, grayscale
With no operation option, pgmmorphconv performs dilation. For each output position it examines the input pixels under the white template pixels and chooses the greatest intensity. Bright regions therefore expand and dark regions shrink according to the template shape. The result has the same dimensions as the input.
The output is binary PGM data by default, which is normal. If you want to inspect it in a terminal, convert it to plain PGM without changing the source:
$ pgmmorphconv cross.pbm input.pgm | pnmtoplainpnm | sed -n '1,8p'
P2
5 5
9
0 5 5 5 0
Your dimensions and pixel values will differ. The useful checks are a successful exit status, a valid PGM header and the expected output dimensions.
4. Choose erosion or a compound operation
Pass exactly one of these operation options before the template file:
-dilatechooses the maximum selected intensity. This is also the default.-erodechooses the minimum selected intensity, making bright regions contract.-openerodes first, then dilates. This can remove small bright features while retaining the broad shape.-closedilates first, then erodes. This can fill small dark gaps inside bright regions.-gradientproduces the difference between the eroded and dilated images.
For example:
$ pgmmorphconv -erode cross.pbm input.pgm > eroded.pgm
$ pgmmorphconv -open cross.pbm input.pgm > opened.pgm
$ pgmmorphconv -close cross.pbm input.pgm > closed.pgm
$ pgmmorphconv -gradient cross.pbm input.pgm > gradient.pgm
$ for image in eroded.pgm opened.pgm closed.pgm gradient.pgm; do
> pnmfile "$image"
> done
eroded.pgm: 5 by 5 grayscale, maxval 9
opened.pgm: 5 by 5 grayscale, maxval 9
closed.pgm: 5 by 5 grayscale, maxval 9
gradient.pgm: 5 by 5 grayscale, maxval 9
These are image operations, not edge-detection guarantees. The visible result depends on the selected template, image intensities and border handling. Compare the output with a known small image before choosing a template for automated recognition.
5. Use standard input safely
Leaving out the PGM file makes the input stream the source. This is useful when another Netpbm command generates the image:
$ pnmscale -width 800 source.pgm | pgmmorphconv --dilate cross.pbm > scaled-dilated.pgm
$ pnmfile scaled-dilated.pgm
scaled-dilated.pgm: 800 by 600 grayscale, maxval 255
The long option is accepted in place of the single-hyphen form, and the program accepts a unique abbreviation. Prefer the complete option in saved scripts, because an abbreviation can become ambiguous if a future version adds another option.
Do not redirect output to the same path as the input. The shell opens the destination before pgmmorphconv reads the source, so this can truncate the original before processing begins. If you accidentally produced a bad destination, replace it from a backup or regenerate it from the original. There is no undo for a truncated source without a separate copy.
6. Diagnose the common failures
If the command reports that the image is smaller than the convolution matrix, inspect the dimensions of both files:
$ pnmfile input.pgm cross.pbm
input.pgm: 5 by 5 grayscale, maxval 9
cross.pbm: 3 by 3 bilevel, maxval 1
The input must be large enough for the template. A 3 by 3 template cannot process a 2 by 2 image. If the template is rejected, check that it is PBM, has odd dimensions and contains white pixels. If a pipeline fails, save its intermediate output and run pnmfile on that file; a command producing an error message instead of an image is a frequent source of misleading downstream failures.
Checkpoint
A finished transformation has a zero exit status, a readable PGM destination, unchanged input, and dimensions matching the input.
Done means
- The installed Netpbm version and executable were checked.
- The PBM template has odd dimensions, a centre pixel and at least one white pixel.
- The selected operation was explicit, or the default dilation was intentional.
- The result was written to a separate file and passed
pnmfile. - The original PGM remains available for comparison or recovery.