UNPKG

image-js

Version:

Image processing and manipulation in JavaScript

44 lines 1.61 kB
import { PolynomialRegression2D } from 'ml-regression-polynomial-2d'; import checkProcessable from '../utils/validators/checkProcessable.js'; /** * Corrects background from an image for baseline correction. * @param image - Image to subtract background from. * @param options - CorrectBackgroundOptions. * @returns Image with corrected baseline. */ export function correctBackground(image, options) { const { background, order = 2, backgroundKind = 'light' } = options; checkProcessable(image, { colorModel: ['GREY'] }); const columns = new Array(); const rows = new Array(); const values = new Array(); for (const point of background) { columns.push(point.column); rows.push(point.row); values.push(image.getValueByPoint(point, 0)); } const model = new PolynomialRegression2D({ x: columns, y: rows }, values, { order, }); const points = { x: [], y: [] }; for (let row = 0; row < image.height; row++) { for (let column = 0; column < image.width; column++) { points.x.push(column); points.y.push(row); } } const Y = model.predict(points); for (let row = 0; row < image.height; row++) { for (let column = 0; column < image.width; column++) { const value = Math.abs(image.getValue(column, row, 0) - Y[row * image.width + column]); image.setValue(column, row, 0, value); } } if (backgroundKind === 'light') { return image.invert(); } else { return image; } } //# sourceMappingURL=correctBackground.js.map