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