@@ -45,23 +45,24 @@ ImagePHash.prototype.distance = function(s1, s2) {
45
45
counter ++ ;
46
46
}
47
47
}
48
+
48
49
return counter / s1 . length ;
49
50
} ;
50
51
51
52
// Returns a 'binary string' (like. 001010111011100010) which is easy to do a hamming distance on.
52
53
ImagePHash . prototype . getHash = function ( img ) {
53
54
/* 1. Reduce size.
54
- * Like Average Hash, pHash starts with a small image.
55
- * However, the image is larger than 8x8; 32x32 is a good size.
56
- * This is really done to simplify the DCT computation and not
57
- * because it is needed to reduce the high frequencies.
58
- */
55
+ * Like Average Hash, pHash starts with a small image.
56
+ * However, the image is larger than 8x8; 32x32 is a good size.
57
+ * This is really done to simplify the DCT computation and not
58
+ * because it is needed to reduce the high frequencies.
59
+ */
59
60
img = img . clone ( ) . resize ( this . size , this . size ) ;
60
61
61
62
/* 2. Reduce color.
62
- * The image is reduced to a grayscale just to further simplify
63
- * the number of computations.
64
- */
63
+ * The image is reduced to a grayscale just to further simplify
64
+ * the number of computations.
65
+ */
65
66
img . grayscale ( ) ;
66
67
67
68
const vals = [ ] ;
@@ -74,23 +75,23 @@ ImagePHash.prototype.getHash = function(img) {
74
75
}
75
76
76
77
/* 3. Compute the DCT.
77
- * The DCT separates the image into a collection of frequencies
78
- * and scalars. While JPEG uses an 8x8 DCT, this algorithm uses
79
- * a 32x32 DCT.
80
- */
78
+ * The DCT separates the image into a collection of frequencies
79
+ * and scalars. While JPEG uses an 8x8 DCT, this algorithm uses
80
+ * a 32x32 DCT.
81
+ */
81
82
const dctVals = applyDCT ( vals , this . size ) ;
82
83
83
84
/* 4. Reduce the DCT.
84
- * This is the magic step. While the DCT is 32x32, just keep the
85
- * top-left 8x8. Those represent the lowest frequencies in the
86
- * picture.
87
- */
85
+ * This is the magic step. While the DCT is 32x32, just keep the
86
+ * top-left 8x8. Those represent the lowest frequencies in the
87
+ * picture.
88
+ */
88
89
/* 5. Compute the average value.
89
- * Like the Average Hash, compute the mean DCT value (using only
90
- * the 8x8 DCT low-frequency values and excluding the first term
91
- * since the DC coefficient can be significantly different from
92
- * the other values and will throw off the average).
93
- */
90
+ * Like the Average Hash, compute the mean DCT value (using only
91
+ * the 8x8 DCT low-frequency values and excluding the first term
92
+ * since the DC coefficient can be significantly different from
93
+ * the other values and will throw off the average).
94
+ */
94
95
let total = 0 ;
95
96
96
97
for ( let x = 0 ; x < this . smallerSize ; x ++ ) {
@@ -102,15 +103,15 @@ ImagePHash.prototype.getHash = function(img) {
102
103
const avg = total / ( this . smallerSize * this . smallerSize ) ;
103
104
104
105
/* 6. Further reduce the DCT.
105
- * This is the magic step. Set the 64 hash bits to 0 or 1
106
- * depending on whether each of the 64 DCT values is above or
107
- * below the average value. The result doesn't tell us the
108
- * actual low frequencies; it just tells us the very-rough
109
- * relative scale of the frequencies to the mean. The result
110
- * will not vary as long as the overall structure of the image
111
- * remains the same; this can survive gamma and color histogram
112
- * adjustments without a problem.
113
- */
106
+ * This is the magic step. Set the 64 hash bits to 0 or 1
107
+ * depending on whether each of the 64 DCT values is above or
108
+ * below the average value. The result doesn't tell us the
109
+ * actual low frequencies; it just tells us the very-rough
110
+ * relative scale of the frequencies to the mean. The result
111
+ * will not vary as long as the overall structure of the image
112
+ * remains the same; this can survive gamma and color histogram
113
+ * adjustments without a problem.
114
+ */
114
115
let hash = '' ;
115
116
116
117
for ( let x = 0 ; x < this . smallerSize ; x ++ ) {
@@ -169,6 +170,7 @@ function applyDCT(f, size) {
169
170
f [ i ] [ j ] ;
170
171
}
171
172
}
173
+
172
174
sum *= ( c [ u ] * c [ v ] ) / 4 ;
173
175
F [ u ] [ v ] = sum ;
174
176
}
0 commit comments