| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- using SixLabors.ImageSharp;
- using SixLabors.ImageSharp.Formats;
- using SixLabors.ImageSharp.PixelFormats;
- using SixLabors.ImageSharp.Processing;
- using SixLabors.ImageSharp.Processing.Processors.Transforms;
- using System.IO;
- namespace Masuit.Tools.Media;
- /// <summary>
- /// 使用ImageSharp进行图像变换
- /// </summary>
- public class ImageSharpTransformer : IImageTransformer
- {
- #if NET6_0_OR_GREATER
- public byte[] TransformImage(Stream stream, int width, int height)
- {
- var decoderOptions = new DecoderOptions
- {
- TargetSize = new Size(160),
- SkipMetadata = true,
- };
- using var image = Image.Load<L8>(decoderOptions, stream);
- image.Mutate(x => x.Resize(new ResizeOptions()
- {
- Size = new Size
- {
- Width = width,
- Height = height
- },
- Mode = ResizeMode.Stretch,
- Sampler = new BicubicResampler()
- }));
- image.DangerousTryGetSinglePixelMemory(out var pixelSpan);
- var pixelArray = pixelSpan.ToArray();
- var pixelCount = width * height;
- var bytes = new byte[pixelCount];
- for (var i = 0; i < pixelCount; i++)
- {
- bytes[i] = pixelArray[i].PackedValue;
- }
- return bytes;
- }
- #else
- public byte[] TransformImage(Stream stream, int width, int height)
- {
- using var image = Image.Load<L8>(stream);
- image.Mutate(x => x.Resize(new ResizeOptions()
- {
- Size = new Size
- {
- Width = width,
- Height = height
- },
- Mode = ResizeMode.Stretch,
- Sampler = new BicubicResampler()
- }));
- image.DangerousTryGetSinglePixelMemory(out var pixelSpan);
- var pixelArray = pixelSpan.ToArray();
- var pixelCount = width * height;
- var bytes = new byte[pixelCount];
- for (var i = 0; i < pixelCount; i++)
- {
- bytes[i] = pixelArray[i].PackedValue;
- }
- return bytes;
- }
- #endif
- public byte[] TransformImage(Image<L8> image, int width, int height)
- {
- using var clone = image.Clone(x => x.Resize(new ResizeOptions()
- {
- Size = new Size
- {
- Width = width,
- Height = height
- },
- Mode = ResizeMode.Stretch,
- Sampler = new BicubicResampler()
- }));
- clone.DangerousTryGetSinglePixelMemory(out var pixelSpan);
- var pixelArray = pixelSpan.ToArray();
- var pixelCount = width * height;
- var bytes = new byte[pixelCount];
- for (var i = 0; i < pixelCount; i++)
- {
- bytes[i] = pixelArray[i].PackedValue;
- }
- return bytes;
- }
- public byte[,] GetPixelData(Image<L8> image, int width, int height)
- {
- using var clone = image.Clone(x => x.Resize(new ResizeOptions()
- {
- Size = new Size
- {
- Width = width,
- Height = height
- },
- Mode = ResizeMode.Stretch,
- Sampler = new BicubicResampler()
- }));
- var grayscalePixels = new byte[width, height];
- clone.ProcessPixelRows(accessor =>
- {
- for (int y = 0; y < width; y++)
- {
- var row = accessor.GetRowSpan(y);
- for (int x = 0; x < height; x++)
- {
- var pixel = row[x];
- grayscalePixels[y, x] = pixel.PackedValue;
- }
- }
- });
- return grayscalePixels;
- }
- }
- public static class ImageHashExt
- {
- private static readonly ImageHasher Hasher = new();
- /// <summary>
- /// 使用平均值算法计算图像的64位哈希
- /// </summary>
- /// <param name="image">读取到的图片流</param>
- /// <returns>64位hash值</returns>
- public static ulong AverageHash64(this Image image)
- {
- return Hasher.AverageHash64(image);
- }
- /// <summary>
- /// 使用平均值算法计算图像的64位哈希
- /// </summary>
- /// <param name="image">读取到的图片流</param>
- /// <returns>64位hash值</returns>
- public static ulong AverageHash64(this Image<L8> image)
- {
- return Hasher.AverageHash64(image);
- }
- /// <summary>
- /// 使用中值算法计算给定图像的64位哈希
- /// 将图像转换为8x8灰度图像,从中查找中值像素值,然后在结果哈希中将值大于中值的所有像素标记为1。与基于平均值的实现相比,更能抵抗非线性图像编辑。
- /// </summary>
- /// <param name="image">读取到的图片流</param>
- /// <returns>64位hash值</returns>
- public static ulong MedianHash64(this Image image)
- {
- return Hasher.MedianHash64(image);
- }
- /// <summary>
- /// 使用中值算法计算给定图像的64位哈希
- /// 将图像转换为8x8灰度图像,从中查找中值像素值,然后在结果哈希中将值大于中值的所有像素标记为1。与基于平均值的实现相比,更能抵抗非线性图像编辑。
- /// </summary>
- /// <param name="image">读取到的图片流</param>
- /// <returns>64位hash值</returns>
- public static ulong MedianHash64(this Image<L8> image)
- {
- return Hasher.MedianHash64(image);
- }
- /// <summary>
- /// 使用中值算法计算给定图像的256位哈希
- /// 将图像转换为16x16的灰度图像,从中查找中值像素值,然后在结果哈希中将值大于中值的所有像素标记为1。与基于平均值的实现相比,更能抵抗非线性图像编辑。
- /// </summary>
- /// <param name="image">读取到的图片流</param>
- /// <returns>256位hash值,生成一个4长度的数组返回</returns>
- public static ulong[] MedianHash256(this Image image)
- {
- return Hasher.MedianHash256(image);
- }
- /// <summary>
- /// 使用中值算法计算给定图像的256位哈希
- /// 将图像转换为16x16的灰度图像,从中查找中值像素值,然后在结果哈希中将值大于中值的所有像素标记为1。与基于平均值的实现相比,更能抵抗非线性图像编辑。
- /// </summary>
- /// <param name="image">读取到的图片流</param>
- /// <returns>256位hash值,生成一个4长度的数组返回</returns>
- public static ulong[] MedianHash256(this Image<L8> image)
- {
- return Hasher.MedianHash256(image);
- }
- /// <summary>
- /// 使用差分哈希算法计算图像的64位哈希。
- /// </summary>
- /// <see cref="https://segmentfault.com/a/1190000038308093"/>
- /// <param name="image">读取到的图片流</param>
- /// <returns>64位hash值</returns>
- public static ulong DifferenceHash64(this Image image)
- {
- return Hasher.DifferenceHash64(image);
- }
- /// <summary>
- /// 使用差分哈希算法计算图像的64位哈希。
- /// </summary>
- /// <see cref="https://segmentfault.com/a/1190000038308093"/>
- /// <param name="image">读取到的图片流</param>
- /// <returns>64位hash值</returns>
- public static ulong DifferenceHash64(this Image<L8> image)
- {
- return Hasher.DifferenceHash64(image);
- }
- /// <summary>
- /// 使用差分哈希算法计算图像的64位哈希。
- /// </summary>
- /// <see cref="https://segmentfault.com/a/1190000038308093"/>
- /// <param name="image">读取到的图片流</param>
- /// <returns>256位hash值</returns>
- public static ulong[] DifferenceHash256(this Image image)
- {
- return Hasher.DifferenceHash256(image);
- }
- /// <summary>
- /// 使用差分哈希算法计算图像的64位哈希。
- /// </summary>
- /// <see cref="https://segmentfault.com/a/1190000038308093"/>
- /// <param name="image">读取到的图片流</param>
- /// <returns>256位hash值</returns>
- public static ulong[] DifferenceHash256(this Image<L8> image)
- {
- return Hasher.DifferenceHash256(image);
- }
- /// <summary>
- /// 使用32分辨率精度DCT算法计算图像的64位哈希
- /// </summary>
- /// <see cref="https://segmentfault.com/a/1190000038308093"/>
- /// <param name="image">读取到的图片流</param>
- /// <returns>64位hash值</returns>
- public static ulong DctHash(this Image image)
- {
- return Hasher.DctHash(image);
- }
- /// <summary>
- /// 使用32分辨率精度DCT算法计算图像的64位哈希
- /// </summary>
- /// <see cref="https://segmentfault.com/a/1190000038308093"/>
- /// <param name="image">读取到的图片流</param>
- /// <returns>64位hash值</returns>
- public static ulong DctHash(this Image<L8> image)
- {
- return Hasher.DctHash(image);
- }
- /// <summary>
- /// 使用64分辨率精度DCT算法计算图像的64位哈希
- /// </summary>
- /// <see cref="https://segmentfault.com/a/1190000038308093"/>
- /// <param name="image">读取到的图片流</param>
- /// <returns>64位hash值</returns>
- public static ulong DctHash64(this Image image)
- {
- return Hasher.DctHash64(image);
- }
- /// <summary>
- /// 使用64分辨率精度DCT算法计算图像的64位哈希
- /// </summary>
- /// <see cref="https://segmentfault.com/a/1190000038308093"/>
- /// <param name="image">读取到的图片流</param>
- /// <returns>64位hash值</returns>
- public static ulong DctHash64(this Image<L8> image)
- {
- return Hasher.DctHash64(image);
- }
- /// <summary>
- /// 使用汉明距离比较两幅图像的哈希值。结果1表示图像完全相同,而结果0表示图像完全不同。
- /// </summary>
- /// <param name="image1">图像1</param>
- /// <param name="image2">图像2</param>
- /// <param name="algorithm">对比算法</param>
- /// <returns>相似度范围:[0,1]</returns>
- public static float Compare(this Image image1, Image image2, ImageHashAlgorithm algorithm = ImageHashAlgorithm.Difference)
- {
- return algorithm switch
- {
- ImageHashAlgorithm.Average => ImageHasher.Compare(Hasher.AverageHash64(image1), Hasher.AverageHash64(image2)),
- ImageHashAlgorithm.Medium => ImageHasher.Compare(Hasher.MedianHash256(image1), Hasher.MedianHash256(image2)),
- ImageHashAlgorithm.Difference => ImageHasher.Compare(Hasher.DifferenceHash256(image1), Hasher.DifferenceHash256(image2)),
- ImageHashAlgorithm.DCT32 => ImageHasher.Compare(Hasher.DctHash(image1), Hasher.DctHash(image2)),
- ImageHashAlgorithm.DCT64 => ImageHasher.Compare(Hasher.DctHash64(image1), Hasher.DctHash64(image2)),
- _ => throw new ArgumentOutOfRangeException(nameof(algorithm), algorithm, null)
- };
- }
- /// <summary>
- /// 使用汉明距离比较两幅图像的哈希值。结果1表示图像完全相同,而结果0表示图像完全不同。
- /// </summary>
- /// <param name="image1">图像1</param>
- /// <param name="image2">图像2</param>
- /// <param name="algorithm">对比算法</param>
- /// <returns>相似度范围:[0,1]</returns>
- public static float Compare(this Image<L8> image1, Image<L8> image2, ImageHashAlgorithm algorithm = ImageHashAlgorithm.Difference)
- {
- return algorithm switch
- {
- ImageHashAlgorithm.Average => ImageHasher.Compare(Hasher.AverageHash64(image1), Hasher.AverageHash64(image2)),
- ImageHashAlgorithm.Medium => ImageHasher.Compare(Hasher.MedianHash256(image1), Hasher.MedianHash256(image2)),
- ImageHashAlgorithm.Difference => ImageHasher.Compare(Hasher.DifferenceHash256(image1), Hasher.DifferenceHash256(image2)),
- ImageHashAlgorithm.DCT32 => ImageHasher.Compare(Hasher.DctHash(image1), Hasher.DctHash(image2)),
- ImageHashAlgorithm.DCT64 => ImageHasher.Compare(Hasher.DctHash64(image1), Hasher.DctHash64(image2)),
- _ => throw new ArgumentOutOfRangeException(nameof(algorithm), algorithm, null)
- };
- }
- /// <summary>
- /// 使用汉明距离比较两幅图像的哈希值。结果1表示图像完全相同,而结果0表示图像完全不同。
- /// </summary>
- /// <param name="image1">图像1的hash</param>
- /// <param name="image2path">图像2的路径</param>
- /// <param name="algorithm">对比算法</param>
- /// <returns>相似度范围:[0,1]</returns>
- public static float Compare(this Image image1, string image2path, ImageHashAlgorithm algorithm = ImageHashAlgorithm.Difference)
- {
- return algorithm switch
- {
- ImageHashAlgorithm.Average => ImageHasher.Compare(Hasher.AverageHash64(image1), Hasher.AverageHash64(image2path)),
- ImageHashAlgorithm.Medium => ImageHasher.Compare(Hasher.MedianHash256(image1), Hasher.MedianHash256(image2path)),
- ImageHashAlgorithm.Difference => ImageHasher.Compare(Hasher.DifferenceHash256(image1), Hasher.DifferenceHash256(image2path)),
- ImageHashAlgorithm.DCT32 => ImageHasher.Compare(Hasher.DctHash(image1), Hasher.DctHash(image2path)),
- ImageHashAlgorithm.DCT64 => ImageHasher.Compare(Hasher.DctHash64(image1), Hasher.DctHash64(image2path)),
- _ => throw new ArgumentOutOfRangeException(nameof(algorithm), algorithm, null)
- };
- }
- /// <summary>
- /// 使用汉明距离比较两幅图像的哈希值。结果1表示图像完全相同,而结果0表示图像完全不同。
- /// </summary>
- /// <param name="image1">图像1的hash</param>
- /// <param name="image2path">图像2的路径</param>
- /// <param name="algorithm">对比算法</param>
- /// <returns>相似度范围:[0,1]</returns>
- public static float Compare(this Image<L8> image1, string image2path, ImageHashAlgorithm algorithm = ImageHashAlgorithm.Difference)
- {
- return algorithm switch
- {
- ImageHashAlgorithm.Average => ImageHasher.Compare(Hasher.AverageHash64(image1), Hasher.AverageHash64(image2path)),
- ImageHashAlgorithm.Medium => ImageHasher.Compare(Hasher.MedianHash256(image1), Hasher.MedianHash256(image2path)),
- ImageHashAlgorithm.Difference => ImageHasher.Compare(Hasher.DifferenceHash256(image1), Hasher.DifferenceHash256(image2path)),
- ImageHashAlgorithm.DCT32 => ImageHasher.Compare(Hasher.DctHash(image1), Hasher.DctHash(image2path)),
- ImageHashAlgorithm.DCT64 => ImageHasher.Compare(Hasher.DctHash64(image1), Hasher.DctHash64(image2path)),
- _ => throw new ArgumentOutOfRangeException(nameof(algorithm), algorithm, null)
- };
- }
- }
- public enum ImageHashAlgorithm
- {
- /// <summary>
- /// 均值算法
- /// </summary>
- Average,
- /// <summary>
- /// 中值算法
- /// </summary>
- Medium,
- /// <summary>
- /// 差异算法
- /// </summary>
- Difference,
- /// <summary>
- /// 32分辨率精度DCT算法
- /// </summary>
- DCT32,
- /// <summary>
- /// 64分辨率精度DCT算法
- /// </summary>
- DCT64,
- }
|