ml-matrix-convolution
Version:
Matrix convolution: It offers the direct and the fourier transform convolution
76 lines (66 loc) • 1.98 kB
JavaScript
import { toBeDeepCloseTo, toMatchCloseTo } from 'jest-matcher-deep-close-to';
import { describe, expect, it } from 'vitest';
import * as MatrixConvolution from '../index.js';
expect.extend({ toBeDeepCloseTo, toMatchCloseTo });
let rows = 5;
let cols = 5;
let matrix = new Array(rows);
for (let i = 0; i < rows; i++) {
matrix[i] = new Array(cols);
for (let j = 0; j < cols; j++) {
matrix[i][j] = 1;
}
}
let kerne11 = [
[],
[],
[],
];
let kerne12 = [
[],
[],
];
let result1 = [
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
];
let result2 = [
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
];
let smallFilter = [
[],
[],
[],
[],
[],
[],
[],
[],
[],
];
describe('Direct convolution', () => {
it('Odd number of rows and columns', () => {
let conv = MatrixConvolution.direct(matrix, kerne11);
expect(conv).toMatchCloseTo(result1, 1e-8);
});
it('Even number of rows and columns', () => {
let conv = MatrixConvolution.direct(matrix, kerne12);
expect(conv).toMatchCloseTo(result2, 1e-8);
});
});
describe('FFT convolution', () => {
it('Odd number of rows and columns', () => {
let conv = MatrixConvolution.fft(matrix, kerne11);
expect(conv).toMatchCloseTo(result1, 1e-8);
});
it('Even number of rows and columns', () => {
let conv = MatrixConvolution.fft(matrix, kerne12);
expect(conv).toMatchCloseTo(result2, 1e-8);
});
});
describe('KernelFatory', () => {
it('LoG', () => {
// eslint-disable-next-line new-cap
let kernel = MatrixConvolution.kernelFactory.LoG(1.4, 9, { factor: 40 });
expect(kernel).toMatchCloseTo(smallFilter, 1e-8);
});
});