https://github.com/pgrho/phash
C# Implementation of pHash
https://github.com/pgrho/phash
phash
Last synced: 5 months ago
JSON representation
C# Implementation of pHash
- Host: GitHub
- URL: https://github.com/pgrho/phash
- Owner: pgrho
- License: gpl-3.0
- Created: 2016-12-07T13:59:06.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2022-12-07T18:38:04.000Z (over 3 years ago)
- Last Synced: 2025-12-06T01:49:38.500Z (6 months ago)
- Topics: phash
- Language: C#
- Homepage:
- Size: 1.82 MB
- Stars: 79
- Watchers: 8
- Forks: 11
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# priHash #
C# Implementation of pHash ().
Based on phash-0.9.4 for Windows.
## NuGet packages ##
- [Shipwreck.Phash](https://www.nuget.org/packages/Shipwreck.Phash/) - C# Implementation of phash-0.9.4.
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`
- [Shipwreck.Phash.Bitmaps](https://www.nuget.org/packages/Shipwreck.Phash.Bitmaps/) - for System.Drawing.dll including Mono implementation.
- [Shipwreck.Phash.PresentationCore](https://www.nuget.org/packages/Shipwreck.Phash.PresentationCore/) - for PresentationCore.dll. Used for WPF, ASP.NET or Windows Service.
There are some more packages for uncommon usage.
- [Shipwreck.Phash.Data](https://www.nuget.org/packages/Shipwreck.Phash.Data/) - Provides Stored Function Implementations for pHash Digests
- [Shipwreck.Phash.CrossCorrelation](https://www.nuget.org/packages/Shipwreck.Phash.CrossCorrelation/) - C# Implementation of phash-0.9.4 that provides Only GetCrossCorrelation functionality. Intended to be referenced from SQL CLR.
## 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) ##
```C#
var bitmap = (Bitmap)Image.FromFile(fullPathToImage);
var hash = ImagePhash.ComputeDigest(bitmap.ToLuminanceImage());
```
## Hashing an image (BitmapSource) ##
```C#
var bitmapSource = BitmapFrame.Create(stream);
var hash = ImagePhash.ComputeDigest(bitmapSource.ToLuminanceImage());
```
## Image similarity score ##
```C#
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.
```C#
public static (ConcurrentDictionary filePathsToHashes, ConcurrentDictionary> hashesToFiles) GetHashes(string dirPath, string searchPattern)
{
var filePathsToHashes = new ConcurrentDictionary();
var hashesToFiles = new ConcurrentDictionary>();
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 currentFilesForHash;
lock (hashesToFiles)
{
if (!hashesToFiles.TryGetValue(hash, out currentFilesForHash))
{
currentFilesForHash = new HashSet();
hashesToFiles[hash] = currentFilesForHash;
}
}
lock (currentFilesForHash)
{
currentFilesForHash.Add(currentFile);
}
});
return (filePathsToHashes, hashesToFiles);
}
```
Then you can call it like this:
```C#
(ConcurrentDictionary gilePathsToHashes, ConcurrentDictionary> hashesToFiles) =
GetHashes(
dirPath: @"C:\some\path\",
searchPattern: "*.jpg");
```
## TestApp ##
Download Image sets from 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