This project analyzes image textures using the Gray Level Co-occurrence Matrix (GLCM). GLCM is a statistical method for examining texture patterns by computing how frequently pairs of pixel intensities occur in an image. The extracted texture features help differentiate between different surface types.
- Image Acquisition: Downloads images from Google Drive.
- Image Preprocessing: Converts images to grayscale for analysis.
- GLCM Computation: Generates co-occurrence matrices for different textures.
- Texture Feature Extraction: Computes statistical properties such as contrast, homogeneity, energy, and correlation.
- Comparative Analysis: Groups images into categories and evaluates their texture characteristics.
The project analyzes five types of textures:
- Smooth
- Rough
- Circuit (Mixed)
- Periodic
- Random
These images are retrieved from Google Drive and processed in Python.
Ensure you have the required dependencies installed:
pip install numpy scikit-image matplotlib pillow requestsRun the script in a Jupyter Notebook or Google Colab to execute the following steps:
from PIL import Image
import numpy as np
from skimage.feature import graycomatrix, graycoprops
from skimage.color import rgb2gray, rgba2rgb
import requests
import matplotlib.pyplot as plt
import matplotlib.image as mpimgImages are retrieved using Google Drive file IDs and saved locally.
def get_file(url, name, format):
response = requests.get(url)
if response.status_code == 200:
with open(f'{name}.{format}', 'wb') as f:
f.write(response.content)Images are displayed using Matplotlib for visualization.
fig, axs = plt.subplots(1, 3, figsize=(15, 5))
for ax, img, title in zip(axs, [circuit, periodic, random], ['Mixed', 'Periodic', 'Random']):
ax.imshow(img)
ax.set_title(title)
ax.axis('off')
plt.show()Convert images to grayscale and compute their GLCM matrices.
smooth_gray = (255*rgb2gray(rgba2rgb(smooth))).astype(np.uint8)
glcm_smooth = graycomatrix(smooth_gray, distances=[2], angles=[0], levels=256, symmetric=True, normed=True)Statistical properties like contrast, homogeneity, energy, and correlation are extracted from the GLCM matrices.
def run_graycoprops(glcm):
stats = {}
for prop in ['contrast', 'dissimilarity', 'homogeneity', 'energy', 'correlation']:
stats[prop] = graycoprops(glcm, prop)[0, 0]
return statsExtracted features are used to compare texture groups.
print("Texture Comparison:")
print("Periodic Texture:", run_graycoprops(glcm_periodic))
print("Random Texture:", run_graycoprops(glcm_random))- Smooth textures show high homogeneity and low contrast.
- Rough and Random textures have high contrast values, indicating more variations.
- Periodic textures exhibit structured repetition, leading to distinct correlation values.
- Circuit (Mixed) textures have features that blend characteristics of multiple categories.
📌 Author: [Arian Fathi]
📌 Contact: [[email protected]]
📌 Keywords: Texture Analysis, GLCM, Image Processing, Python, skimage, Matplotlib