Skip to content

its-Sohan/Fractal_Analysis

Repository files navigation

Fractal Analysis for AI Image Detection

This project implements a method for detecting AI-generated images by analyzing the fractal properties of the noise residual in the frequency domain.

🎯 What This Project Does

This tool analyzes images to determine if they are:

  • Real photographs (captured by cameras)
  • AI-generated images (created by AI models like DALL-E, Midjourney, Stable Diffusion)

🔍 Core Scientific Concept

Why does this work?

Natural photographs have distinctive noise patterns that AI generators don't perfectly replicate:

  1. Real images have noise that follows a power-law distribution with spectral exponent β ≈ 1.5-2.5
  2. AI-generated images often have "too clean" or "too uniform" noise patterns with β outside this range

The algorithm:

  1. Removes the "main content" (edges, structures) using bilateral filtering
  2. Analyzes the remaining "noise residual" in the frequency domain
  3. Measures how quickly power decreases with frequency (spectral exponent β)
  4. Converts this to a Fractal Dimension (D)
  5. Classifies based on whether these values match natural patterns

📊 Understanding the Results

Key Metrics

Metric Description Real Image Range AI-Generated Range
β (beta) Spectral exponent 1.5 - 2.5 < 1.5 or > 2.5
D (Fractal Dimension) Fractal complexity 2.0 - 3.0 < 2.0 or > 3.0
Confidence Classification certainty 50-100% 50-100%

Example Results

✅ Real Image (example: photo.jpg):
   Beta: 1.85
   Fractal Dimension: 2.52
   Prediction: Real (confidence: 95%)

❌ AI-Generated Image (example: ai_art.png):
   Beta: 1.25
   Fractal Dimension: 2.75
   Prediction: AI-generated (confidence: 82%)

🛠️ Installation

python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt

🚀 Quick Start

1. Test with a single image

# Activate environment first
source venv/bin/activate

# Set Python path
export PYTHONPATH=$PYTHONPATH:$(pwd)/src

# Run analysis
python3 main.py data/image.jpeg --out results/

2. Process all images in a folder

python3 main.py --dir data/ --out results/

3. View results

Look in the results/ folder for:

  • {filename}_residual.png - The noise residual visualization
  • {filename}_spectrum.png - Log-log PSD plot with fit line

📈 Interpreting the Plots

Noise Residual Image

  • Shows the "noise" that remains after removing main image content
  • Real images have complex, irregular patterns
  • AI images may look smoother or have artifacts

Log-Log PSD Plot

  • X-axis: Spatial frequency (low to high)
  • Y-axis: Power at each frequency
  • Green dashed line: Linear fit
  • Slope ≈ -β: The steeper the slope, the more "natural" the noise

If the data points follow a straight line, your image has fractal noise characteristics (likely real).

🔧 Advanced Usage

Customize filter parameters

Different images may need different bilateral filter settings:

python3 main.py image.jpg --d 15 --sigma-color 30 --sigma-space 25

Disable classification (metrics only)

python3 main.py image.jpg --no-classify

Evaluate against ground truth

Create a CSV file labels.csv:

photo1.jpg,Real
photo2.jpg,Real
ai_art1.png,AI-generated
ai_art2.png,AI-generated

Then run:

python3 main.py --dir data/ --ground-truth labels.csv

🧪 Python API Usage

from fractal_analysis import (
    load_grayscale_image,
    adaptive_gamma_correction,
    denoise_image,
    get_noise_residual,
    compute_spectrum,
    calculate_fractal_spectrum,
    classify_image
)

# Load and process image
img = load_grayscale_image("my_image.jpg")
img_gamma = adaptive_gamma_correction(img)
denoised = denoise_image(img_gamma)
residual = get_noise_residual(img_gamma, denoised)

# Analyze fractal properties
spectrum = compute_spectrum(residual)
freqs, psd1D, beta, D = calculate_fractal_spectrum(spectrum, img.shape)

# Classify
prediction, confidence = classify_image(beta, D)
print(f"Prediction: {prediction} ({confidence:.1%})")

📁 Project Structure

Fractal_Analysis/
├── src/fractal_analysis/
│   ├── __init__.py         # Package exports
│   ├── preprocessing.py    # Gamma correction, denoising
│   ├── analysis.py         # FFT, fractal calculations
│   ├── utils.py            # Image I/O
│   └── classifier.py       # Classification logic
├── main.py                 # CLI entry point
├── data/                   # Input images
├── results/                # Output files
└── requirements.txt        # Dependencies

⚠️ Important Notes

  1. Not 100% accurate: This is a heuristic method. Some AI images may be classified as real, and some real images with unusual noise may be classified as AI-generated.

  2. Image quality matters: Very noisy or low-quality images may give unreliable results.

  3. Multiple factors: AI detection is complex. This method examines just one aspect (noise patterns).

  4. Calibrate for your use case: Use calibrate_thresholds() with your own labeled data for best results.

🧬 Scientific Background

This method is based on research showing that:

  • Natural images have fractal characteristics in their noise
  • AI generators often produce images with different statistical properties
  • Frequency domain analysis reveals these differences

Key papers:

  • "Detecting AI-generated images using frequency domain features"
  • "Fractal analysis for digital image forensics"

📚 Next Steps

  1. Test with your own images
  2. Collect labeled data (real vs AI-generated)
  3. Calibrate thresholds using calibrate_thresholds()
  4. Combine with other detection methods for better accuracy

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors