UNPKG

image-js

Version:

Image processing and manipulation in JavaScript

37 lines 1.34 kB
// @ts-expect-error: median-quisckselect has no types import quickMedian from 'median-quickselect'; /** * Returns the median pixel of the image. The median is computed on each channel individually. * @param image - Image to process. * @param options - Median options. * @returns Median pixel. */ export function median(image, options) { const pixel = new Array(image.channels).fill(0); if (options) { if (options.points.length === 0) { throw new RangeError('Array of coordinates is empty.'); } for (let i = 0; i < image.channels; i++) { const channel = []; for (const point of options.points) { if (point.column < 0 || point.column >= image.width || point.row < 0 || point.row >= image.height) { throw new RangeError(`Invalid coordinate: {column: ${point.column}, row: ${point.row}}.`); } channel.push(image.getValueByPoint(point, i)); } pixel[i] = quickMedian(channel); } } else { for (let i = 0; i < image.channels; i++) { const channel = image.getChannel(i); pixel[i] = quickMedian(channel); } } return pixel; } //# sourceMappingURL=median.js.map