phash.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. "use strict";
  2. /*
  3. Copyright (c) 2011 Elliot Shepherd
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. // https://code.google.com/p/ironchef-team21/source/browse/ironchef_team21/src/ImagePHash.java
  21. /*
  22. * pHash-like image hash.
  23. * Author: Elliot Shepherd (elliot@jarofworms.com
  24. * Based On: http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html
  25. */
  26. function ImagePHash(size, smallerSize) {
  27. this.size = this.size || size;
  28. this.smallerSize = this.smallerSize || smallerSize;
  29. initCoefficients(this.size);
  30. }
  31. ImagePHash.prototype.size = 32;
  32. ImagePHash.prototype.smallerSize = 8;
  33. ImagePHash.prototype.distance = function (s1, s2) {
  34. var counter = 0;
  35. for (var k = 0; k < s1.length; k++) {
  36. if (s1[k] !== s2[k]) {
  37. counter++;
  38. }
  39. }
  40. return counter / s1.length;
  41. }; // Returns a 'binary string' (like. 001010111011100010) which is easy to do a hamming distance on.
  42. ImagePHash.prototype.getHash = function (img) {
  43. /* 1. Reduce size.
  44. * Like Average Hash, pHash starts with a small image.
  45. * However, the image is larger than 8x8; 32x32 is a good size.
  46. * This is really done to simplify the DCT computation and not
  47. * because it is needed to reduce the high frequencies.
  48. */
  49. img = img.clone().resize(this.size, this.size);
  50. /* 2. Reduce color.
  51. * The image is reduced to a grayscale just to further simplify
  52. * the number of computations.
  53. */
  54. img.grayscale();
  55. var vals = [];
  56. for (var x = 0; x < img.bitmap.width; x++) {
  57. vals[x] = [];
  58. for (var y = 0; y < img.bitmap.height; y++) {
  59. vals[x][y] = intToRGBA(img.getPixelColor(x, y)).b;
  60. }
  61. }
  62. /* 3. Compute the DCT.
  63. * The DCT separates the image into a collection of frequencies
  64. * and scalars. While JPEG uses an 8x8 DCT, this algorithm uses
  65. * a 32x32 DCT.
  66. */
  67. var dctVals = applyDCT(vals, this.size);
  68. /* 4. Reduce the DCT.
  69. * This is the magic step. While the DCT is 32x32, just keep the
  70. * top-left 8x8. Those represent the lowest frequencies in the
  71. * picture.
  72. */
  73. /* 5. Compute the average value.
  74. * Like the Average Hash, compute the mean DCT value (using only
  75. * the 8x8 DCT low-frequency values and excluding the first term
  76. * since the DC coefficient can be significantly different from
  77. * the other values and will throw off the average).
  78. */
  79. var total = 0;
  80. for (var _x = 0; _x < this.smallerSize; _x++) {
  81. for (var _y = 0; _y < this.smallerSize; _y++) {
  82. total += dctVals[_x][_y];
  83. }
  84. }
  85. var avg = total / (this.smallerSize * this.smallerSize);
  86. /* 6. Further reduce the DCT.
  87. * This is the magic step. Set the 64 hash bits to 0 or 1
  88. * depending on whether each of the 64 DCT values is above or
  89. * below the average value. The result doesn't tell us the
  90. * actual low frequencies; it just tells us the very-rough
  91. * relative scale of the frequencies to the mean. The result
  92. * will not vary as long as the overall structure of the image
  93. * remains the same; this can survive gamma and color histogram
  94. * adjustments without a problem.
  95. */
  96. var hash = '';
  97. for (var _x2 = 0; _x2 < this.smallerSize; _x2++) {
  98. for (var _y2 = 0; _y2 < this.smallerSize; _y2++) {
  99. hash += dctVals[_x2][_y2] > avg ? '1' : '0';
  100. }
  101. }
  102. return hash;
  103. }; // DCT function stolen from http://stackoverflow.com/questions/4240490/problems-with-dct-and-idct-algorithm-in-java
  104. function intToRGBA(i) {
  105. var rgba = {};
  106. rgba.r = Math.floor(i / Math.pow(256, 3));
  107. rgba.g = Math.floor((i - rgba.r * Math.pow(256, 3)) / Math.pow(256, 2));
  108. rgba.b = Math.floor((i - rgba.r * Math.pow(256, 3) - rgba.g * Math.pow(256, 2)) / Math.pow(256, 1));
  109. rgba.a = Math.floor((i - rgba.r * Math.pow(256, 3) - rgba.g * Math.pow(256, 2) - rgba.b * Math.pow(256, 1)) / Math.pow(256, 0));
  110. return rgba;
  111. }
  112. var c = [];
  113. function initCoefficients(size) {
  114. for (var i = 1; i < size; i++) {
  115. c[i] = 1;
  116. }
  117. c[0] = 1 / Math.sqrt(2.0);
  118. }
  119. function applyDCT(f, size) {
  120. var N = size;
  121. var F = [];
  122. for (var u = 0; u < N; u++) {
  123. F[u] = [];
  124. for (var v = 0; v < N; v++) {
  125. var sum = 0;
  126. for (var i = 0; i < N; i++) {
  127. for (var j = 0; j < N; j++) {
  128. sum += Math.cos((2 * i + 1) / (2.0 * N) * u * Math.PI) * Math.cos((2 * j + 1) / (2.0 * N) * v * Math.PI) * f[i][j];
  129. }
  130. }
  131. sum *= c[u] * c[v] / 4;
  132. F[u][v] = sum;
  133. }
  134. }
  135. return F;
  136. }
  137. module.exports = ImagePHash;
  138. //# sourceMappingURL=phash.js.map