Table of Contents

Class ImageFingerprint

Namespace
LMKit.Media.Image
Assembly
LM-Kit.NET.dll

A compact description of what a picture looks like, made to tell whether two image files show the same picture: a copy saved at another size, in another format, at another compression level or with different metadata keeps the fingerprint of its source, where a hash of the bytes or of the pixels changes entirely.

public sealed class ImageFingerprint : IEquatable<ImageFingerprint>
Inheritance
ImageFingerprint
Implements
Inherited Members

Remarks

The fingerprint is deterministic and needs no model. The picture is averaged down to a fixed square grid of brightness values (every source pixel weighs in, whatever the source size or shape), the grid is taken to the frequency domain, and the fingerprint keeps one bit per low frequency: whether that frequency is present in phase or in counter-phase. Coarse frequencies describe the composition, finer ones the detail that separates two documents drawn on the same template. Color takes no part: a grayscale copy has the fingerprint of its color source.

Two fingerprints are compared with GetSimilarity(ImageFingerprint), which answers 1 for the same picture and 0 for unrelated pictures. A resized or recompressed copy typically answers above 0.85; a crop moves the content on the grid and is not recognized as the same picture.

A fingerprint holds the composition of a picture, not its fine print. Two different pages printed on one form, two charts of one style or two screenshots of one screen look alike down to a few marks and can read as the same picture. FlatShare tells such pictures from photographs: a caller looking for duplicate photographs compares the pictures whose flat share is low and identifies the others by their text.

A fingerprint is a small value (ByteLength bytes) meant to be stored beside the file it describes: ToByteArray() and ToString() give its stable serialized forms, FromByteArray(byte[]) and Parse(string) read them back. The first serialized byte is the Version of the layout; fingerprints of different versions are not comparable.

ImageFingerprint original = ImageFingerprint.Compute(File.ReadAllBytes("photo.jpg"));
ImageFingerprint candidate = ImageFingerprint.Compute(File.ReadAllBytes("photo-small.webp"));

bool photographs = original.FlatShare < 0.6 && candidate.FlatShare < 0.6;

if (photographs && original.GetSimilarity(candidate) >= 0.9) { Console.WriteLine("Same picture."); }

Fields

ByteLength

The length of a serialized fingerprint, in bytes: the version, the flat share, then the bits.

CurrentVersion

The layout version written by this release.

Properties

FlatShare

The share of the picture, from 0 to 1, lying in its single most common tone: low for a photograph (typically under 0.4), high for a page of text, a form, a chart or a screenshot (typically above 0.7), whose paper or background is one tone.

Version

The layout version of this fingerprint. Only fingerprints of the same version are comparable; see IsComparableTo(ImageFingerprint).

Methods

Compute(ImageBuffer)

Computes the fingerprint of decoded pixels, exactly as the buffer holds them. A buffer decoded from a file that states an orientation holds the stored pixels, not the displayed picture: use Compute(byte[]) on the file to honor it.

Compute(byte[])

Computes the fingerprint of an encoded image file, as the picture is displayed: the orientation the file states (EXIF) is applied first, so a photo stored sideways with an orientation tag has the fingerprint of its upright copy.

Compute(byte[], int)

Computes the fingerprint of one page of an encoded image file, as the picture is displayed. See Compute(byte[]).

Equals(ImageFingerprint)
Equals(object)
FromByteArray(byte[])

Reads a fingerprint serialized by ToByteArray().

GetHashCode()
GetSimilarity(ImageFingerprint)

Measures how alike two pictures are, both read as displayed.

GetSimilarity(ImageFingerprint, bool)

Measures how alike two pictures are, optionally whatever way round each one is.

IsComparableTo(ImageFingerprint)

Whether this fingerprint and other can be compared: both exist and carry the same Version.

Parse(string)

Reads a fingerprint written by ToString().

ToByteArray()

Serializes the fingerprint: the Version byte followed by the bits, in a layout that never changes within a version.

ToString()

The fingerprint as lowercase hexadecimal text: the bytes of ToByteArray().

TryParse(string, out ImageFingerprint)

Reads a fingerprint written by ToString(), without throwing.

Share