haralickt: GPU-Accelerated Haralick Texture Features in PyTorch, Over 20x Faster

haralickt implements all 13 Haralick features in pure PyTorch, with GPU acceleration up to tens of times faster.
haralickt is a pure PyTorch implementation of Haralick texture features that fills the gaps of mahotas being CPU-only and scikit-image computing only 10 of 13 features. It puts all 13 descriptors on the GPU or multi-threaded CPU via vectorized tensor operations and block tiling. Benchmarks show CPU mode averages 16.7x speedup over single-threaded mahotas, GPU mode averages 20.5x over 10-threaded mahotas, and roughly 20–60x on realistic images of 512×512 and above. Maximum numerical difference from mahotas is 2.02×10⁻⁴ from float32 rounding. The library offers a sliding-window API, memory-safe tiling, batch processing, a drop-in interface, handles images larger than 4K, requires zero C compilation, and is MPL-2.0 licensed. Performance on larger datasets and different GPU architectures still awaits community validation.
Why We Need GPU-Accelerated Haralick Features
In fields such as medical imaging, remote sensing, materials science, and defect detection, Haralick texture features (based on the gray-level co-occurrence matrix, GLCM) are a classic tool for describing image texture. For a long time, mahotas and scikit-image have been the two most commonly used libraries among researchers. They are stable and reliable, but share a common shortcoming: CPU-only computation. When dealing with large images or batch data, the GPU sits idle, creating an obvious bottleneck in the computation pipeline.
A developer built haralickt for this reason—a pure PyTorch alternative that puts all 13 Haralick descriptors directly on the GPU (also supporting multi-threaded CPU), accelerating via vectorized tensor operations and block tiling.

