DarkShadow44

C# image compare

Aug 19th, 2015
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Drawing;
  7. using System.Drawing.Imaging;
  8.  
  9. namespace testsharp
  10. {
  11. class test12
  12. {
  13. static byte[] ImageToBytes(Bitmap bitmap)
  14. {
  15. byte[] data = new byte[bitmap.Width * bitmap.Height * 4];
  16. BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
  17.  
  18. unsafe
  19. {
  20. for (int y = 0; y < bitmapData.Height; y++)
  21. {
  22. byte* row = (byte*)bitmapData.Scan0 + (y * bitmapData.Stride);
  23. for (int x = 0; x < bitmapData.Width; x++)
  24. {
  25. data[(y * bitmapData.Width + x) * 4 + 0] = row[x * 4 + 2]; //Internal R
  26. data[(y * bitmapData.Width + x) * 4 + 1] = row[x * 4 + 1]; //Internal G
  27. data[(y * bitmapData.Width + x) * 4 + 2] = row[x * 4 + 0]; //Internal B
  28. data[(y * bitmapData.Width + x) * 4 + 3] = row[x * 4 + 3]; //Internal A
  29. }
  30. }
  31. }
  32. bitmap.UnlockBits(bitmapData);
  33.  
  34. return data;
  35. }
  36.  
  37. static Bitmap BytesToImage(byte[] data, int width, int height)
  38. {
  39. Bitmap bitmap = new Bitmap(width, height);
  40. BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
  41.  
  42. unsafe
  43. {
  44. for (int y = 0; y < bitmapData.Height; y++)
  45. {
  46. byte* row = (byte*)bitmapData.Scan0 + (y * bitmapData.Stride);
  47. for (int x = 0; x < bitmapData.Width; x++)
  48. {
  49. row[x * 4 + 0] = data[(y * bitmapData.Width + x) * 4 + 2]; //Internal B
  50. row[x * 4 + 1] = data[(y * bitmapData.Width + x) * 4 + 1]; //Internal G
  51. row[x * 4 + 2] = data[(y * bitmapData.Width + x) * 4 + 0]; //Internal R
  52. row[x * 4 + 3] = data[(y * bitmapData.Width + x) * 4 + 3]; //Internal A
  53. }
  54. }
  55. }
  56.  
  57. bitmap.UnlockBits(bitmapData);
  58.  
  59. return bitmap;
  60. }
  61.  
  62. static byte AdjustBrightness(byte b, float factor)
  63. {
  64. return (byte)(Math.Pow((float)b / 255, factor) * 255); // gamma
  65. }
  66.  
  67. static void FindImageDifferences(string pathIn1, string pathIn2, string pathOut)
  68. {
  69. Image img1 = Image.FromFile(pathIn1);
  70. Image img2 = Image.FromFile(pathIn2);
  71.  
  72. if (img1.Width != img2.Width || img1.Height != img2.Height)
  73. return;
  74.  
  75. byte[] data1 = ImageToBytes((Bitmap)img1);
  76. byte[] data2 = ImageToBytes((Bitmap)img2);
  77.  
  78.  
  79. byte[] data3 = new byte[data1.Length];
  80.  
  81. for (int i = 0; i < data3.Length; i++)
  82. {
  83. int diff = Math.Abs(data1[i] - data2[i]);
  84. if (diff < 40)
  85. diff = 0;
  86. diff *= 20;
  87. int newValue = diff + AdjustBrightness(data1[i], 1.6f);
  88. if (newValue > 255)
  89. data3[i] = 255;
  90. else
  91. data3[i] = (byte)newValue;
  92. }
  93. Image imgOut = BytesToImage(data3, img1.Width, img1.Height);
  94. imgOut.Save(pathOut, ImageFormat.Png);
  95.  
  96. img1.Dispose();
  97. img2.Dispose();
  98. imgOut.Dispose();
  99. }
  100.  
  101. public static void Main()
  102. {
  103. string pathIn1 = @"e:\temp\i1.png";
  104. string pathIn2 = @"e:\temp\i2.png";
  105. string pathOut = @"e:\temp\out.png";
  106.  
  107. FindImageDifferences(pathIn1, pathIn2, pathOut);
  108. }
  109.  
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment