UNPKG

fast-sobel-tfjs

Version:

GPU-accelerated Sobel edge detection for TensorFlow.js - 5-10x faster than CPU implementations

60 lines (59 loc) 2.7 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import * as tf from "@tensorflow/tfjs"; // Export main class export { SobelFilter } from "./sobel"; // Export utility functions export { getAvailableKernelSizes, isValidKernelSize } from "./kernels"; export { getAvailableOutputFormats, isValidOutputFormat } from "./processors"; /** * Detects edges in an image using the Sobel operator with optimal settings * * @param input Input image (ImageData or tensor) * @param useGrayscale Whether to convert RGB images to grayscale (default: true) * @returns Promise resolving to processed data */ export function detectEdges(input_1) { return __awaiter(this, arguments, void 0, function* (input, useGrayscale = true) { const { SobelFilter } = yield import("./sobel"); const options = { kernelSize: 3, output: "normalized", normalizationRange: [0, 255], grayscale: useGrayscale, }; if (input instanceof ImageData) { const filter = new SobelFilter(options); return yield filter.processImageData(input); } else { return tf.tidy(() => { const filter = new SobelFilter(options); return filter.applyToTensor(input); }); } }); } /** * Creates a static factory function that returns a SobelFilter with preset configuration * * @param defaultOptions Default options for the filter factory * @returns A factory function that creates SobelFilter instances */ export function createSobelFilterFactory(defaultOptions) { return function createFilter(overrideOptions) { return __awaiter(this, void 0, void 0, function* () { const { SobelFilter } = yield import("./sobel"); return new SobelFilter(Object.assign(Object.assign({}, defaultOptions), overrideOptions)); }); }; } // Re-export utility functions for convenience export { imageDataToCanvas, normalizeTensor, pixelArrayToTensor, processHTMLImage, tensorToImageData, } from "./utils";