GLCM (gray-level co-occurrence matrix) is the statistical foundation of Haralick features: it counts how many times a pair of pixels separated by a specific distance and direction in an image have a given gray-level combination (i, j), forming an L×L matrix (L being the number of gray levels). From this matrix, 13 descriptors—such as contrast, correlation, energy, and homogeneity—can be derived to quantify the coarseness, directionality, and regularity of texture. GLCM computation scales quadratically with the number of gray levels and requires iterating over every pixel pair in the image, making it very time-consuming on large images; gray-level quantization (e.g., compressing 256 levels to 32) is a common technique for acceleration and noise reduction.
How It Differs from Existing Tools
haralickt's positioning is not to reinvent the algorithm from scratch, but to fill the gaps in hardware acceleration and engineering usability of existing tools. In the release post, the author compared the capability boundaries of the three:
- mahotas: Computes all 13 features, but CPU-only, no batch processing support, and high memory pressure on large images.
- scikit-image: NumPy-based, CPU-only, and computes only 10 of the 13 features (missing Sum Variance, Sum Entropy, Diff Variance, Diff Entropy, IMC1, IMC2), with no sliding-window or tiling API.
- haralickt: Supports CUDA GPU acceleration, all 13 features, a sliding-window API, memory-safe block tiling (can handle images larger than 4K), and batch processing.
This means that for scenarios requiring the complete feature set with large data volumes, haralickt offers more complete functional coverage.
Benchmarks: Where the 20x Speedup Comes From
The author tested 27 parameter combinations (window, stride, distance, number of gray levels) on 256×256 images with 32 gray levels. Compared with mahotas:
- CPU mode (vs. single-threaded mahotas): 16.7x average speedup, ranging from 9.1x to 67.7x.
- GPU mode (vs. 10-threaded mahotas): 20.5x average speedup, ranging from 6.9x to 295x.
The data on image-size scalability is even more telling. With window 16, stride 4, distance 1, and 32 gray levels:
| Image Size | mahotas (single-threaded) | haralickt CPU | Speedup | mahotas (10-threaded) | haralickt GPU | Speedup |
|---|---|---|---|---|---|---|
| 256×256 | 2.34s | 0.11s | 22x | 7.13s | 0.02s | 464x* |
| 512×512 | 5.44s | 0.43s | 13x | 3.22s | 0.05s | 61x |
| 1024×1024 | 19.98s | 1.88s | 11x | 6.54s | 0.23s | 29x |
| 2048×2048 | 70.13s | 6.68s | 11x | 15.25s | 0.72s | 21x |
The author specifically notes that the 464x speedup at 256×256 is inflated by mahotas' multiprocessing overhead. On realistic image sizes of 512×512 and above, the GPU speedup over 10-threaded OpenMP mahotas is more realistic, at roughly 20 to 60x.
Block tiling is a memory optimization strategy: splitting a large image into several non-overlapping or slightly overlapping blocks, computing GLCM block by block and aggregating the results, avoiding the need to allocate a huge intermediate tensor for the entire image at once. The sliding-window API, on the other hand, extracts local texture features position by position across the image at a fixed window size and stride, outputting a feature vector for each position, commonly used to feed texture as pixel-level or region-level features into downstream models. The difference between the two: tiling focuses on reducing peak memory usage, while sliding windows focus on generating spatially distributed feature maps.
Precision and Engineering Details
Numerical precision is a core concern for replacement tools. The data provided by the author is: the maximum difference compared with mahotas is 2.02 × 10⁻⁴, originating from float32 rounding errors in Diff Variance. For most texture analysis tasks, the impact at this magnitude is negligible, but if the downstream pipeline is extremely precision-sensitive, it is still worth verifying independently.
In terms of engineering implementation, haralickt has several pragmatic design choices:
- Zero C compilation: Pure Python + PyTorch (>=2.0), no compiler toolchain needed for installation.
- Ecosystem compatibility: Can be directly integrated with torchvision and MONAI, suitable for medical imaging deep learning pipelines.
- Dual interfaces: Provides both a sliding-window API (
haralick(image, num_levels=32, window_size=32, stride=8, device="cuda")returning a tensor of shape (H, W, 13)) andfeatures.haralick()as a drop-in replacement for mahotas, returning a NumPy array of shape (4, 13). - License: MPL-2.0.
Installation is via pip install haralickt.
float32 rounding errors stem from GPU tensor operations defaulting to single-precision floating point. GLCM normalization, entropy, and variance-type features involve logarithms, summations, and divisions; different accumulation orders during parallel reduction can amplify rounding differences. The maximum absolute difference of 2.02×10⁻⁴ is usually far smaller than the dynamic range of the features themselves, but if the downstream classifier is sensitive to feature scale, it is recommended to perform a feature-by-feature comparison on the target dataset before migrating, and consider using float64 for entropy-type features or increasing gray-level quantization precision.
Use Cases and Open Questions
From the design goals, the most direct beneficiaries of haralickt are teams processing large medical images (such as whole-slide images) and satellite remote sensing imagery, as well as pipelines that need to extract texture features in batches as deep learning inputs. The combination of sliding windows and block tiling means images larger than 4K no longer need to be loaded into GPU memory all at once.
However, the author is also soliciting feedback, especially test results on large-scale medical/satellite datasets and different GPU architectures. Currently, the public benchmarks are concentrated on 256×256 images and a limited set of parameter combinations; performance at larger sizes and higher gray levels still needs community validation. In addition, the project's current version number is low, and long-term maintenance activity is also a factor to evaluate before adoption.
For teams already heavily reliant on mahotas running on CPU clusters, the migration cost is low (drop-in interface), but the benefit depends on data scale—speedup is limited on small images, and large images and batch processing scenarios are where the GPU truly pulls ahead.
One-line Summary
haralickt implements all 13 Haralick features in pure PyTorch, with GPU acceleration up to tens of times faster.
Paragraph Summary
haralickt is a pure PyTorch implementation of Haralick texture features that fills the gaps of mahotas being CPU-only and scikit-image computing only 10 of 13 features. It puts all 13 descriptors on the GPU or multi-threaded CPU, accelerating via vectorized tensor operations and block tiling. Benchmarks show CPU mode averages 16.7x speedup over single-threaded mahotas, GPU mode averages 20.5x over 10-threaded mahotas, and roughly 20–60x on realistic images of 512×512 and above. The maximum numerical difference from mahotas is 2.02×10⁻⁴, due to float32 rounding. The library offers a sliding-window API, memory-safe tiling, batch processing, and a drop-in interface, can handle images larger than 4K, requires zero C compilation, and is MPL-2.0 licensed. Performance on larger datasets and different GPU architectures still awaits community validation.
Related articles

Why Microsoft CEO Nadella Still Backs Nvidia After Jensen Huang's Departure
Microsoft CEO Nadella publicly expresses confidence in Nvidia interim CEO Colette Kress. This article analyzes Nvidia's power transition after Jensen Huang's departure, Microsoft's compute dependency, and the trust logic of the AI supply chain.

Nadella: Critical Infrastructure Is 'Unpatchable' as AI Cybersecurity Arms Race Escalates
Microsoft CEO Nadella addresses AI cyberattack fears, saying much critical infrastructure is unpatchable, downplaying the Outlook outage, and analyzing AI's dual role in cybersecurity.

Musk Backs Jensen Huang: Firing Him Is Like Cutting Jordan
Musk says Jensen Huang is loyal to the U.S. and likens firing him to cutting Michael Jordan—highlighting politicization anxieties in the AI chip industry and defending talent value.