Measure PGM Image Texture with pgmtexture
You will finish with a repeatable command for calculating texture features from a greyscale PGM image, and a way to compare runs made with different pixel distances. The installed Netpbm package here is version 2:11.05.02-1.1build1. Its pgmtexture command calculates features from spatial dependence matrices at 0, 45, 90 and 135 degrees.
The route
Jump straight to the step you need, or tick off Done means at the end.
Allow about fifteen minutes. You need a readable PGM file, a shell and Netpbm. The examples only read the image and write results to standard output. They do not modify the source image, require root, or change any system configuration.
1. Confirm the installed command
Check the executable and package version before relying on output in a script or report:
$ command -v pgmtexture
/usr/bin/pgmtexture
$ dpkg-query -W -f='${Package} ${Version}\n' netpbm
netpbm 2:11.05.02-1.1build1
Package versions differ between distributions. Keep the version beside saved measurements when results need to be reproduced later. The program reads one PGM image from the optional file argument, or from standard input when no file is supplied.
Checkpoint: make sure the input really is the file you intend to analyse:
$ file /path/to/image.pgm
$ test -r /path/to/image.pgm && echo 'readable'
Do not confuse PGM with PPM. PGM is the greyscale Netpbm format expected here. If you have another image format, convert it with a separate tool first and verify that conversion independently.
2. Run the default measurement
Run pgmtexture with the input path as its final argument:
$ pgmtexture /path/to/image.pgm
pgmtexture: (Image has 4 gray levels.)
pgmtexture: Computing spatial dependence matrix...
pgmtexture: ...done.
pgmtexture: Computing textural features ...
pgmtexture: ...done.
Angle 0 45 90 135 Avg
Angular Second Moment ...
Contrast ...
Correlation ...
Variance ...
Inverse Diff Moment ...
The exact values and the number of grey levels depend on the image. The table has one column for each direction and an average column. The default distance is 1 pixel. The command reports progress and the table on standard output, so redirect it when you need a record:
$ pgmtexture /path/to/image.pgm > texture-distance-1.txt
$ test -s texture-distance-1.txt && echo 'measurement saved'
Redirection with > replaces an existing destination before the command runs. If the old report matters, choose a new name or copy the old file first. For a safer replacement, write to a temporary name, inspect it, then move it deliberately:
$ pgmtexture /path/to/image.pgm > texture-distance-1.txt.new
$ test -s texture-distance-1.txt.new
$ mv texture-distance-1.txt.new texture-distance-1.txt
If the measurement fails, remove only the incomplete .new file with rm -- texture-distance-1.txt.new. That deletion is irreversible, so check the name before running it. The original report remains in place until the final mv.
3. Choose a different pixel distance
Use -d followed by a distance when texture at a wider separation is relevant:
$ pgmtexture -d 2 /path/to/image.pgm > texture-distance-2.txt
$ test -s texture-distance-2.txt && echo 'distance-2 measurement saved'
Distance 2 does not resize the image and does not mean two output pixels. It changes the separation used when building the spatial dependence matrices. Keep the distance in the output filename and in your notes, because a distance-1 result is not directly interchangeable with a distance-2 result.
Use a positive integer that is meaningful for the image dimensions. A large distance leaves fewer useful pixel pairs, especially in a small image, and can make the result less representative. The manual documents distance 1 as the default; it does not promise that every larger distance will produce a stable or useful measurement.
4. Read the feature table without overclaiming
The installed command prints a set of Haralick-style features, including Angular Second Moment, Contrast, Correlation, Variance, Inverse Difference Moment, Sum Average, Sum Variance, Sum Entropy, Entropy, Difference Variance, Difference Entropy, Information Measures of Correlation and Maximal Correlation Coefficient.
Compare like with like: use the same image preparation, distance, and command version when comparing samples. The directional columns show that texture can vary with orientation. The Avg column is useful as a compact summary, but it does not replace the four directional values when orientation matters.
These numbers are measurements derived from the image, not labels such as smooth or rough. Interpret them in the context of your image set and preprocessing. Do not compare values from different grey-level scaling or different image crops as if only texture had changed.
5. Handle the known convergence limitation
The calculation of Maximal Correlation Coefficient requires a second-largest eigenvalue of a matrix named Q. The manual warns that this method does not always converge. A run can therefore print a warning or fail to provide a trustworthy value even when other rows look usable.
Capture both the output and the exit status when the result feeds automation:
$ pgmtexture -d 2 /path/to/image.pgm > texture-distance-2.txt
$ status=$?
$ printf 'pgmtexture exit status: %s\n' "$status"
pgmtexture exit status: 0
A zero status tells you that the process completed; it does not remove the documented convergence limitation. Review the terminal diagnostics and the Maximal Correlation Coefficient row before publishing a result. If convergence is reported as a problem, retain the other values only if your analysis explicitly allows a partial feature set, and record the warning with the measurement.
Do not hide diagnostics by redirecting only standard output while troubleshooting. If you need one combined capture for an experiment, use &> in Bash and label the file clearly, then inspect which lines are diagnostics and which are table data.
6. Diagnose ordinary input failures
If the command cannot open the image, check the path and permissions before trying elevated privileges:
$ ls -l /path/to/image.pgm
$ test -r /path/to/image.pgm && echo 'input is readable'
If the input is not a valid PGM, convert or repair it with the appropriate image tool rather than changing the pgmtexture command at random. Keep the original input unchanged while testing a conversion. If standard input is part of a pipeline, isolate the producer first so you can tell whether the failure is in the image stream or in pgmtexture.
No step in this workflow needs sudo unless your chosen input or output directory is deliberately restricted. Running as root will not improve the texture calculation and can create root-owned report files that are awkward to replace later.
Done means
- You confirmed the installed Netpbm version and the path to
pgmtexture. - You measured a readable PGM image with the documented default distance of 1.
- You stored any redirected report without blindly destroying a previous result.
- You recorded non-default distances beside their output and compared like with like.
- You checked diagnostics and treated Maximal Correlation Coefficient convergence as a limitation.
- You used ordinary user permissions and left the source image and system configuration unchanged.