image-js
Version:
Image processing and manipulation in JavaScript
26 lines • 1.07 kB
JavaScript
import { match } from 'ts-pattern';
import { PREWITT_X, PREWITT_Y, SCHARR_X, SCHARR_Y, SOBEL_X, SOBEL_Y, } from '../utils/constants/kernels.js';
export const DerivativeFilter = {
SOBEL: 'sobel',
SCHARR: 'scharr',
PREWITT: 'prewitt',
// todo: handle even sized kernels to implement Roberts' filter
// for 2x2 matrices, the current pixel corresponds to the top-left
// ROBERTS = 'roberts',
};
/**
* Apply a derivative filter to an image.
* @param image - Image to process.
* @param options - Derivative filter options.
* @returns The processed image.
*/
export function derivativeFilter(image, options = {}) {
const { filter = 'sobel' } = options;
const kernels = match(filter)
.with('sobel', () => ({ kernelX: SOBEL_X, kernelY: SOBEL_Y }))
.with('scharr', () => ({ kernelX: SCHARR_X, kernelY: SCHARR_Y }))
.with('prewitt', () => ({ kernelX: PREWITT_X, kernelY: PREWITT_Y }))
.exhaustive();
return image.gradientFilter({ ...kernels, ...options });
}
//# sourceMappingURL=derivativeFilter.js.map