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.
Files changed (151) hide show
  1. package/CLA.md +66 -0
  2. package/CONTRIBUTING.md +119 -0
  3. package/LICENSE +1 -1
  4. package/LICENSES/LIVE Images Database +18 -0
  5. package/LICENSES/Subjective quality assessment - IVC database +5 -0
  6. package/LICENSES/Wang et al SSIM +24 -0
  7. package/LICENSES/bezkrovny +49 -0
  8. package/LICENSES/cell2csv +2 -0
  9. package/LICENSES/obartra-ssim +21 -0
  10. package/LICENSES/rhys-e +21 -0
  11. package/README.md +216 -34
  12. package/dist/bezkrovnySsim.d.ts +5 -19
  13. package/dist/bezkrovnySsim.js +113 -72
  14. package/dist/bezkrovnySsim.js.map +1 -1
  15. package/dist/browser.d.mts +9 -0
  16. package/dist/browser.d.ts +9 -0
  17. package/dist/browser.js +37 -0
  18. package/dist/browser.js.map +1 -0
  19. package/dist/browser.mjs +742 -0
  20. package/dist/browser.mjs.map +7 -0
  21. package/dist/defaults.d.ts +3 -1
  22. package/dist/defaults.js +14 -4
  23. package/dist/defaults.js.map +1 -1
  24. package/dist/gaussian.d.ts +2 -0
  25. package/dist/gaussian.js +168 -0
  26. package/dist/gaussian.js.map +1 -0
  27. package/dist/gaussianSymmetric.d.ts +3 -0
  28. package/dist/gaussianSymmetric.js +62 -0
  29. package/dist/gaussianSymmetric.js.map +1 -0
  30. package/dist/index.d.mts +9 -0
  31. package/dist/index.d.ts +8 -19
  32. package/dist/index.js +19 -88
  33. package/dist/index.js.map +1 -1
  34. package/dist/index.mjs +718 -0
  35. package/dist/index.mjs.map +7 -0
  36. package/dist/options.d.ts +2 -0
  37. package/dist/options.js +60 -0
  38. package/dist/options.js.map +1 -0
  39. package/dist/originalSsim.d.ts +3 -26
  40. package/dist/originalSsim.js +5 -57
  41. package/dist/originalSsim.js.map +1 -1
  42. package/dist/pixels.d.ts +5 -0
  43. package/dist/pixels.js +157 -0
  44. package/dist/pixels.js.map +1 -0
  45. package/dist/prepare.d.ts +7 -0
  46. package/dist/prepare.js +48 -0
  47. package/dist/prepare.js.map +1 -0
  48. package/dist/referenceSsim.d.ts +4 -0
  49. package/dist/referenceSsim.js +59 -0
  50. package/dist/referenceSsim.js.map +1 -0
  51. package/dist/result.d.ts +2 -0
  52. package/dist/result.js +20 -0
  53. package/dist/result.js.map +1 -0
  54. package/dist/ssim.d.ts +3 -21
  55. package/dist/ssim.js +4 -126
  56. package/dist/ssim.js.map +1 -1
  57. package/dist/ssim.web.js +2 -2
  58. package/dist/ssim.web.js.map +7 -1
  59. package/dist/types.d.ts +27 -7
  60. package/dist/weberSsim.d.ts +19 -0
  61. package/dist/weberSsim.js +119 -0
  62. package/dist/weberSsim.js.map +1 -0
  63. package/package.json +116 -1
  64. package/src/bezkrovnySsim.ts +133 -67
  65. package/src/browser.ts +49 -0
  66. package/src/defaults.ts +16 -6
  67. package/src/gaussian.ts +228 -0
  68. package/src/gaussianSymmetric.ts +95 -0
  69. package/src/index.ts +47 -96
  70. package/src/options.ts +74 -0
  71. package/src/originalSsim.ts +9 -79
  72. package/src/pixels.ts +198 -0
  73. package/src/prepare.ts +73 -0
  74. package/src/referenceSsim.ts +71 -0
  75. package/src/result.ts +23 -0
  76. package/src/ssim.ts +9 -168
  77. package/src/types.ts +44 -15
  78. package/src/weberSsim.ts +132 -0
  79. package/dist/downsample.d.ts +0 -12
  80. package/dist/downsample.js +0 -70
  81. package/dist/downsample.js.map +0 -1
  82. package/dist/math.d.ts +0 -155
  83. package/dist/math.js +0 -394
  84. package/dist/math.js.map +0 -1
  85. package/dist/matlab/conv2.d.ts +0 -137
  86. package/dist/matlab/conv2.js +0 -250
  87. package/dist/matlab/conv2.js.map +0 -1
  88. package/dist/matlab/filter2.d.ts +0 -18
  89. package/dist/matlab/filter2.js +0 -53
  90. package/dist/matlab/filter2.js.map +0 -1
  91. package/dist/matlab/fspecial.d.ts +0 -39
  92. package/dist/matlab/fspecial.js +0 -102
  93. package/dist/matlab/fspecial.js.map +0 -1
  94. package/dist/matlab/imfilter.d.ts +0 -24
  95. package/dist/matlab/imfilter.js +0 -84
  96. package/dist/matlab/imfilter.js.map +0 -1
  97. package/dist/matlab/index.d.ts +0 -23
  98. package/dist/matlab/index.js +0 -29
  99. package/dist/matlab/index.js.map +0 -1
  100. package/dist/matlab/internal/numbers.d.ts +0 -14
  101. package/dist/matlab/internal/numbers.js +0 -28
  102. package/dist/matlab/internal/numbers.js.map +0 -1
  103. package/dist/matlab/mod.d.ts +0 -21
  104. package/dist/matlab/mod.js +0 -27
  105. package/dist/matlab/mod.js.map +0 -1
  106. package/dist/matlab/normpdf.d.ts +0 -36
  107. package/dist/matlab/normpdf.js +0 -55
  108. package/dist/matlab/normpdf.js.map +0 -1
  109. package/dist/matlab/ones.d.ts +0 -15
  110. package/dist/matlab/ones.js +0 -22
  111. package/dist/matlab/ones.js.map +0 -1
  112. package/dist/matlab/padarray.d.ts +0 -37
  113. package/dist/matlab/padarray.js +0 -349
  114. package/dist/matlab/padarray.js.map +0 -1
  115. package/dist/matlab/rgb2gray.d.ts +0 -15
  116. package/dist/matlab/rgb2gray.js +0 -58
  117. package/dist/matlab/rgb2gray.js.map +0 -1
  118. package/dist/matlab/skip2d.d.ts +0 -34
  119. package/dist/matlab/skip2d.js +0 -55
  120. package/dist/matlab/skip2d.js.map +0 -1
  121. package/dist/matlab/sub.d.ts +0 -17
  122. package/dist/matlab/sub.js +0 -33
  123. package/dist/matlab/sub.js.map +0 -1
  124. package/dist/matlab/transpose.d.ts +0 -18
  125. package/dist/matlab/transpose.js +0 -34
  126. package/dist/matlab/transpose.js.map +0 -1
  127. package/dist/matlab/zeros.d.ts +0 -15
  128. package/dist/matlab/zeros.js +0 -22
  129. package/dist/matlab/zeros.js.map +0 -1
  130. package/dist/version.d.ts +0 -2
  131. package/dist/version.js +0 -5
  132. package/dist/version.js.map +0 -1
  133. package/src/downsample.ts +0 -81
  134. package/src/math.ts +0 -447
  135. package/src/matlab/conv2.ts +0 -272
  136. package/src/matlab/filter2.ts +0 -52
  137. package/src/matlab/fspecial.ts +0 -107
  138. package/src/matlab/imfilter.ts +0 -93
  139. package/src/matlab/index.ts +0 -23
  140. package/src/matlab/internal/numbers.ts +0 -28
  141. package/src/matlab/mod.ts +0 -23
  142. package/src/matlab/normpdf.ts +0 -57
  143. package/src/matlab/ones.ts +0 -19
  144. package/src/matlab/padarray.ts +0 -375
  145. package/src/matlab/rgb2gray.ts +0 -65
  146. package/src/matlab/skip2d.ts +0 -58
  147. package/src/matlab/sub.ts +0 -38
  148. package/src/matlab/transpose.ts +0 -33
  149. package/src/matlab/zeros.ts +0 -19
  150. package/src/types/bmp-js.d.ts +0 -1
  151. package/src/version.ts +0 -2
