Coder Social home page Coder Social logo

phash's Introduction

priHash

C# Implementation of pHash (http://phash.org). Based on phash-0.9.4 for Windows.

NuGet packages

The Shipwreck.Phash accepts only IByteImage interface. The package does not contain any method to load an image. So additional packages provide extension methods to instantiate ByteImage

There are some more packages for uncommon usage.

Compatibility Notice

Following methods contains breaking changes at versions below. Hashes computed by older versions cannot be compared with the new one:

  • ImagePhash.ComputeDctHash: at 0.5.0.

Hashing an image (Bitmap)

var bitmap = (Bitmap)Image.FromFile(fullPathToImage);
var hash = ImagePhash.ComputeDigest(bitmap.ToLuminanceImage());

Hashing an image (BitmapSource)

var bitmapSource = BitmapFrame.Create(stream);
var hash = ImagePhash.ComputeDigest(bitmapSource.ToLuminanceImage());

Image similarity score

var score = ImagePhash.GetCrossCorrelation(hash1, hash2);

Multithreaded hashing of all images in a folder

Example below required .NET 4.7+ since the function returns a tuple of results.

public static (ConcurrentDictionary<string, Digest> filePathsToHashes, ConcurrentDictionary<Digest, HashSet<string>> hashesToFiles) GetHashes(string dirPath, string searchPattern)
{
    var filePathsToHashes = new ConcurrentDictionary<string, Digest>();
    var hashesToFiles = new ConcurrentDictionary<Digest, HashSet<string>>();

    var files = Directory.GetFiles(dirPath, searchPattern);

    Parallel.ForEach(files, (currentFile) =>
    {
        var bitmap = (Bitmap)Image.FromFile(currentFile);
        var hash = ImagePhash.ComputeDigest(bitmap);
        filePathsToHashes[currentFile] = hash;

        HashSet<string> currentFilesForHash;

        lock (hashesToFiles)
        {
            if (!hashesToFiles.TryGetValue(hash, out currentFilesForHash))
            {
                currentFilesForHash = new HashSet<string>();
                hashesToFiles[hash] = currentFilesForHash;
            }
        }

        lock (currentFilesForHash)
        {
            currentFilesForHash.Add(currentFile);
        }
    });

    return (filePathsToHashes, hashesToFiles);
}

Then you can call it like this:

(ConcurrentDictionary<string, Digest> gilePathsToHashes, ConcurrentDictionary<Digest, HashSet<string>> hashesToFiles) =
    GetHashes(
        dirPath: @"C:\some\path\",
        searchPattern: "*.jpg");

TestApp

Download Image sets from http://phash.org/download/ and extract into the /data/compr, /data/blur, /data/rotd, /data/misc directories. Or you can create test Image sets by yourself.

License

GNU General Public License version 3 or later http://www.gnu.org/licenses/

phash's People

Contributors

pgrho avatar thatrubenaguilar avatar zmarty avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.