Skip to main content

Binned Cython 5 Feature GLCM

Result

Photo by Flo Maderebner from Pexels

800 Times faster, the above image takes 5.7 Days to process, compared to 9.8 Minutes with glcmbin5

Performance will vary with devices

pip install glcmbin5

Motivation

There are 2 main improvements, speed and memory size

Speed

This is >800 times faster than using skimage.feature.graycomatrix and graycoprops because this is Cython optimized.

With a 2000x1000x3 image, it takes around 2 minutes. Compared to a 33 hours with skimage

GLCM Progress: 100%|██████████| 12/12 [02:27<00:00, 12.30s/it]

Memory Size

If you don't bin the array before calculating GLCM, you'll end up with an extremely large GLCM.

With this algorithm, I omit generating the whole GLCM, instead, it's integrated in the GLCM feature calculation. Memory used is freed asap.

Plus, decreasing the GLCM size improves performance significantly.

Example

You can also see an example in /examples

from glcm.glcm import CyGLCM
import numpy as np
ar = ...
glcm = CyGLCM(ar.astype(np.float32),
              radius=3,
              bins=8,
              pairs=('H', 'V', 'SE', 'NE')
              ).create_glcm()

Arguments

  • radius: The radius of the GLCM window
  • bins: The number of bins to use
  • pairs:
    • H: Horizontal Pair
    • V: Vertical Pair
    • SE: South-East Diagonal Pair
    • NE: North-East Diagonal Pair

I/O

Input:

  • ndim = 3
  • shape=(in_dim0, in_dim1, channel)

Output:

  • ndim = 4
  • shape=(in_dim0, in_dim1, channel, features)
  • Methods:
    • Contrast
    • Correlation
    • Angular Second Moment
    • GLCM Mean
    • GLCM Variance

Progress Bar

The progress bar value is the current pair calculated.

Gotchas

GLCM Shrink

The resulting GLCM array will be smaller than the original.

GLCM Dimension = Dimension - (2 * radius + 1) = Dimension - Diameter

The + 1 comes from the pairing.

Data Type float32

Arrays MUST BE in np.float32, you need to cast it.

ar.astype(np.float32)

Features

Based on GLCM Texture: A Tutorial v. 3.0 March 2017.

For an effective segmentation, we just need 5 features as selected here.

Many features are not significantly orthogonal, hence more will introduce redundancy.

Chosen methods are for simplicity and efficiency in coding.

Binning

Arrays are Binned before going through GLCM.

All arrays will be processed to integer values [0,bin-1] band-independently.

Custom Cython Build

Run this command with the c_setup.py here

python c_setup.py build_ext --inplace

What's the magic?

There are several optimizations

  1. It's written mainly in Cython (with little required Python calls)
  2. Binning before running the GLCM decreases required GLCM calls

Citation

If you have used or referenced any of the code in the repository, please kindly cite

@misc{glcmbin5,
  author = {John Chang},
  title = {Binned Cython 5 Feature GLCM},
  year = {2021},
  publisher = {GitHub},
  journal = {GitHub Repository},
  howpublished = {\url{https://github.com/Eve-ning/glcm}},
}

Acknowledgements

Annex

Speed Benchmark with skimage.feature.greycomatrix

import time

import PIL.Image
import numpy as np
from matplotlib import pyplot as plt
from skimage.feature import greycomatrix, greycoprops
#%%

image = np.asarray(PIL.Image.open("sample.jpg"))[::2,::2,0]

s = time.time()
for i in range(10000):
    glcm = greycomatrix(np.random.randint(0, 192, [5, 5]).astype(np.uint8), [1], [0])
    g = greycoprops(glcm, 'contrast')
    g = greycoprops(glcm, 'dissimilarity')
    g = greycoprops(glcm, 'energy')
    g = greycoprops(glcm, 'ASM')
    g = greycoprops(glcm, 'correlation')

e = time.time()

# / 10000 for each window
# 1116 * 1991 because the image has that many windows
# 3 for 3 channels
# /147 for 2m27s of my current timing
# ~ 832.0464063705289
print(((e-s) * 8 / 10000 * 1116 * 1991 * 3)/147)

Metadata

Release files for glcmbin5 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for glcmbin5 0.1.4
File Size Uploaded
glcmbin5-0.1.4.tar.gz 56.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for glcmbin5 0.1.4
File Interpreter ABI Platform
glcmbin5-0.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 67.8 kB

Release files / glcmbin5-0.1.4.tar.gz

Download URL glcmbin5-0.1.4.tar.gz
Size 56.6 kB
Tags Source
SHA-256 checksum
How to use checksums
6cdb6d17fc7f2c9ef70307655df61b00d6ded71d6415fa7f8f0f363ac1467eb1
BLAKE2b-256 checksum
How to use checksums
14f14ccc1473791d3a819a2771b26dceac94908e106c0389b4843257a63d80d8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.9

Release files / glcmbin5-0.1.4-py3-none-any.whl

Download URL glcmbin5-0.1.4-py3-none-any.whl
Size 11.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7881dc0959f901fe96d54145b55daf2b02c04916ae2efaf549f06b8b219e5f52
BLAKE2b-256 checksum
How to use checksums
019dfa298daf78a6ceb8c185eb10cd765497b2eecf871e13ca9b3968eab8180e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.9
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page