package/src/math.ts DELETED
@@ -1,447 +0,0 @@
1
- /**
2
- * Generates all basic arithmetic and matrix computations required
3
- *
4
- * Most of these methods use plain for loops and reduce nested calls. This results in about ~100x
5
- * improvement on SSIM computation for 512x512 images on recent versions of node (~v6.7) over
6
- * implementations using map or reduce.
7
- *
8
- * @namespace math
9
- */
10
- import { Matrix } from "./types";
11
-
12
- /**
13
- * Computes the mean value of a given array. It is the sum of a list of numbers divided by the
14
- * number of numbers in the list.
15
- *
16
- * @method average
17
- * @param {Number[]} xn - The target array
18
- * @returns {Number} average - The mean value of all elements within the array
19
- * @public
20
- * @memberOf math
21
- * @since 0.0.1
22
- */
23
- export function average(xn: number[]): number {
24
- return sum(xn) / xn.length;
25
- }
26
-
27
- /**
28
- * Computes the sum of a given array. It adds all values within the array and returns the total
29
- *
30
- * @method sum
31
- * @param {Number[]} xn - The target array
32
- * @returns {Number} sum - The total value
33
- * @private
34
- * @memberOf math
35
- * @since 0.0.1
36
- */
37
- export function sum(xn: number[]): number {
38
- let out = 0;
39
-
40
- for (let x = 0; x < xn.length; x++) {
41
- out += xn[x];
42
- }
43
- return out;
44
- }
45
-
46
- /**
47
- * Computes the largest integer less than or equal to a given number for each member of a given
48
- * array.
49
- *
50
- * @method floor
51
- * @param {Number[]} xn - The target array
52
- * @returns {Number[]} floorArr - An array with the Math.floor value for each element of the target
53
- * array
54
- * @private
55
- * @memberOf math
56
- * @since 0.0.1
57
- */
58
- export function floor(xn: number[]): number[] {
59
- const out = new Array(xn.length);
60
-
61
- for (let x = 0; x < xn.length; x++) {
62
- out[x] = Math.floor(xn[x]);
63
- }
64
-
65
- return out;
66
- }
67
-
68
- /**
69
- * Computes the sum of all elements within a matrix
70
- *
71
- * @method sum2d
72
- * @param {Matrix} A - The input matrix
73
- * @returns {Number} sum - The total value of adding each cell
74
- * @public
75
- * @memberOf math
76
- * @since 0.0.2
77
- */
78
- export function sum2d({ data }: Matrix): number {
79
- let out = 0;
80
-
81
- for (let x = 0; x < data.length; x++) {
82
- out += data[x];
83
- }
84
-
85
- return out;
86
- }
87
-
88
- /**
89
- * Adds values of two matrices of the same size
90
- *
91
- * @method add2dMx
92
- * @param {Matrix} A - The first input matrix
93
- * @param {Matrix} B - The second input matrix
94
- * @returns {Matrix} out - A matrix with a cell-by-cell sum of `A` and `B`
95
- * @private
96
- * @memberOf math
97
- * @since 0.0.2
98
- */
99
- function add2dMx(
100
- { data: ref1, width, height }: Matrix,
101
- { data: ref2 }: Matrix
102
- ): Matrix {
103
- const data = new Array(ref1.length);
104
-
105
- for (let x = 0; x < height; x++) {
106
- const offset = x * width;
107
-
108
- for (let y = 0; y < width; y++) {
109
- data[offset + y] = ref1[offset + y] + ref2[offset + y];
110
- }
111
- }
112
-
113
- return {
114
- data,
115
- width,
116
- height
117
- };
118
- }
119
-
120
- /**
121
- * Subtracts values of second matrix from the first one. It assumes both matrices are of the same
122
- * size
123
- *
124
- * @method subtract2dMx
125
- * @param {Matrix} A - The first input matrix
126
- * @param {Matrix} B - The second input matrix
127
- * @returns {Matrix} out - A matrix with a cell-by-cell subtraction of `A` minus `B`
128
- * @private
129
- * @memberOf math
130
- * @since 0.0.2
131
- */
132
- function subtract2dMx(
133
- { data: ref1, width, height }: Matrix,
134
- { data: ref2 }: Matrix
135
- ): Matrix {
136
- const data = new Array(ref1.length);
137
-
138
- for (let x = 0; x < height; x++) {
139
- const offset = x * width;
140
-
141
- for (let y = 0; y < width; y++) {
142
- data[offset + y] = ref1[offset + y] - ref2[offset + y];
143
- }
144
- }
145
-
146
- return {
147
- data,
148
- width,
149
- height
150
- };
151
- }
152
-
153
- /**
154
- * Adds a constant value two each matrix cell
155
- *
156
- * @method add2dScalar
157
- * @param {Matrix} A - The first input matrix
158
- * @param {Number} increase - The value to add
159
- * @returns {Matrix} B - The cell-by-cell matrix sum of `A` and `increase`
160
- * @private
161
- * @memberOf math
162
- * @since 0.0.2
163
- */
164
- function add2dScalar(
165
- { data: ref, width, height }: Matrix,
166
- increase: number
167
- ): Matrix {
168
- const data = new Array(ref.length);
169
-
170
- for (let x = 0; x < ref.length; x++) {
171
- data[x] = ref[x] + increase;
172
- }
173
-
174
- return {
175
- data,
176
- width,
177
- height
178
- };
179
- }
180
-
181
- /**
182
- * Adds values of two matrices of the same size or a matrix and a constant
183
- *
184
- * @method add2d
185
- * @param {Matrix} A - The first input matrix
186
- * @param {Matrix|Number} increase - The second input matrix or the constant value
187
- * @returns {Matrix} B - A matrix with a cell-by-cell sum of the first and second parameters
188
- * @public
189
- * @memberOf math
190
- * @since 0.0.2
191
- */
192
- export function add2d(A: Matrix, increase: Matrix | number): Matrix {
193
- if (typeof increase === "number") {
194
- return add2dScalar(A, increase);
195
- }
196
- return add2dMx(A, increase);
197
- }
198
-
199
- /**
200
- * Subtracts values of two matrices of the same size or a matrix and a constant
201
- *
202
- * @method subtract2d
203
- * @param {Matrix} A - The first input matrix
204
- * @param {Matrix|Number} decrease - The second input matrix or the constant value
205
- * @returns {Matrix} B - A matrix with a cell-by-cell subtraction of the first parameter minus the
206
- * second one
207
- * @public
208
- * @memberOf math
209
- */
210
- export function subtract2d(A: Matrix, decrease: Matrix | number): Matrix {
211
- if (typeof decrease === "number") {
212
- return add2dScalar(A, -decrease);
213
- }
214
- return subtract2dMx(A, decrease);
215
- }
216
-
217
- /**
218
- * Divides each matrix cell by a constant value
219
- *
220
- * @method divide2dScalar
221
- * @param {Matrix} A - The first input matrix
222
- * @param {Number} divisor - The value to divide by
223
- * @returns {Matrix} B - The cell-by-cell matrix divison of `A` and `divisor`
224
- * @private
225
- * @memberOf math
226
- * @since 0.0.2
227
- */
228
- function divide2dScalar(
229
- { data: ref, width, height }: Matrix,
230
- divisor: number
231
- ): Matrix {
232
- const data = new Array(ref.length);
233
-
234
- for (let x = 0; x < ref.length; x++) {
235
- data[x] = ref[x] / divisor;
236
- }
237
-
238
- return {
239
- data,
240
- width,
241
- height
242
- };
243
- }
244
-
245
- /**
246
- * Divides, cell-by-cell, values of two matrices of the same size
247
- *
248
- * @method divide2dMx
249
- * @param {Matrix} A - The first input matrix
250
- * @param {Matrix} B - The second input matrix
251
- * @returns {Matrix} out - A matrix with a cell-by-cell division of `A`/`B`
252
- * @private
253
- * @memberOf math
254
- * @since 0.0.2
255
- */
256
- function divide2dMx(
257
- { data: ref1, width, height }: Matrix,
258
- { data: ref2 }: Matrix
259
- ): Matrix {
260
- const data = new Array(ref1.length);
261
-
262
- for (let x = 0; x < ref1.length; x++) {
263
- data[x] = ref1[x] / ref2[x];
264
- }
265
-
266
- return {
267
- data,
268
- width,
269
- height
270
- };
271
- }
272
-
273
- /**
274
- * Divides values of two matrices of the same size or between a matrix and a constant
275
- *
276
- * @method divide2d
277
- * @param {Matrix} A - The first input matrix
278
- * @param {Matrix|Number} divisor - The second input matrix or the constant value
279
- * @returns {Matrix} B - A matrix with the cell-by-cell division of the first and second parameters
280
- * @public
281
- * @memberOf math
282
- * @since 0.0.2
283
- */
284
- export function divide2d(A: Matrix, divisor: Matrix | number): Matrix {
285
- if (typeof divisor === "number") {
286
- return divide2dScalar(A, divisor);
287
- }
288
- return divide2dMx(A, divisor);
289
- }
290
-
291
- /**
292
- * Multiplies each matrix cell by a constant value
293
- *
294
- * @method multiply2dScalar
295
- * @param {Matrix} A - The first input matrix
296
- * @param {Number} multiplier - The value to multiply each cell with
297
- * @returns {Matrix} B - The cell-by-cell matrix multiplication of `A` and `multiplier`
298
- * @private
299
- * @memberOf math
300
- * @since 0.0.2
301
- */
302
- function multiply2dScalar(
303
- { data: ref, width, height }: Matrix,
304
- multiplier: number
305
- ): Matrix {
306
- const data = new Array(ref.length);
307
-
308
- for (let x = 0; x < ref.length; x++) {
309
- data[x] = ref[x] * multiplier;
310
- }
311
-
312
- return {
313
- data,
314
- width,
315
- height
316
- };
317
- }
318
-
319
- /**
320
- * Multiplies, cell-by-cell, values of two matrices of the same size
321
- *
322
- * @method multiply2dMx
323
- * @param {Matrix} A - The first input matrix
324
- * @param {Matrix} B - The second input matrix
325
- * @returns {Matrix} out - A matrix with a cell-by-cell multiplication of `A` * `B`
326
- * @private
327
- * @memberOf math
328
- * @since 0.0.2
329
- */
330
- function multiply2dMx(
331
- { data: ref1, width, height }: Matrix,
332
- { data: ref2 }: Matrix
333
- ): Matrix {
334
- const data = new Array(ref1.length);
335
-
336
- for (let x = 0; x < ref1.length; x++) {
337
- data[x] = ref1[x] * ref2[x];
338
- }
339
-
340
- return {
341
- data,
342
- width,
343
- height
344
- };
345
- }
346
-
347
- /**
348
- * Multiplies values of two matrices of the same size or between a matrix and a constant
349
- *
350
- * @method multiply2d
351
- * @param {Matrix} A - The first input matrix
352
- * @param {Matrix|Number} multiplier - The second input matrix or the constant value
353
- * @returns {Matrix} out - A matrix with the cell-by-cell multiplication of the first and second
354
- * parameters
355
- * @public
356
- * @memberOf math
357
- * @since 0.0.2
358
- */
359
- export function multiply2d(A: Matrix, multiplier: Matrix | number): Matrix {
360
- if (typeof multiplier === "number") {
361
- return multiply2dScalar(A, multiplier);
362
- }
363
- return multiply2dMx(A, multiplier);
364
- }
365
-
366
- /**
367
- * Generates the cell-by-cell square value of a target matrix
368
- *
369
- * @method square2d
370
- * @param {Matrix} A - The target matrix
371
- * @returns {Matrix} B - A matrix with squared value of each cell
372
- * @public
373
- * @memberOf math
374
- * @since 0.0.2
375
- */
376
- export function square2d(A: Matrix): Matrix {
377
- return multiply2d(A, A);
378
- }
379
-
380
- /**
381
- * Calculates the total mean value for a given matrix
382
- *
383
- * @method mean2d
384
- * @param {Matrix} A - The target matrix
385
- * @returns {Number} mean - The total mean of each cell
386
- * @public
387
- * @memberOf math
388
- * @since 0.0.2
389
- */
390
- export function mean2d(A: Matrix): number {
391
- return sum2d(A) / A.data.length;
392
- }
393
-
394
- /**
395
- * Computes the variance for a given array
396
- *
397
- * @method variance
398
- * @param {Array<Number>} values - The target array
399
- * @param {Number} [avg=average(values)] - If specified, it will use this values as the average of
400
- * the array values. If not, it will compute the actual average
401
- * @returns {Number} varx - The resulting variance value
402
- * @public
403
- * @memberOf math
404
- */
405
- export function variance(
406
- values: number[],
407
- avg: number = average(values)
408
- ): number {
409
- let varx = 0;
410
- let i = values.length;
411
-
412
- while (i--) {
413
- varx += (values[i] - avg) ** 2;
414
- }
415
-
416
- return varx / values.length;
417
- }
418
-
419
- /**
420
- * Computes the covariance between 2 arrays
421
- *
422
- * @method covariance
423
- * @param {Array<Number>} values1 - The first target array
424
- * @param {Array<Number>} values2 - The second target array
425
- * @param {Number} [average1=average(values)] - If specified, it will use this values as the average
426
- * of the first array. If not, it will compute the actual average
427
- * @param {Number} [average2=average(values)] - If specified, it will use this values as the average
428
- * of the second array. If not, it will compute the actual average
429
- * @returns {Number} cov - The resulting covariance
430
- * @public
431
- * @memberOf math
432
- */
433
- export function covariance(
434
- values1: number[],
435
- values2: number[],
436
- average1: number = average(values1),
437
- average2: number = average(values2)
438
- ): number {
439
- let cov = 0;
440
- let i = values1.length;
441
-
442
- while (i--) {
443
- cov += (values1[i] - average1) * (values2[i] - average2);
444
- }
445
-
446
- return cov / values1.length;
447
- }
@@ -1,272 +0,0 @@
1
- import { multiply2d } from "../math";
2
- import { ones } from "./ones";
3
- import { sub } from "./sub";
4
- import { zeros } from "./zeros";
5
- import { Matrix, Shape } from "../types";
6
-
7
- /**
8
- * `C = conv2(a,b)` computes the two-dimensional convolution of matrices `a` and `b`. If one of
9
- * these matrices describes a two-dimensional finite impulse response (FIR) filter, the other matrix
10
- * is filtered in two dimensions. The size of `c` is determined as follows:
11
- *
12
- * ```
13
- * if [ma,na] = size(a), [mb,nb] = size(b), and [mc,nc] = size(c), then
14
- * mc = max([ma+mb-1,ma,mb]) and nc = max([na+nb-1,na,nb]).
15
- * ```
16
- *
17
- * `shape` returns a subsection of the two-dimensional convolution, based on one of these values for
18
- * the parameter:
19
- *
20
- * - **full**: Returns the full two-dimensional convolution (default).
21
- * - **same**: Returns the central part of the convolution of the same size as `a`.
22
- * - **valid**: Returns only those parts of the convolution that are computed without the
23
- * zero-padded edges. Using this option, `size(c) === max([ma-max(0,mb-1),na-max(0,nb-1)],0)`
24
- *
25
- * @method mxConv2
26
- * @param {Matrix} a - The first matrix
27
- * @param {Matrix} b - The second matrix
28
- * @param {String} [shape='full'] - One of 'full' / 'same' / 'valid'
29
- * @returns {Matrix} c - Returns the convolution filtered by `shape`
30
- * @private
31
- * @memberOf matlab
32
- */
33
- function mxConv2(
34
- { data: ref, width: refWidth, height: refHeight }: Matrix,
35
- b: Matrix,
36
- shape: Shape = "full"
37
- ): Matrix {
38
- const cWidth = refWidth + b.width - 1;
39
- const cHeight = refHeight + b.height - 1;
40
- const { data } = zeros(cHeight, cWidth);
41
-
42
- /**
43
- * Computing the convolution is the most computentionally intensive task for SSIM and we do it
44
- * several times.
45
- *
46
- * This section has been optimized for performance and readability suffers.
47
- */
48
- for (let r1 = 0; r1 < b.height; r1++) {
49
- for (let c1 = 0; c1 < b.width; c1++) {
50
- const br1c1 = b.data[r1 * b.width + c1];
51
-
52
- if (br1c1) {
53
- for (let i = 0; i < refHeight; i++) {
54
- for (let j = 0; j < refWidth; j++) {
55
- data[(i + r1) * cWidth + j + c1] += ref[i * refWidth + j] * br1c1;
56
- }
57
- }
58
- }
59
- }
60
- }
61
-
62
- const c = {
63
- data,
64
- width: cWidth,
65
- height: cHeight
66
- };
67
-
68
- return reshape(c, shape, refHeight, b.height, refWidth, b.width);
69
- }
70
-
71
- /**
72
- * `C = boxConv(a,b)` computes the two-dimensional convolution of a matrix `a` and box kernel `b`.
73
- *
74
- * The `shape` parameter returns a subsection of the two-dimensional convolution as defined by
75
- * mxConv2.
76
- *
77
- * @method boxConv
78
- * @param {Matrix} a - The first matrix
79
- * @param {Matrix} b - The box kernel
80
- * @param {String} [shape='full'] - One of 'full' / 'same' / 'valid'
81
- * @returns {Matrix} c - Returns the convolution filtered by `shape`
82
- * @private
83
- * @memberOf matlab
84
- */
85
- function boxConv(
86
- a: Matrix,
87
- { data, width, height }: Matrix,
88
- shape: Shape = "full"
89
- ): Matrix {
90
- const b1 = ones(height, 1);
91
- const b2 = ones(1, width);
92
- const out = convn(a, b1, b2, shape);
93
-
94
- return multiply2d(out, data[0]);
95
- }
96
-
97
- /**
98
- * Determines whether all values in an array are the same so that the kernel can be treated as a box
99
- * kernel
100
- *
101
- * @method isBoxKernel
102
- * @param {Matrix} a - The input matrix
103
- * @returns {Boolean} boxKernel - Returns true if all values in the matrix are the same, false
104
- * otherwise
105
- * @private
106
- * @memberOf matlab
107
- */
108
- function isBoxKernel({ data }: Matrix): boolean {
109
- const expected = data[0];
110
-
111
- for (let i = 1; i < data.length; i++) {
112
- if (data[i] !== expected) {
113
- return false;
114
- }
115
- }
116
- return true;
117
- }
118
-
119
- /**
120
- * `C = convn(a,b1, b2)` computes the two-dimensional convolution of matrices `a.*b1.*b2`.
121
- *
122
- * The size of `c` is determined as follows:
123
- *
124
- * ```
125
- * if [ma,na] = size(a), [mb] = size(b1), [nb] = size(b2) and [mc,nc] = size(c), then
126
- * mc = max([ma+mb-1,ma,mb]) and nc = max([na+nb-1,na,nb]).
127
- * ```
128
- *
129
- * `shape` returns a section of the two-dimensional convolution, based on one of these values for
130
- * the parameter:
131
- *
132
- * - **full**: Returns the full two-dimensional convolution (default).
133
- * - **same**: Returns the central part of the convolution of the same size as `a`.
134
- * - **valid**: Returns only those parts of the convolution that are computed without the
135
- * zero-padded edges. Using this option, `size(c) === max([ma-max(0,mb-1),na-max(0,nb-1)],0)`
136
- *
137
- * This method mimics Matlab's `convn` method but limited to 2 1 dimensional kernels.
138
- *
139
- * @method convn
140
- * @param {Matrix} a - The first matrix
141
- * @param {Matrix} b1 - The first 1-D kernel
142
- * @param {Matrix} b2 - The second 1-D kernel
143
- * @param {String} [shape='full'] - One of 'full' / 'same' / 'valid'
144
- * @returns {Matrix} c - Returns the convolution filtered by `shape`
145
- * @private
146
- * @memberOf matlab
147
- */
148
- function convn(
149
- a: Matrix,
150
- b1: Matrix,
151
- b2: Matrix,
152
- shape: Shape = "full"
153
- ): Matrix {
154
- const mb = Math.max(b1.height, b1.width);
155
- const nb = Math.max(b2.height, b2.width);
156
- const temp = mxConv2(a, b1, "full");
157
- const c = mxConv2(temp, b2, "full");
158
-
159
- return reshape(c, shape, a.height, mb, a.width, nb);
160
- }
161
-
162
- /**
163
- * `reshape` crops the resulting convolution matrix to match the values specified in `shape`.
164
- *
165
- * - **full**: Returns the input
166
- * - **same**: Returns the central part of the convolution of the same size as `a`.
167
- * - **valid**: Returns only those parts of the convolution that are computed without the
168
- * zero-padded edges
169
- *
170
- * @method reshape
171
- * @param {Matrix} c - The output matrix
172
- * @param {String} shape - One of 'full' / 'same' / 'valid'
173
- * @param {Number} ma - The number of rows of the input matrix
174
- * @param {Number} mb - The number of rows of the input filter
175
- * @param {Number} na - The number of columns of the input matrix
176
- * @param {Number} nb - The number of columns of the input filter
177
- * @returns {Matrix} c - Returns the input convolution filtered by `shape`
178
- * @private
179
- * @memberOf matlab
180
- */
181
- function reshape(
182
- c: Matrix,
183
- shape: Shape,
184
- ma: number,
185
- mb: number,
186
- na: number,
187
- nb: number
188
- ): Matrix {
189
- if (shape === "full") {
190
- return c;
191
- } else if (shape === "same") {
192
- const rowStart = Math.ceil((c.height - ma) / 2);
193
- const colStart = Math.ceil((c.width - na) / 2);
194
-
195
- return sub(c, rowStart, ma, colStart, na);
196
- }
197
-
198
- return sub(c, mb - 1, ma - mb + 1, nb - 1, na - nb + 1);
199
- }
200
-
201
- /**
202
- * `C = conv2(a,b)` computes the two-dimensional convolution of matrices `a` and `b`. If one of
203
- * these matrices describes a two-dimensional finite impulse response (FIR) filter, the other matrix
204
- * is filtered in two dimensions.
205
- *
206
- * The size of `c` is determined as follows:
207
- *
208
- * ```
209
- * if [ma,na] = size(a), [mb,nb] = size(b), and [mc,nc] = size(c), then
210
- * mc = max([ma+mb-1,ma,mb]) and nc = max([na+nb-1,na,nb]).
211
- * ```
212
- *
213
- * `shape` returns a subsection of the two-dimensional convolution, based on one of these values for
214
- * the parameter:
215
- *
216
- * - **full**: Returns the full two-dimensional convolution (default).
217
- * - **same**: Returns the central part of the convolution of the same size as `a`.
218
- * - **valid**: Returns only those parts of the convolution that are computed without the
219
- * zero-padded edges. Using this option, `size(c) === max([ma-max(0,mb-1),na-max(0,nb-1)],0)`
220
- *
221
- * Alternatively, 2 1-D filters may be provided as parameters, following the format:
222
- * `conv2(a, b1, b2, shape)`. This is similar to Matlab's implementation allowing any number of 1-D
223
- * filters to be applied but limited to 2
224
- *
225
- * This method mimics Matlab's `conv2` method.
226
- *
227
- * Given:
228
- * const A = rand(3);
229
- * const B = rand(4);
230
- *
231
- * @example conv2(A,B); // output is 6-by-6
232
- * {
233
- * data: [
234
- * 0.1838, 0.2374, 0.9727, 1.2644, 0.7890, 0.3750,
235
- * 0.6929, 1.2019, 1.5499, 2.1733, 1.3325, 0.3096,
236
- * 0.5627, 1.5150, 2.3576, 3.1553, 2.5373, 1.0602,
237
- * 0.9986, 2.3811, 3.4302, 3.5128, 2.4489, 0.8462,
238
- * 0.3089, 1.1419, 1.8229, 2.1561, 1.6364, 0.6841,
239
- * 0.3287, 0.9347, 1.6464, 1.7928, 1.2422, 0.5423
240
- * ],
241
- * width: 6,
242
- * height: 6
243
- * }
244
- *
245
- * @example conv2(A,B,'same') => // output is the same size as A: 3-by-3
246
- * {
247
- * data: [
248
- * 2.3576, 3.1553, 2.5373,
249
- * 3.4302, 3.5128, 2.4489,
250
- * 1.8229, 2.1561, 1.6364
251
- * ],
252
- * width: 3,
253
- * height: 3
254
- * }
255
- *
256
- * @method conv2
257
- * @param {Array} args - The list of arguments, see `mxConv2` and `convn` for the exact parameters
258
- * @returns {Matrix} c - Returns the convolution filtered by `shape`
259
- * @public
260
- * @memberOf matlab
261
- * @since 0.0.2
262
- */
263
- export function conv2(
264
- ...args: Parameters<typeof boxConv | typeof convn | typeof mxConv2>
265
- ) {
266
- if (args[2] && (args[2] as Matrix).data) {
267
- return convn(...(args as Parameters<typeof convn>));
268
- } else if (isBoxKernel(args[1])) {
269
- return boxConv(...(args as Parameters<typeof boxConv>));
270
- }
271
- return mxConv2(...(args as Parameters<typeof mxConv2>));
272
- }