ImageHash .NET Library

repository·develop·Indexed 42 days ago

https://github.com/coenm/imagehash

A .NET Standard library for identifying visually similar images using perceptual hashing algorithms. It implements AverageHash, PerceptualHash, and DifferenceHash, relying on ImageSharp for image processing to calculate hashes and determine similarity scores.

Tokens
571
Snippets
2
Records
4
Agent score
4%

What's inside ImageHash

  1. Overview of ImageHash

    develop

    ImageHash is a .NET Standard library used to calculate perceptual hashes of images and determine image similarity. Unlike cryptographic hashes, perceptual hashes produce similar outputs for visually similar inputs, making them ideal for finding duplicate or near-duplicate images.

    This library relies on ImageSharp for image processing.

  2. Calculate image similarity

    develop

    To determine how similar two images are, calculate their hashes using the same hash algorithm, then use CompareHash.Similarity to get a similarity score.

    Note: Both hashes must be generated by the same algorithm (e.g., both AverageHash) for the comparison to be valid.

    // calculate the two image hashes
    ulong hash1 = hashAlgorithm.Hash(imageStream1);
    ulong hash2 = hashAlgorithm.Hash(imageStream2);
    
    double percentageImageSimilarity = CompareHash.Similarity(hash1, hash2);
  3. Calculate an image hash

    develop

    To generate a hash for an image, instantiate one of the available hash algorithm classes (e.g., AverageHash, DifferenceHash, or PerceptualHash) and call the .Hash(Stream) method. The method accepts a Stream containing the image data and returns a ulong representing the perceptual hash.

    var hashAlgorithm = new AverageHash();
    // or one of the other available algorithms:
    // var hashAlgorithm = new DifferenceHash();
    // var hashAlgorithm = new PerceptualHash();
    
    string filename = "your filename";
    using var stream = File.OpenRead(filename);
    
    ulong imageHash = hashAlgorithm.Hash(stream);
  4. Available Hash Algorithms

    develop

    The library implements three primary perceptual hashing algorithms:

    • AverageHash: Implemented by Dr. Neal Krawetz.
    • PerceptualHash: Implemented by Dr. Neal Krawetz.
    • DifferenceHash: Implemented by David Oftedal with improvements by Dr. Neal Krawetz.