Last modified: Apr 21, 2025 By Alexander Williams
Python Basic Image Texture Analysis Guide
Image texture analysis helps in understanding patterns in images. It is used in many fields like medical imaging and object recognition. Python makes it easy with libraries like OpenCV and scikit-image.
Table Of Contents
What Is Image Texture Analysis?
Texture analysis studies the spatial arrangement of color or intensities. It helps in identifying patterns like smoothness or roughness. This is useful in image classification and segmentation.
Common texture features include contrast, energy, and homogeneity. These features can be extracted using Python. Learn more in our Python Image Classification Guide.
Libraries for Texture Analysis
Python offers powerful libraries for texture analysis. OpenCV and scikit-image are the most popular. They provide built-in functions for feature extraction.
OpenCV is great for real-time applications. scikit-image is ideal for research and prototyping. Both are easy to use and well-documented.
Loading an Image in Python
First, load an image using OpenCV or scikit-image. Here’s how to do it with OpenCV:
import cv2
# Load an image
image = cv2.imread('texture.jpg', cv2.IMREAD_GRAYSCALE)
print(image.shape)
(512, 512)
The cv2.imread
function loads the image. The second argument converts it to grayscale. Grayscale simplifies texture analysis.
Extracting Texture Features
Texture features can be extracted using GLCM (Gray-Level Co-occurrence Matrix). The skimage.feature.graycomatrix
function helps compute GLCM.
from skimage.feature import graycomatrix, graycoprops
# Compute GLCM
glcm = graycomatrix(image, distances=[1], angles=[0], levels=256, symmetric=True, normed=True)
# Extract contrast
contrast = graycoprops(glcm, 'contrast')[0, 0]
print(f"Contrast: {contrast}")
Contrast: 350.25
The graycoprops
function calculates texture properties. Contrast measures intensity variations. Higher values mean more texture.
Other Texture Features
Besides contrast, other features include energy and homogeneity. Energy measures uniformity. Homogeneity checks pixel similarity.
energy = graycoprops(glcm, 'energy')[0, 0]
homogeneity = graycoprops(glcm, 'homogeneity')[0, 0]
print(f"Energy: {energy}")
print(f"Homogeneity: {homogeneity}")
Energy: 0.02
Homogeneity: 0.45
Low energy means less uniformity. High homogeneity means similar pixels. These features help in image segmentation.
Applying Filters for Texture
Filters like Gabor enhance texture features. The skimage.filters.gabor
function applies Gabor filters.
from skimage.filters import gabor
# Apply Gabor filter
filt_real, filt_imag = gabor(image, frequency=0.6)
print(filt_real.shape)
(512, 512)
Gabor filters detect edges and textures. They are useful in image analysis.
Conclusion
Python makes texture analysis simple with OpenCV and scikit-image. You can extract features like contrast and energy. These features help in classification and segmentation.
Start experimenting with texture analysis today. Use the code examples to explore more. For more tips, check our guides.