Skip to Content
Documentation@blazediff/ssim

@blazediff/ssim

Fast SSIM (Structural Similarity Index) implementations for perceptual image quality assessment. Includes standard SSIM, MS-SSIM (Multi-Scale SSIM), and Hitchhiker’s SSIM for various use cases and performance requirements.

Installation

npm install @blazediff/ssim

Features

  • Three SSIM Variants - Standard SSIM, MS-SSIM, and Hitchhiker’s SSIM
  • Perceptual metric - Measures structural similarity, not just pixel differences
  • MATLAB-compatible - Matches reference implementation with <0.01% error
  • High performance - Optimized implementations with ~4x faster Hitchhiker’s SSIM
  • SSIM map output - Optional grayscale visualization of similarity
  • TypeScript support out of the box

What is SSIM?

SSIM (Structural Similarity Index) is a perceptual image quality metric that:

  • Measures structural similarity between images (luminance, contrast, structure)
  • Returns a score from 0 to 1 (1 = identical, 0 = completely different)
  • Correlates better with human perception than MSE or PSNR
  • Is widely used in image/video quality assessment

Variants

  • SSIM - Standard Gaussian-based implementation, MATLAB-compatible
  • MS-SSIM - Multi-scale version for better perceptual correlation
  • Hitchhiker’s SSIM - Fast rectangular-window version using integral images (~4x faster)

For detailed mathematical explanation, see the package documentation .

API Reference

ssim(image1, image2, output, width, height, options?)

Compares two images using standard SSIM metric and returns a similarity score.

Parameters

ParameterTypeDescription
image1Buffer, Uint8Array, or Uint8ClampedArrayImage data of the first image
image2Buffer, Uint8Array, or Uint8ClampedArrayImage data of the second image
outputBuffer, Uint8Array, Uint8ClampedArray, or undefinedOptional output buffer for SSIM map visualization
widthnumberWidth of the images in pixels
heightnumberHeight of the images in pixels
optionsSsimOptionsComparison options (optional)
Options
OptionTypeDefaultDescription
windowSizenumber11Size of the Gaussian window
k1number0.01Algorithm parameter for luminance
k2number0.03Algorithm parameter for contrast
Lnumber255Dynamic range of pixel values
Returns

number - SSIM score between 0 and 1

Score Interpretation

Score RangeSimilarity LevelDescription
1.0IdenticalImages are identical or perceptually identical
0.99+ExcellentExtremely high similarity (minor compression artifacts)
0.95-0.99Very GoodHigh similarity (small compression or noise)
0.90-0.95GoodNoticeable but acceptable differences
0.80-0.90FairSignificant but tolerable differences
<0.80PoorMajor structural differences

Threshold Guidelines: - Use threshold >0.99 for strict visual regression testing - Use threshold >0.95 for standard visual regression testing - Scores below 0.90 indicate substantial visual differences

Usage Examples

import ssim from "@blazediff/ssim/ssim"; // Basic usage const score = ssim(img1.data, img2.data, undefined, width, height); // With SSIM map output const output = new Uint8ClampedArray(width * height * 4); const score = ssim(img1.data, img2.data, output, width, height); // With custom options const score = ssim(img1.data, img2.data, undefined, width, height, { windowSize: 8, k1: 0.01, k2: 0.03, });

When to Use Each Variant

Use SSIM when:

  • You need MATLAB-compatible results for research or comparison
  • You want high accuracy with Gaussian weighting
  • You need automatic downsampling for large images
  • Performance is not critical

Use MS-SSIM when:

  • You need multi-scale analysis for better perceptual correlation
  • You’re working with images at different resolutions
  • You want better correlation with human perception
  • You can afford the ~2-3x computation cost

Use Hitchhiker’s SSIM when:

  • You need maximum performance (~4x faster than standard SSIM)
  • You’re processing large images or many images
  • You want O(1) window computation regardless of window size
  • You need flexible window stride for overlapping/non-overlapping windows
  • CoV pooling provides better perceptual correlation than mean

CLI Usage

All three variants are available via the @blazediff/bin CLI:

# Standard SSIM blazediff ssim image1.png image2.png # MS-SSIM blazediff msssim image1.png image2.png # Hitchhiker's SSIM blazediff hitchhikers-ssim image1.png image2.png # With options blazediff hitchhikers-ssim image1.png image2.png --window-size 16 --no-cov-pooling
Last updated on