ssim.js 4.0.1 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CLA.md +66 -0
- package/CONTRIBUTING.md +119 -0
- package/LICENSE +1 -1
- package/LICENSES/LIVE Images Database +18 -0
- package/LICENSES/Subjective quality assessment - IVC database +5 -0
- package/LICENSES/Wang et al SSIM +24 -0
- package/LICENSES/bezkrovny +49 -0
- package/LICENSES/cell2csv +2 -0
- package/LICENSES/obartra-ssim +21 -0
- package/LICENSES/rhys-e +21 -0
- package/README.md +216 -34
- package/dist/bezkrovnySsim.d.ts +5 -19
- package/dist/bezkrovnySsim.js +113 -72
- package/dist/bezkrovnySsim.js.map +1 -1
- package/dist/browser.d.mts +9 -0
- package/dist/browser.d.ts +9 -0
- package/dist/browser.js +37 -0
- package/dist/browser.js.map +1 -0
- package/dist/browser.mjs +742 -0
- package/dist/browser.mjs.map +7 -0
- package/dist/defaults.d.ts +3 -1
- package/dist/defaults.js +14 -4
- package/dist/defaults.js.map +1 -1
- package/dist/gaussian.d.ts +2 -0
- package/dist/gaussian.js +168 -0
- package/dist/gaussian.js.map +1 -0
- package/dist/gaussianSymmetric.d.ts +3 -0
- package/dist/gaussianSymmetric.js +62 -0
- package/dist/gaussianSymmetric.js.map +1 -0
- package/dist/index.d.mts +9 -0
- package/dist/index.d.ts +8 -19
- package/dist/index.js +19 -88
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +718 -0
- package/dist/index.mjs.map +7 -0
- package/dist/options.d.ts +2 -0
- package/dist/options.js +60 -0
- package/dist/options.js.map +1 -0
- package/dist/originalSsim.d.ts +3 -26
- package/dist/originalSsim.js +5 -57
- package/dist/originalSsim.js.map +1 -1
- package/dist/pixels.d.ts +5 -0
- package/dist/pixels.js +157 -0
- package/dist/pixels.js.map +1 -0
- package/dist/prepare.d.ts +7 -0
- package/dist/prepare.js +48 -0
- package/dist/prepare.js.map +1 -0
- package/dist/referenceSsim.d.ts +4 -0
- package/dist/referenceSsim.js +59 -0
- package/dist/referenceSsim.js.map +1 -0
- package/dist/result.d.ts +2 -0
- package/dist/result.js +20 -0
- package/dist/result.js.map +1 -0
- package/dist/ssim.d.ts +3 -21
- package/dist/ssim.js +4 -126
- package/dist/ssim.js.map +1 -1
- package/dist/ssim.web.js +2 -2
- package/dist/ssim.web.js.map +7 -1
- package/dist/types.d.ts +27 -7
- package/dist/weberSsim.d.ts +19 -0
- package/dist/weberSsim.js +119 -0
- package/dist/weberSsim.js.map +1 -0
- package/package.json +116 -1
- package/src/bezkrovnySsim.ts +133 -67
- package/src/browser.ts +49 -0
- package/src/defaults.ts +16 -6
- package/src/gaussian.ts +228 -0
- package/src/gaussianSymmetric.ts +95 -0
- package/src/index.ts +47 -96
- package/src/options.ts +74 -0
- package/src/originalSsim.ts +9 -79
- package/src/pixels.ts +198 -0
- package/src/prepare.ts +73 -0
- package/src/referenceSsim.ts +71 -0
- package/src/result.ts +23 -0
- package/src/ssim.ts +9 -168
- package/src/types.ts +44 -15
- package/src/weberSsim.ts +132 -0
- package/dist/downsample.d.ts +0 -12
- package/dist/downsample.js +0 -70
- package/dist/downsample.js.map +0 -1
- package/dist/math.d.ts +0 -155
- package/dist/math.js +0 -394
- package/dist/math.js.map +0 -1
- package/dist/matlab/conv2.d.ts +0 -137
- package/dist/matlab/conv2.js +0 -250
- package/dist/matlab/conv2.js.map +0 -1
- package/dist/matlab/filter2.d.ts +0 -18
- package/dist/matlab/filter2.js +0 -53
- package/dist/matlab/filter2.js.map +0 -1
- package/dist/matlab/fspecial.d.ts +0 -39
- package/dist/matlab/fspecial.js +0 -102
- package/dist/matlab/fspecial.js.map +0 -1
- package/dist/matlab/imfilter.d.ts +0 -24
- package/dist/matlab/imfilter.js +0 -84
- package/dist/matlab/imfilter.js.map +0 -1
- package/dist/matlab/index.d.ts +0 -23
- package/dist/matlab/index.js +0 -29
- package/dist/matlab/index.js.map +0 -1
- package/dist/matlab/internal/numbers.d.ts +0 -14
- package/dist/matlab/internal/numbers.js +0 -28
- package/dist/matlab/internal/numbers.js.map +0 -1
- package/dist/matlab/mod.d.ts +0 -21
- package/dist/matlab/mod.js +0 -27
- package/dist/matlab/mod.js.map +0 -1
- package/dist/matlab/normpdf.d.ts +0 -36
- package/dist/matlab/normpdf.js +0 -55
- package/dist/matlab/normpdf.js.map +0 -1
- package/dist/matlab/ones.d.ts +0 -15
- package/dist/matlab/ones.js +0 -22
- package/dist/matlab/ones.js.map +0 -1
- package/dist/matlab/padarray.d.ts +0 -37
- package/dist/matlab/padarray.js +0 -349
- package/dist/matlab/padarray.js.map +0 -1
- package/dist/matlab/rgb2gray.d.ts +0 -15
- package/dist/matlab/rgb2gray.js +0 -58
- package/dist/matlab/rgb2gray.js.map +0 -1
- package/dist/matlab/skip2d.d.ts +0 -34
- package/dist/matlab/skip2d.js +0 -55
- package/dist/matlab/skip2d.js.map +0 -1
- package/dist/matlab/sub.d.ts +0 -17
- package/dist/matlab/sub.js +0 -33
- package/dist/matlab/sub.js.map +0 -1
- package/dist/matlab/transpose.d.ts +0 -18
- package/dist/matlab/transpose.js +0 -34
- package/dist/matlab/transpose.js.map +0 -1
- package/dist/matlab/zeros.d.ts +0 -15
- package/dist/matlab/zeros.js +0 -22
- package/dist/matlab/zeros.js.map +0 -1
- package/dist/version.d.ts +0 -2
- package/dist/version.js +0 -5
- package/dist/version.js.map +0 -1
- package/src/downsample.ts +0 -81
- package/src/math.ts +0 -447
- package/src/matlab/conv2.ts +0 -272
- package/src/matlab/filter2.ts +0 -52
- package/src/matlab/fspecial.ts +0 -107
- package/src/matlab/imfilter.ts +0 -93
- package/src/matlab/index.ts +0 -23
- package/src/matlab/internal/numbers.ts +0 -28
- package/src/matlab/mod.ts +0 -23
- package/src/matlab/normpdf.ts +0 -57
- package/src/matlab/ones.ts +0 -19
- package/src/matlab/padarray.ts +0 -375
- package/src/matlab/rgb2gray.ts +0 -65
- package/src/matlab/skip2d.ts +0 -58
- package/src/matlab/sub.ts +0 -38
- package/src/matlab/transpose.ts +0 -33
- package/src/matlab/zeros.ts +0 -19
- package/src/types/bmp-js.d.ts +0 -1
- package/src/version.ts +0 -2
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { ImageMatrix, Matrix, Options } from './types'
|
|
2
|
+
import { gaussianSsim } from './gaussian'
|
|
3
|
+
|
|
4
|
+
/** Author-reference Gaussian profile. Odd windows use separable filtering;
|
|
5
|
+
* explicit even sizes retain their requested geometry through direct filtering. */
|
|
6
|
+
export function referenceSsim(
|
|
7
|
+
a: ImageMatrix,
|
|
8
|
+
b: ImageMatrix,
|
|
9
|
+
options: Options,
|
|
10
|
+
map = true
|
|
11
|
+
): Matrix {
|
|
12
|
+
const L = 2 ** options.bitDepth - 1
|
|
13
|
+
const c1 = (options.k1 * L) ** 2,
|
|
14
|
+
c2 = (options.k2 * L) ** 2
|
|
15
|
+
if (c1 > 0 && c2 > 0)
|
|
16
|
+
return gaussianSsim(a, b, options, options.windowSize % 2 === 1, true, map)
|
|
17
|
+
|
|
18
|
+
// Cold path for zero/underflowed constants: preserve the author's piecewise
|
|
19
|
+
// equation, including 1 for two zero-valued patches, without changing legacy modes.
|
|
20
|
+
const size = options.windowSize,
|
|
21
|
+
center = (size - 1) / 2
|
|
22
|
+
const weights = Array.from({ length: size * size }, (_, i) => {
|
|
23
|
+
const x = (i % size) - center,
|
|
24
|
+
y = Math.floor(i / size) - center
|
|
25
|
+
return Math.exp(-(x * x + y * y) / (2 * 1.5 ** 2))
|
|
26
|
+
})
|
|
27
|
+
const total = weights.reduce((sum, weight) => sum + weight, 0)
|
|
28
|
+
for (let i = 0; i < weights.length; i++) weights[i] /= total
|
|
29
|
+
const width = a.width - size + 1,
|
|
30
|
+
height = a.height - size + 1
|
|
31
|
+
const data = map ? new Array<number>(width * height) : []
|
|
32
|
+
let sum = 0
|
|
33
|
+
for (let y = 0; y < height; y++)
|
|
34
|
+
for (let x = 0; x < width; x++) {
|
|
35
|
+
let sx = 0,
|
|
36
|
+
sy = 0,
|
|
37
|
+
xx = 0,
|
|
38
|
+
yy = 0,
|
|
39
|
+
xy = 0
|
|
40
|
+
for (let row = 0; row < size; row++)
|
|
41
|
+
for (let column = 0; column < size; column++) {
|
|
42
|
+
const i = (y + row) * a.width + x + column
|
|
43
|
+
const av = a.data[i],
|
|
44
|
+
bv = b.data[i],
|
|
45
|
+
weight = weights[row * size + column]
|
|
46
|
+
sx += av * weight
|
|
47
|
+
sy += bv * weight
|
|
48
|
+
xx += av * av * weight
|
|
49
|
+
yy += bv * bv * weight
|
|
50
|
+
xy += av * bv * weight
|
|
51
|
+
}
|
|
52
|
+
const numerator1 = 2 * sx * sy + c1
|
|
53
|
+
const numerator2 = 2 * (xy - sx * sy) + c2
|
|
54
|
+
const denominator1 = sx * sx + sy * sy + c1
|
|
55
|
+
const denominator2 = xx - sx * sx + (yy - sy * sy) + c2
|
|
56
|
+
const value =
|
|
57
|
+
denominator1 * denominator2 > 0
|
|
58
|
+
? (numerator1 * numerator2) / (denominator1 * denominator2)
|
|
59
|
+
: denominator1 !== 0 && denominator2 === 0
|
|
60
|
+
? numerator1 / denominator1
|
|
61
|
+
: 1
|
|
62
|
+
if (map) data[y * width + x] = value
|
|
63
|
+
else sum += value
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
data,
|
|
67
|
+
width,
|
|
68
|
+
height,
|
|
69
|
+
...(map ? {} : { mssim: sum / (width * height) }),
|
|
70
|
+
}
|
|
71
|
+
}
|
package/src/result.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Matrix, MSSIMMatrix, SsimResult } from './types'
|
|
2
|
+
|
|
3
|
+
function mean2d(map: Matrix): number {
|
|
4
|
+
let sum = 0
|
|
5
|
+
for (let i = 0; i < map.data.length; i++) sum += map.data[i]
|
|
6
|
+
return sum / map.data.length
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function finishComparison(
|
|
10
|
+
map: Matrix,
|
|
11
|
+
start: number,
|
|
12
|
+
includeMap = true
|
|
13
|
+
): SsimResult {
|
|
14
|
+
const mssim =
|
|
15
|
+
(map as MSSIMMatrix).mssim !== undefined
|
|
16
|
+
? (map as MSSIMMatrix).mssim
|
|
17
|
+
: mean2d(map)
|
|
18
|
+
return {
|
|
19
|
+
mssim,
|
|
20
|
+
...(includeMap ? { ssim_map: map } : {}),
|
|
21
|
+
performance: Date.now() - start,
|
|
22
|
+
}
|
|
23
|
+
}
|
package/src/ssim.ts
CHANGED
|
@@ -1,171 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
* Reproduces the original SSIM matlab scripts. For a direct comparison you may want to check the
|
|
5
|
-
* scripts contained within `/assets`
|
|
6
|
-
*
|
|
7
|
-
* @namespace ssim
|
|
8
|
-
*/
|
|
9
|
-
import {
|
|
10
|
-
add2d,
|
|
11
|
-
divide2d,
|
|
12
|
-
multiply2d,
|
|
13
|
-
square2d,
|
|
14
|
-
subtract2d,
|
|
15
|
-
sum2d
|
|
16
|
-
} from "./math";
|
|
17
|
-
import { conv2, normpdf, transpose } from "./matlab";
|
|
18
|
-
import { Options, Matrix } from "./types";
|
|
1
|
+
import { gaussianSsim } from './gaussian'
|
|
2
|
+
import type { ImageMatrix, Options } from './types'
|
|
19
3
|
|
|
20
|
-
/**
|
|
21
|
-
* Generates a SSIM map based on two input image matrices. For images greater than 512 pixels, it
|
|
22
|
-
* will downsample by default (unless `options.downsample` is set to falsy).
|
|
23
|
-
*
|
|
24
|
-
* Images must be a 2-Dimensional grayscale image
|
|
25
|
-
*
|
|
26
|
-
* This method produces the same exact output than `assets/ssim.m` when running on Matlab. It's
|
|
27
|
-
* mathematically equivalent but it is not a line-by-line port. If you want a line-by-line port
|
|
28
|
-
* check `originalSsim`. Several performance optimizations have been made here to achieve greater
|
|
29
|
-
* performance.
|
|
30
|
-
*
|
|
31
|
-
* @method ssim
|
|
32
|
-
* @param {Matrix} pixels1 - The reference matrix
|
|
33
|
-
* @param {Matrix} pixels2 - The second matrix to compare against
|
|
34
|
-
* @param {Options} options - The input options parameter
|
|
35
|
-
* @returns {Matrix} ssim_map - A matrix containing the map of computed SSIMs
|
|
36
|
-
* @public
|
|
37
|
-
* @memberOf ssim
|
|
38
|
-
*/
|
|
4
|
+
/** Separable Gaussian SSIM, preserving the legacy odd-window convention. */
|
|
39
5
|
export function ssim(
|
|
40
|
-
pixels1:
|
|
41
|
-
pixels2:
|
|
42
|
-
options: Options
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const c1 = (options.k1 * L) ** 2;
|
|
47
|
-
const c2 = (options.k2 * L) ** 2;
|
|
48
|
-
|
|
49
|
-
w = divide2d(w, sum2d(w));
|
|
50
|
-
const wt = transpose(w);
|
|
51
|
-
const μ1 = conv2(pixels1, w, wt, "valid");
|
|
52
|
-
const μ2 = conv2(pixels2, w, wt, "valid");
|
|
53
|
-
const μ1Sq = square2d(μ1);
|
|
54
|
-
const μ2Sq = square2d(μ2);
|
|
55
|
-
const μ12 = multiply2d(μ1, μ2);
|
|
56
|
-
const pixels1Sq = square2d(pixels1);
|
|
57
|
-
const pixels2Sq = square2d(pixels2);
|
|
58
|
-
const σ1Sq = subtract2d(conv2(pixels1Sq, w, wt, "valid"), μ1Sq);
|
|
59
|
-
const σ2Sq = subtract2d(conv2(pixels2Sq, w, wt, "valid"), μ2Sq);
|
|
60
|
-
const σ12 = subtract2d(
|
|
61
|
-
conv2(multiply2d(pixels1, pixels2), w, wt, "valid"),
|
|
62
|
-
μ12
|
|
63
|
-
);
|
|
64
|
-
|
|
65
|
-
if (c1 > 0 && c2 > 0) {
|
|
66
|
-
return genSSIM(μ12, σ12, μ1Sq, μ2Sq, σ1Sq, σ2Sq, c1, c2);
|
|
67
|
-
}
|
|
68
|
-
return genUQI(μ12, σ12, μ1Sq, μ2Sq, σ1Sq, σ2Sq);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Generates a range of distances of size `2n+1` with increments of 1 and centered at 0.
|
|
73
|
-
*
|
|
74
|
-
* @example `getRange(2) => [2 1 0 1 2]
|
|
75
|
-
* @method getRange
|
|
76
|
-
* @param {Number} size - The maximum distance from the center
|
|
77
|
-
* @returns {Matrix} out - The generated vector
|
|
78
|
-
* @private
|
|
79
|
-
* @memberOf ssim
|
|
80
|
-
*/
|
|
81
|
-
function getRange(size: number): Matrix {
|
|
82
|
-
const offset = Math.floor(size / 2);
|
|
83
|
-
const data = new Array(offset * 2 + 1);
|
|
84
|
-
|
|
85
|
-
for (let x = -offset; x <= offset; x++) {
|
|
86
|
-
data[x + offset] = Math.abs(x);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return {
|
|
90
|
-
data,
|
|
91
|
-
width: data.length,
|
|
92
|
-
height: 1
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Generates the ssim_map based on the intermediate values of the convolutions of the input with the
|
|
98
|
-
* gaussian filter.
|
|
99
|
-
*
|
|
100
|
-
* These methods apply when K1 or K2 are not 0 (non UQI)
|
|
101
|
-
*
|
|
102
|
-
* @method genSSIM
|
|
103
|
-
* @param {Matrix} μ12 - The cell-by cell multiplication of both images convolved
|
|
104
|
-
* with the gaussian filter
|
|
105
|
-
* @param {Matrix} σ12 - The convolution of cell-by cell multiplication of both
|
|
106
|
-
* images minus μ12
|
|
107
|
-
* @param {Matrix} μ1Sq - The convolution of image1 with the gaussian filter squared
|
|
108
|
-
* @param {Matrix} μ2Sq - The convolution of image2 with the gaussian filter squared
|
|
109
|
-
* @param {Matrix} σ1Sq - The convolution of image1^2, minus μ1Sq
|
|
110
|
-
* @param {Matrix} σ2Sq - The convolution of image2^2, minus μ2Sq
|
|
111
|
-
* @param {Number} c1 - The first stability constant
|
|
112
|
-
* @param {Number} c2 - The second stability constant
|
|
113
|
-
* @returns {Matrix} ssim_map - The generated map of SSIM values at each window
|
|
114
|
-
* @private
|
|
115
|
-
* @memberOf ssim
|
|
116
|
-
*/
|
|
117
|
-
function genSSIM(
|
|
118
|
-
μ12: Matrix,
|
|
119
|
-
σ12: Matrix,
|
|
120
|
-
μ1Sq: Matrix,
|
|
121
|
-
μ2Sq: Matrix,
|
|
122
|
-
σ1Sq: Matrix,
|
|
123
|
-
σ2Sq: Matrix,
|
|
124
|
-
c1: number,
|
|
125
|
-
c2: number
|
|
126
|
-
): Matrix {
|
|
127
|
-
const num1 = add2d(multiply2d(μ12, 2), c1);
|
|
128
|
-
const num2 = add2d(multiply2d(σ12, 2), c2);
|
|
129
|
-
const denom1 = add2d(add2d(μ1Sq, μ2Sq), c1);
|
|
130
|
-
const denom2 = add2d(add2d(σ1Sq, σ2Sq), c2);
|
|
131
|
-
|
|
132
|
-
return divide2d(multiply2d(num1, num2), multiply2d(denom1, denom2));
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Generates the Universal Quality Index (UQI) ssim_map based on the intermediate values of the
|
|
137
|
-
* convolutions of the input with the gaussian filter.
|
|
138
|
-
*
|
|
139
|
-
* These methods apply when K1 or K2 are 0 (UQI)
|
|
140
|
-
*
|
|
141
|
-
* @method genUQI
|
|
142
|
-
* @param {Matrix} μ12 - The cell-by cell multiplication of both images convolved
|
|
143
|
-
* with the gaussian filter
|
|
144
|
-
* @param {Matrix} σ12 - The convolution of cell-by cell multiplication of both
|
|
145
|
-
* images minus μ12
|
|
146
|
-
* @param {Matrix} μ1Sq - The convolution of image1 with the gaussian filter squared
|
|
147
|
-
* @param {Matrix} μ2Sq - The convolution of image2 with the gaussian filter squared
|
|
148
|
-
* @param {Matrix} σ1Sq - The convolution of image1^2, minus μ1Sq
|
|
149
|
-
* @param {Matrix} σ2Sq - The convolution of image2^2, minus μ2Sq
|
|
150
|
-
* @returns {Matrix} ssim_map - The generated map of SSIM values at each window
|
|
151
|
-
* @private
|
|
152
|
-
* @memberOf ssim
|
|
153
|
-
*/
|
|
154
|
-
function genUQI(
|
|
155
|
-
μ12: Matrix,
|
|
156
|
-
σ12: Matrix,
|
|
157
|
-
μ1Sq: Matrix,
|
|
158
|
-
μ2Sq: Matrix,
|
|
159
|
-
σ1Sq: Matrix,
|
|
160
|
-
σ2Sq: Matrix
|
|
161
|
-
): Matrix {
|
|
162
|
-
const numerator1 = multiply2d(μ12, 2);
|
|
163
|
-
const numerator2 = multiply2d(σ12, 2);
|
|
164
|
-
const denominator1 = add2d(μ1Sq, μ2Sq);
|
|
165
|
-
const denominator2 = add2d(σ1Sq, σ2Sq);
|
|
166
|
-
|
|
167
|
-
return divide2d(
|
|
168
|
-
multiply2d(numerator1, numerator2),
|
|
169
|
-
multiply2d(denominator1, denominator2)
|
|
170
|
-
);
|
|
6
|
+
pixels1: ImageMatrix,
|
|
7
|
+
pixels2: ImageMatrix,
|
|
8
|
+
options: Options,
|
|
9
|
+
map = true
|
|
10
|
+
) {
|
|
11
|
+
return gaussianSsim(pixels1, pixels2, options, true, false, map)
|
|
171
12
|
}
|
package/src/types.ts
CHANGED
|
@@ -1,19 +1,48 @@
|
|
|
1
1
|
export type Options = {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
2
|
+
map: boolean
|
|
3
|
+
rgb2grayVersion: 'original' | 'integer' | 'matlab'
|
|
4
|
+
k1: number
|
|
5
|
+
k2: number
|
|
6
|
+
ssim: 'gaussian' | 'fast' | 'original' | 'bezkrovny' | 'weber'
|
|
7
|
+
windowSize: number
|
|
8
|
+
bitDepth: number
|
|
9
|
+
downsample: 'wang' | 'original' | 'fast' | false
|
|
10
|
+
maxSize?: number
|
|
11
|
+
}
|
|
10
12
|
|
|
11
13
|
export type Matrix = {
|
|
12
|
-
width: number
|
|
13
|
-
height: number
|
|
14
|
-
data: number[]
|
|
15
|
-
}
|
|
16
|
-
export type Shape = "full" | "same" | "valid";
|
|
14
|
+
width: number
|
|
15
|
+
height: number
|
|
16
|
+
data: number[]
|
|
17
|
+
}
|
|
17
18
|
|
|
18
|
-
export type
|
|
19
|
-
|
|
19
|
+
export type ImageData = {
|
|
20
|
+
readonly data: Uint8ClampedArray | Uint8Array
|
|
21
|
+
readonly height: number
|
|
22
|
+
readonly width: number
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export type ImageMatrix =
|
|
26
|
+
| Matrix
|
|
27
|
+
| ImageData
|
|
28
|
+
| {
|
|
29
|
+
width: number
|
|
30
|
+
height: number
|
|
31
|
+
data:
|
|
32
|
+
| Uint8Array
|
|
33
|
+
| Int8Array
|
|
34
|
+
| Uint32Array
|
|
35
|
+
| Int32Array
|
|
36
|
+
| Uint16Array
|
|
37
|
+
| Float64Array
|
|
38
|
+
}
|
|
39
|
+
export type MSSIMMatrix = Matrix & {
|
|
40
|
+
mssim: number
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export type SsimResult = {
|
|
44
|
+
mssim: number
|
|
45
|
+
ssim_map?: Matrix
|
|
46
|
+
performance: number
|
|
47
|
+
}
|
|
48
|
+
export type SsimMapResult = SsimResult & { ssim_map: Matrix }
|
package/src/weberSsim.ts
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import type { ImageMatrix, MSSIMMatrix, Options } from './types'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Generates a SSIM map based on two input image matrices.
|
|
5
|
+
* Weber SSIM operates in linear time with sliding sums, squared sums, and
|
|
6
|
+
* cross-products. Five vertical column moments serve every window in a row;
|
|
7
|
+
* only O(image width) temporary moment storage is needed.
|
|
8
|
+
*
|
|
9
|
+
* Images must be a 2-Dimensional grayscale image.
|
|
10
|
+
*
|
|
11
|
+
* @method weberSsim
|
|
12
|
+
* @param {ImageMatrix} pixels1 - The reference matrix
|
|
13
|
+
* @param {ImageMatrix} pixels2 - The second matrix to compare against
|
|
14
|
+
* @param {Options} options - The input options parameter
|
|
15
|
+
* @returns {ImageMatrix} ssim_map - A matrix containing the map of computed
|
|
16
|
+
* SSIMs
|
|
17
|
+
* @public
|
|
18
|
+
* @memberOf weberSsim
|
|
19
|
+
*/
|
|
20
|
+
export function weberSsim(
|
|
21
|
+
pixels1: ImageMatrix,
|
|
22
|
+
pixels2: ImageMatrix,
|
|
23
|
+
options: Options,
|
|
24
|
+
map = true
|
|
25
|
+
): MSSIMMatrix {
|
|
26
|
+
const data1 =
|
|
27
|
+
pixels1.data instanceof Uint8Array
|
|
28
|
+
? pixels1.data
|
|
29
|
+
: Int32Array.from(pixels1.data, (value) => (value + 0.5) | 0)
|
|
30
|
+
const data2 =
|
|
31
|
+
pixels2.data instanceof Uint8Array
|
|
32
|
+
? pixels2.data
|
|
33
|
+
: Int32Array.from(pixels2.data, (value) => (value + 0.5) | 0)
|
|
34
|
+
const { bitDepth, k1, k2, windowSize } = options
|
|
35
|
+
const L = 2 ** bitDepth - 1
|
|
36
|
+
const c1 = k1 * L * (k1 * L)
|
|
37
|
+
const c2 = k2 * L * (k2 * L)
|
|
38
|
+
const windowSquared = windowSize * windowSize
|
|
39
|
+
// Five vertical column moments, updated by removing the outgoing row and
|
|
40
|
+
// adding the incoming row. Storage depends on width, not image area.
|
|
41
|
+
// Rounded byte moments are exact integers; sliding does not accumulate drift.
|
|
42
|
+
const columns = new Float64Array(pixels1.width * 5)
|
|
43
|
+
const sumX = columns.subarray(0, pixels1.width)
|
|
44
|
+
const sumY = columns.subarray(pixels1.width, pixels1.width * 2)
|
|
45
|
+
const squareX = columns.subarray(pixels1.width * 2, pixels1.width * 3)
|
|
46
|
+
const squareY = columns.subarray(pixels1.width * 3, pixels1.width * 4)
|
|
47
|
+
const product = columns.subarray(pixels1.width * 4)
|
|
48
|
+
for (let y = 0; y < windowSize; y++) {
|
|
49
|
+
for (let x = 0; x < pixels1.width; x++) {
|
|
50
|
+
const input = y * pixels1.width + x
|
|
51
|
+
const a = data1[input]
|
|
52
|
+
const b = data2[input]
|
|
53
|
+
sumX[x] += a
|
|
54
|
+
sumY[x] += b
|
|
55
|
+
squareX[x] += a * a
|
|
56
|
+
squareY[x] += b * b
|
|
57
|
+
product[x] += a * b
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
const width = pixels1.width - windowSize + 1
|
|
61
|
+
const height = pixels1.height - windowSize + 1
|
|
62
|
+
const ssims = map ? new Array<number>(width * height) : []
|
|
63
|
+
let total = 0
|
|
64
|
+
let correction = 0
|
|
65
|
+
let index = 0
|
|
66
|
+
for (let y = 0; y < height; y++) {
|
|
67
|
+
let rowSum = 0
|
|
68
|
+
if (y > 0) {
|
|
69
|
+
for (let x = 0; x < pixels1.width; x++) {
|
|
70
|
+
const top = (y - 1) * pixels1.width + x
|
|
71
|
+
const bottom = top + windowSize * pixels1.width
|
|
72
|
+
const a = data1[top]
|
|
73
|
+
const b = data2[top]
|
|
74
|
+
const c = data1[bottom]
|
|
75
|
+
const d = data2[bottom]
|
|
76
|
+
sumX[x] += c - a
|
|
77
|
+
sumY[x] += d - b
|
|
78
|
+
squareX[x] += c * c - a * a
|
|
79
|
+
squareY[x] += d * d - b * b
|
|
80
|
+
product[x] += c * d - a * b
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
let sx = 0,
|
|
84
|
+
sy = 0,
|
|
85
|
+
xx = 0,
|
|
86
|
+
yy = 0,
|
|
87
|
+
xy = 0
|
|
88
|
+
for (let x = 0; x < windowSize; x++) {
|
|
89
|
+
sx += sumX[x]
|
|
90
|
+
sy += sumY[x]
|
|
91
|
+
xx += squareX[x]
|
|
92
|
+
yy += squareY[x]
|
|
93
|
+
xy += product[x]
|
|
94
|
+
}
|
|
95
|
+
for (let x = 0; x < width; x++, index++) {
|
|
96
|
+
const meanx = sx / windowSquared
|
|
97
|
+
const meany = sy / windowSquared
|
|
98
|
+
// Preserve the reference fixed-point truncation and arithmetic order.
|
|
99
|
+
const varx =
|
|
100
|
+
Math.trunc(1024 * (xx / windowSquared - meanx * meanx)) / 1024
|
|
101
|
+
const vary =
|
|
102
|
+
Math.trunc(1024 * (yy / windowSquared - meany * meany)) / 1024
|
|
103
|
+
const cov =
|
|
104
|
+
Math.trunc(
|
|
105
|
+
1024 *
|
|
106
|
+
(xy / windowSquared - (sx / windowSquared) * (sy / windowSquared))
|
|
107
|
+
) / 1024
|
|
108
|
+
const na = 2 * meanx * meany + c1
|
|
109
|
+
const nb = 2 * cov + c2
|
|
110
|
+
const da = meanx * meanx + meany * meany + c1
|
|
111
|
+
const db = varx + vary + c2
|
|
112
|
+
const score = (na * nb) / (da * db)
|
|
113
|
+
if (map) ssims[index] = score
|
|
114
|
+
rowSum += score
|
|
115
|
+
// Slide horizontally through exact integer moments. The legacy
|
|
116
|
+
// 1/1024 truncation remains; pooling uses compensated row sums.
|
|
117
|
+
const next = x + windowSize
|
|
118
|
+
if (next < pixels1.width) {
|
|
119
|
+
sx += sumX[next] - sumX[x]
|
|
120
|
+
sy += sumY[next] - sumY[x]
|
|
121
|
+
xx += squareX[next] - squareX[x]
|
|
122
|
+
yy += squareY[next] - squareY[x]
|
|
123
|
+
xy += product[next] - product[x]
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
const corrected = rowSum - correction
|
|
127
|
+
const nextTotal = total + corrected
|
|
128
|
+
correction = nextTotal - total - corrected
|
|
129
|
+
total = nextTotal
|
|
130
|
+
}
|
|
131
|
+
return { data: ssims, width, height, mssim: total / (width * height) }
|
|
132
|
+
}
|
package/dist/downsample.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { Matrix, Options } from "./types";
|
|
2
|
-
/**
|
|
3
|
-
* Determines the downsizing algorithm to implement (if any) to the reference and target images
|
|
4
|
-
*
|
|
5
|
-
* @method downsample
|
|
6
|
-
* @param {[Matrix, Matrix]} pixels - The first and second matrices to downsample
|
|
7
|
-
* @param {Object} options - The inputs options object
|
|
8
|
-
* @returns {[Matrix, Matrix]} pixels - An array containing the 2 downsized images
|
|
9
|
-
* @public
|
|
10
|
-
* @memberOf downsample
|
|
11
|
-
*/
|
|
12
|
-
export declare function downsample(pixels: [Matrix, Matrix], options: Options): [Matrix, Matrix];
|
package/dist/downsample.js
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
/**
|
|
4
|
-
* Implements downsampling logic
|
|
5
|
-
*
|
|
6
|
-
* @namespace downsample
|
|
7
|
-
*/
|
|
8
|
-
var math_1 = require("./math");
|
|
9
|
-
var matlab_1 = require("./matlab");
|
|
10
|
-
/**
|
|
11
|
-
* For a given 2D filter `filter`, downsize image `pixels` by a factor of `f`.
|
|
12
|
-
*
|
|
13
|
-
* @method imageDownsample
|
|
14
|
-
* @param {Matrix} pixels - The matrix to downsample
|
|
15
|
-
* @param {Matrix} filter - The filter to convolve the image with
|
|
16
|
-
* @param {number} f - The downsampling factor (`image size / f`)
|
|
17
|
-
* @returns {Matrix} imdown - The downsampled, filtered image
|
|
18
|
-
* @private
|
|
19
|
-
* @memberOf downsample
|
|
20
|
-
*/
|
|
21
|
-
function imageDownsample(pixels, filter, f) {
|
|
22
|
-
var imdown = matlab_1.imfilter(pixels, filter, "symmetric", "same");
|
|
23
|
-
return matlab_1.skip2d(imdown, [0, f, imdown.height], [0, f, imdown.width]);
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Downsamples images greater than `maxSize` pixels on the smallest direction. If neither image
|
|
27
|
-
* exceeds these dimensions they are returned as they are.
|
|
28
|
-
*
|
|
29
|
-
* It replicates the same logic than the original matlab scripts
|
|
30
|
-
*
|
|
31
|
-
* @method originalDownsample
|
|
32
|
-
* @param {Matrix} pixels1 - The first matrix to downsample
|
|
33
|
-
* @param {Matrix} pixels2 - The second matrix to downsample
|
|
34
|
-
* @param {number} [maxSize=256] - The maximum size on the smallest dimension
|
|
35
|
-
* @returns {[Matrix, Matrix]} ssim_map - A matrix containing the map of computed SSIMs
|
|
36
|
-
* @private
|
|
37
|
-
* @memberOf downsample
|
|
38
|
-
*/
|
|
39
|
-
function originalDownsample(pixels1, pixels2, maxSize) {
|
|
40
|
-
if (maxSize === void 0) { maxSize = 256; }
|
|
41
|
-
var factor = Math.min(pixels1.width, pixels2.height) / maxSize;
|
|
42
|
-
var f = Math.round(factor);
|
|
43
|
-
if (f > 1) {
|
|
44
|
-
var lpf = matlab_1.ones(f);
|
|
45
|
-
lpf = math_1.divide2d(lpf, math_1.sum2d(lpf));
|
|
46
|
-
pixels1 = imageDownsample(pixels1, lpf, f);
|
|
47
|
-
pixels2 = imageDownsample(pixels2, lpf, f);
|
|
48
|
-
}
|
|
49
|
-
return [pixels1, pixels2];
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Determines the downsizing algorithm to implement (if any) to the reference and target images
|
|
53
|
-
*
|
|
54
|
-
* @method downsample
|
|
55
|
-
* @param {[Matrix, Matrix]} pixels - The first and second matrices to downsample
|
|
56
|
-
* @param {Object} options - The inputs options object
|
|
57
|
-
* @returns {[Matrix, Matrix]} pixels - An array containing the 2 downsized images
|
|
58
|
-
* @public
|
|
59
|
-
* @memberOf downsample
|
|
60
|
-
*/
|
|
61
|
-
function downsample(pixels, options) {
|
|
62
|
-
if (options.downsample === "original") {
|
|
63
|
-
return originalDownsample(pixels[0], pixels[1], options.maxSize);
|
|
64
|
-
}
|
|
65
|
-
// else if options.downsample === 'fast' -> the image is downsampled when read (readpixels.js)
|
|
66
|
-
// else do not downsample
|
|
67
|
-
return pixels;
|
|
68
|
-
}
|
|
69
|
-
exports.downsample = downsample;
|
|
70
|
-
//# sourceMappingURL=downsample.js.map
|
package/dist/downsample.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"downsample.js","sourceRoot":"","sources":["../src/downsample.ts"],"names":[],"mappings":";;AAAA;;;;GAIG;AACH,+BAAyC;AACzC,mCAAkD;AAGlD;;;;;;;;;;GAUG;AACH,SAAS,eAAe,CAAC,MAAc,EAAE,MAAc,EAAE,CAAS;IAChE,IAAM,MAAM,GAAG,iBAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,CAAC,CAAC;IAE7D,OAAO,eAAM,CAAC,MAAM,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;AACrE,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,SAAS,kBAAkB,CACzB,OAAe,EACf,OAAe,EACf,OAAa;IAAb,wBAAA,EAAA,aAAa;IAEb,IAAM,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC;IACjE,IAAM,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IAE7B,IAAI,CAAC,GAAG,CAAC,EAAE;QACT,IAAI,GAAG,GAAG,aAAI,CAAC,CAAC,CAAC,CAAC;QAElB,GAAG,GAAG,eAAQ,CAAC,GAAG,EAAE,YAAK,CAAC,GAAG,CAAC,CAAC,CAAC;QAEhC,OAAO,GAAG,eAAe,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC;QAC3C,OAAO,GAAG,eAAe,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC;KAC5C;IAED,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;AAC5B,CAAC;AAED;;;;;;;;;GASG;AACH,SAAgB,UAAU,CACxB,MAAwB,EACxB,OAAgB;IAEhB,IAAI,OAAO,CAAC,UAAU,KAAK,UAAU,EAAE;QACrC,OAAO,kBAAkB,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;KAClE;IACD,8FAA8F;IAC9F,yBAAyB;IACzB,OAAO,MAAM,CAAC;AAChB,CAAC;AAVD,gCAUC"}
|