All notes

AI/ML & Data Science / TamuraTexture

Tamura Texture Features in Python

A comprehensive Python implementation of Tamura texture features for image analysis, supporting both NumPy and TensorFlow backends.

October 24, 20254 min readPython1 stars
Open the full technical write-up

A comprehensive Python implementation of Tamura texture features for image analysis, supporting both NumPy and TensorFlow backends.

What this project does

This project implements the six Tamura texture features as described in the original paper:

Tamura, H., Mori, S., & Yamawaki, T. (1978). Textural features corresponding to visual perception. IEEE Transactions on Systems, Man, and Cybernetics, 8(6), 460-473.

Features Implemented

  1. Coarseness - Measures the scale of texture primitives
  2. Contrast - Measures the intensity difference between pixels
  3. Directionality - Measures the degree of directional patterns
  4. Linelikeness - Measures the presence of line-like structures
  5. Regularity - Measures the uniformity of texture patterns
  6. Roughness - Combined measure of coarseness and contrast

Installation

Prerequisites

  • Python 3.7 or higher
  • Windows 10/11 (for the batch file)

Quick Start

  1. Clone or download this repository
  2. Run the setup script:
    run.bat
    
    This will automatically:
    • Create a virtual environment
    • Install all required dependencies
    • Provide an interactive menu for analysis

Manual Installation

  1. Create a virtual environment:

    python -m venv venv
    
  2. Activate the virtual environment:

    # Windows
    venv\Scripts\activate
    
    # Linux/Mac
    source venv/bin/activate
    
  3. Install dependencies:

    pip install -r requirements.txt
    

Usage

Command Line Interface

Basic Usage

python tamura_features.py  [options]

Examples

# Analyze with NumPy backend (default)
python tamura_features.py test.jpg

# Analyze with TensorFlow backend
python tamura_features.py test.jpg --backend tensorflow

# Save results to file
python tamura_features.py test.jpg --output results.txt

# Compare both backends
python tamura_features.py test.jpg --backend numpy --output numpy_results.txt
python tamura_features.py test.jpg --backend tensorflow --output tf_results.txt

Programmatic Usage

from tamura_features import TamuraFeatures

# Initialize with NumPy backend
calculator = TamuraFeatures(backend='numpy')

# Compute all features
features = calculator.compute_all_features('path/to/image.jpg')
print(features)

# Compute individual features
image = calculator.load_image('path/to/image.jpg')
coarseness = calculator.coarseness(image)
contrast = calculator.contrast(image)
# ... etc

Interactive Batch File

Simply double-click run.bat for an interactive experience that:

  • Sets up the environment automatically
  • Provides menu options for different backends
  • Handles test images automatically
  • Offers comparison between backends

Backend Comparison

Feature NumPy Backend TensorFlow Backend
Speed Faster for small images Faster for large images
Memory Lower memory usage Higher memory usage
GPU Support CPU only GPU acceleration available
Dependencies Minimal TensorFlow ecosystem
Precision Slightly different due to implementation Slightly different due to implementation

API Reference

TamuraFeatures Class

Constructor

TamuraFeatures(backend='numpy')
  • backend: 'numpy' or 'tensorflow'

Methods

load_image(image_path: str)`

Load and preprocess an image for analysis.

coarseness(image, kmax=5)`

Compute coarseness feature.

  • kmax: Maximum scale factor (default: 5)
contrast(image)`

Compute contrast feature.

directionality(image)`

Compute directionality feature.

linelikeness(image, theta=0.0, dist=4)`

Compute linelikeness feature.

  • theta: Orientation angle (default: 0.0)
  • dist: Distance parameter (default: 4)
regularity(image, filter_size=64)`

Compute regularity feature.

  • filter_size: Size of the filter (default: 64)
roughness(fcrs, fcon)`

Compute roughness feature.

  • fcrs: Coarseness value
  • fcon: Contrast value
compute_all_features(image_path: str)`

Compute all Tamura features for an image and return as dictionary.

Requirements

  • opencv-python>=4.5.0 - Image processing
  • numpy>=1.21.0 - Numerical computations
  • tensorflow>=2.8.0 - TensorFlow backend (optional)

File Structure

Tamura-In-Python/
├── tamura_features.py    # Main implementation
├── run.bat              # Windows setup and run script
├── requirements.txt     # Python dependencies
├── test.jpg            # Sample test image
├── README.md           # This file
├── .gitignore          # Git ignore rules
└── venv/               # Virtual environment (created by setup)

Performance Notes

  • NumPy Backend: Optimized for CPU processing, faster for smaller images
  • TensorFlow Backend: Can utilize GPU acceleration, better for large images
  • Memory Usage: TensorFlow backend uses more memory due to graph construction
  • Precision: Small numerical differences between backends are expected

Troubleshooting

Common Issues

  1. "ModuleNotFoundError: No module named 'cv2'"

    • Solution: Activate the virtual environment first
    • Run: venv\Scripts\activate (Windows) or source venv/bin/activate (Linux/Mac)
  2. "TensorFlow not available"

    • Solution: Install TensorFlow with pip install tensorflow
    • Or use NumPy backend: --backend numpy
  3. "Image file not found"

    • Solution: Check the image path is correct
    • Ensure the image file exists and is readable
  4. Performance Issues

    • For large images, try TensorFlow backend with GPU
    • For small images, use NumPy backend
    • Consider resizing images if memory is limited

Getting Help

  • Check that all dependencies are installed correctly
  • Ensure Python 3.7+ is being used
  • Verify image file format is supported (JPG, PNG, etc.)
  • Check file permissions for image access

Contributing

This implementation provides a solid foundation for Tamura texture analysis. Potential improvements:

  • Additional texture feature implementations
  • GPU optimization for NumPy backend
  • Batch processing capabilities
  • Visualization tools for feature analysis

License

This project implements academic research from the original Tamura paper. Please cite the original work when using this implementation in research.

Citation

If you use this implementation in your research, please cite:

@article{tamura1978textural,
  title={Textural features corresponding to visual perception},
  author={Tamura, Hideyuki and Mori, Shunji and Yamawaki, Takashi},
  journal={IEEE Transactions on Systems, Man, and Cybernetics},
  volume={8},
  number={6},
  pages={460--473},
  year={1978},
  publisher={IEEE}
}

Related notes

AI/ML & Data Science

AI Lab

A hands-on AI, machine learning, deep learning, computer vision, and data analytics lab built for learners who want practical projects, readable code, and a clear roadmap.

AI/ML & Data Science

CNN

Pure NumPy implementation of a tiny convolutional neural network (CNN) designed for education and experimentation.

AI/ML & Data Science

Emotion Detection Chatbot

An intelligent chatbot system that detects emotions from user text input using advanced Machine Learning, Deep Learning, and Natural Language Processing techniques.