ImageSharpTransformer.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. using SixLabors.ImageSharp;
  2. using SixLabors.ImageSharp.Formats;
  3. using SixLabors.ImageSharp.PixelFormats;
  4. using SixLabors.ImageSharp.Processing;
  5. using SixLabors.ImageSharp.Processing.Processors.Transforms;
  6. using System.IO;
  7. namespace Masuit.Tools.Media;
  8. /// <summary>
  9. /// 使用ImageSharp进行图像变换
  10. /// </summary>
  11. public class ImageSharpTransformer : IImageTransformer
  12. {
  13. #if NET6_0_OR_GREATER
  14. public byte[] TransformImage(Stream stream, int width, int height)
  15. {
  16. var decoderOptions = new DecoderOptions
  17. {
  18. TargetSize = new Size(144),
  19. SkipMetadata = true,
  20. };
  21. using var image = Image.Load<L8>(decoderOptions, stream);
  22. return TransformImage(image, width, height);
  23. }
  24. #else
  25. public byte[] TransformImage(Stream stream, int width, int height)
  26. {
  27. using var image = Image.Load<L8>(stream);
  28. return TransformImage(image, width, height);
  29. }
  30. #endif
  31. public byte[] TransformImage(Image<L8> image, int width, int height)
  32. {
  33. image.Mutate(x => x.Resize(new ResizeOptions()
  34. {
  35. Size = new Size
  36. {
  37. Width = width,
  38. Height = height
  39. },
  40. Mode = ResizeMode.Stretch,
  41. Sampler = new BicubicResampler()
  42. }));
  43. image.DangerousTryGetSinglePixelMemory(out var pixelSpan);
  44. var pixelArray = pixelSpan.ToArray();
  45. var pixelCount = width * height;
  46. var bytes = new byte[pixelCount];
  47. for (var i = 0; i < pixelCount; i++)
  48. {
  49. bytes[i] = pixelArray[i].PackedValue;
  50. }
  51. return bytes;
  52. }
  53. public byte[,] GetPixelData(Image<L8> image, int width, int height)
  54. {
  55. image.Mutate(x => x.Resize(new ResizeOptions()
  56. {
  57. Size = new Size
  58. {
  59. Width = width,
  60. Height = height
  61. },
  62. Mode = ResizeMode.Stretch,
  63. Sampler = new BicubicResampler()
  64. }));
  65. var grayscalePixels = new byte[width, height];
  66. image.ProcessPixelRows(accessor =>
  67. {
  68. for (int y = 0; y < width; y++)
  69. {
  70. var row = accessor.GetRowSpan(y);
  71. for (int x = 0; x < height; x++)
  72. {
  73. var pixel = row[x];
  74. grayscalePixels[y, x] = pixel.PackedValue;
  75. }
  76. }
  77. });
  78. return grayscalePixels;
  79. }
  80. }
  81. public static class ImageHashExt
  82. {
  83. /// <summary>
  84. /// 使用平均值算法计算图像的64位哈希
  85. /// </summary>
  86. /// <param name="image">读取到的图片流</param>
  87. /// <returns>64位hash值</returns>
  88. public static ulong AverageHash64(this Image image)
  89. {
  90. var hasher = new ImageHasher();
  91. return hasher.AverageHash64(image);
  92. }
  93. /// <summary>
  94. /// 使用平均值算法计算图像的64位哈希
  95. /// </summary>
  96. /// <param name="image">读取到的图片流</param>
  97. /// <returns>64位hash值</returns>
  98. public static ulong AverageHash64(this Image<L8> image)
  99. {
  100. var hasher = new ImageHasher();
  101. return hasher.AverageHash64(image);
  102. }
  103. /// <summary>
  104. /// 使用中值算法计算给定图像的64位哈希
  105. /// 将图像转换为8x8灰度图像,从中查找中值像素值,然后在结果哈希中将值大于中值的所有像素标记为1。与基于平均值的实现相比,更能抵抗非线性图像编辑。
  106. /// </summary>
  107. /// <param name="image">读取到的图片流</param>
  108. /// <returns>64位hash值</returns>
  109. public static ulong MedianHash64(this Image image)
  110. {
  111. var hasher = new ImageHasher();
  112. return hasher.MedianHash64(image);
  113. }
  114. /// <summary>
  115. /// 使用中值算法计算给定图像的64位哈希
  116. /// 将图像转换为8x8灰度图像,从中查找中值像素值,然后在结果哈希中将值大于中值的所有像素标记为1。与基于平均值的实现相比,更能抵抗非线性图像编辑。
  117. /// </summary>
  118. /// <param name="image">读取到的图片流</param>
  119. /// <returns>64位hash值</returns>
  120. public static ulong MedianHash64(this Image<L8> image)
  121. {
  122. var hasher = new ImageHasher();
  123. return hasher.MedianHash64(image);
  124. }
  125. /// <summary>
  126. /// 使用中值算法计算给定图像的256位哈希
  127. /// 将图像转换为16x16的灰度图像,从中查找中值像素值,然后在结果哈希中将值大于中值的所有像素标记为1。与基于平均值的实现相比,更能抵抗非线性图像编辑。
  128. /// </summary>
  129. /// <param name="image">读取到的图片流</param>
  130. /// <returns>256位hash值,生成一个4长度的数组返回</returns>
  131. public static ulong[] MedianHash256(this Image image)
  132. {
  133. var hasher = new ImageHasher();
  134. return hasher.MedianHash256(image);
  135. }
  136. /// <summary>
  137. /// 使用中值算法计算给定图像的256位哈希
  138. /// 将图像转换为16x16的灰度图像,从中查找中值像素值,然后在结果哈希中将值大于中值的所有像素标记为1。与基于平均值的实现相比,更能抵抗非线性图像编辑。
  139. /// </summary>
  140. /// <param name="image">读取到的图片流</param>
  141. /// <returns>256位hash值,生成一个4长度的数组返回</returns>
  142. public static ulong[] MedianHash256(this Image<L8> image)
  143. {
  144. var hasher = new ImageHasher();
  145. return hasher.MedianHash256(image);
  146. }
  147. /// <summary>
  148. /// 使用差分哈希算法计算图像的64位哈希。
  149. /// </summary>
  150. /// <see cref="https://segmentfault.com/a/1190000038308093"/>
  151. /// <param name="image">读取到的图片流</param>
  152. /// <returns>64位hash值</returns>
  153. public static ulong DifferenceHash64(this Image image)
  154. {
  155. var hasher = new ImageHasher();
  156. return hasher.DifferenceHash64(image);
  157. }
  158. /// <summary>
  159. /// 使用差分哈希算法计算图像的64位哈希。
  160. /// </summary>
  161. /// <see cref="https://segmentfault.com/a/1190000038308093"/>
  162. /// <param name="image">读取到的图片流</param>
  163. /// <returns>64位hash值</returns>
  164. public static ulong DifferenceHash64(this Image<L8> image)
  165. {
  166. var hasher = new ImageHasher();
  167. return hasher.DifferenceHash64(image);
  168. }
  169. /// <summary>
  170. /// 使用差分哈希算法计算图像的64位哈希。
  171. /// </summary>
  172. /// <see cref="https://segmentfault.com/a/1190000038308093"/>
  173. /// <param name="image">读取到的图片流</param>
  174. /// <returns>256位hash值</returns>
  175. public static ulong[] DifferenceHash256(this Image image)
  176. {
  177. var hasher = new ImageHasher();
  178. return hasher.DifferenceHash256(image);
  179. }
  180. /// <summary>
  181. /// 使用差分哈希算法计算图像的64位哈希。
  182. /// </summary>
  183. /// <see cref="https://segmentfault.com/a/1190000038308093"/>
  184. /// <param name="image">读取到的图片流</param>
  185. /// <returns>256位hash值</returns>
  186. public static ulong[] DifferenceHash256(this Image<L8> image)
  187. {
  188. var hasher = new ImageHasher();
  189. return hasher.DifferenceHash256(image);
  190. }
  191. /// <summary>
  192. /// 使用DCT算法计算图像的64位哈希
  193. /// </summary>
  194. /// <see cref="https://segmentfault.com/a/1190000038308093"/>
  195. /// <param name="image">读取到的图片流</param>
  196. /// <returns>64位hash值</returns>
  197. public static ulong DctHash(this Image image)
  198. {
  199. var hasher = new ImageHasher();
  200. return hasher.DctHash(image);
  201. }
  202. /// <summary>
  203. /// 使用DCT算法计算图像的64位哈希
  204. /// </summary>
  205. /// <see cref="https://segmentfault.com/a/1190000038308093"/>
  206. /// <param name="image">读取到的图片流</param>
  207. /// <returns>64位hash值</returns>
  208. public static ulong DctHash(this Image<L8> image)
  209. {
  210. var hasher = new ImageHasher();
  211. return hasher.DctHash(image);
  212. }
  213. /// <summary>
  214. /// 使用汉明距离比较两幅图像的哈希值。结果1表示图像完全相同,而结果0表示图像完全不同。
  215. /// </summary>
  216. /// <param name="image1">图像1</param>
  217. /// <param name="image2">图像2</param>
  218. /// <param name="algorithm">对比算法</param>
  219. /// <returns>相似度范围:[0,1]</returns>
  220. public static float Compare(this Image image1, Image image2, ImageHashAlgorithm algorithm = ImageHashAlgorithm.Difference)
  221. {
  222. var hasher = new ImageHasher();
  223. return algorithm switch
  224. {
  225. ImageHashAlgorithm.Average => ImageHasher.Compare(hasher.AverageHash64(image1), hasher.AverageHash64(image2)),
  226. ImageHashAlgorithm.Medium => ImageHasher.Compare(hasher.MedianHash256(image1), hasher.MedianHash256(image2)),
  227. ImageHashAlgorithm.Difference => ImageHasher.Compare(hasher.DifferenceHash256(image1), hasher.DifferenceHash256(image2)),
  228. ImageHashAlgorithm.DCT => ImageHasher.Compare(hasher.DctHash(image1), hasher.DctHash(image2)),
  229. _ => throw new ArgumentOutOfRangeException(nameof(algorithm), algorithm, null)
  230. };
  231. }
  232. /// <summary>
  233. /// 使用汉明距离比较两幅图像的哈希值。结果1表示图像完全相同,而结果0表示图像完全不同。
  234. /// </summary>
  235. /// <param name="image1">图像1</param>
  236. /// <param name="image2">图像2</param>
  237. /// <param name="algorithm">对比算法</param>
  238. /// <returns>相似度范围:[0,1]</returns>
  239. public static float Compare(this Image<L8> image1, Image<L8> image2, ImageHashAlgorithm algorithm = ImageHashAlgorithm.Difference)
  240. {
  241. var hasher = new ImageHasher();
  242. return algorithm switch
  243. {
  244. ImageHashAlgorithm.Average => ImageHasher.Compare(hasher.AverageHash64(image1), hasher.AverageHash64(image2)),
  245. ImageHashAlgorithm.Medium => ImageHasher.Compare(hasher.MedianHash256(image1), hasher.MedianHash256(image2)),
  246. ImageHashAlgorithm.Difference => ImageHasher.Compare(hasher.DifferenceHash256(image1), hasher.DifferenceHash256(image2)),
  247. ImageHashAlgorithm.DCT => ImageHasher.Compare(hasher.DctHash(image1), hasher.DctHash(image2)),
  248. _ => throw new ArgumentOutOfRangeException(nameof(algorithm), algorithm, null)
  249. };
  250. }
  251. /// <summary>
  252. /// 使用汉明距离比较两幅图像的哈希值。结果1表示图像完全相同,而结果0表示图像完全不同。
  253. /// </summary>
  254. /// <param name="image1">图像1的hash</param>
  255. /// <param name="image2path">图像2的路径</param>
  256. /// <param name="algorithm">对比算法</param>
  257. /// <returns>相似度范围:[0,1]</returns>
  258. public static float Compare(this Image image1, string image2path, ImageHashAlgorithm algorithm = ImageHashAlgorithm.Difference)
  259. {
  260. var hasher = new ImageHasher();
  261. return algorithm switch
  262. {
  263. ImageHashAlgorithm.Average => ImageHasher.Compare(hasher.AverageHash64(image1), hasher.AverageHash64(image2path)),
  264. ImageHashAlgorithm.Medium => ImageHasher.Compare(hasher.MedianHash256(image1), hasher.MedianHash256(image2path)),
  265. ImageHashAlgorithm.Difference => ImageHasher.Compare(hasher.DifferenceHash256(image1), hasher.DifferenceHash256(image2path)),
  266. ImageHashAlgorithm.DCT => ImageHasher.Compare(hasher.DctHash(image1), hasher.DctHash(image2path)),
  267. _ => throw new ArgumentOutOfRangeException(nameof(algorithm), algorithm, null)
  268. };
  269. }
  270. /// <summary>
  271. /// 使用汉明距离比较两幅图像的哈希值。结果1表示图像完全相同,而结果0表示图像完全不同。
  272. /// </summary>
  273. /// <param name="image1">图像1的hash</param>
  274. /// <param name="image2path">图像2的路径</param>
  275. /// <param name="algorithm">对比算法</param>
  276. /// <returns>相似度范围:[0,1]</returns>
  277. public static float Compare(this Image<L8> image1, string image2path, ImageHashAlgorithm algorithm = ImageHashAlgorithm.Difference)
  278. {
  279. var hasher = new ImageHasher();
  280. return algorithm switch
  281. {
  282. ImageHashAlgorithm.Average => ImageHasher.Compare(hasher.AverageHash64(image1), hasher.AverageHash64(image2path)),
  283. ImageHashAlgorithm.Medium => ImageHasher.Compare(hasher.MedianHash256(image1), hasher.MedianHash256(image2path)),
  284. ImageHashAlgorithm.Difference => ImageHasher.Compare(hasher.DifferenceHash256(image1), hasher.DifferenceHash256(image2path)),
  285. ImageHashAlgorithm.DCT => ImageHasher.Compare(hasher.DctHash(image1), hasher.DctHash(image2path)),
  286. _ => throw new ArgumentOutOfRangeException(nameof(algorithm), algorithm, null)
  287. };
  288. }
  289. }
  290. public enum ImageHashAlgorithm
  291. {
  292. /// <summary>
  293. /// 均值算法
  294. /// </summary>
  295. Average,
  296. /// <summary>
  297. /// 中值算法
  298. /// </summary>
  299. Medium,
  300. /// <summary>
  301. /// 差异算法
  302. /// </summary>
  303. Difference,
  304. /// <summary>
  305. /// DCT算法
  306. /// </summary>
  307. DCT
  308. }