UNPKG

@thi.ng/pixel-analysis

Version:

Image color & feature analysis utilities

43 lines (42 loc) 1.27 kB
import { convolveImage, EDGE5, SOBEL_X, SOBEL_Y } from "@thi.ng/pixel-convolve/convolve"; import {} from "@thi.ng/pixel/float"; import { FLOAT_GRAY } from "@thi.ng/pixel/format/float-gray"; import { FLOAT_GRAY_RANGE } from "@thi.ng/pixel/format/float-gray-range"; import { magSq } from "@thi.ng/vectors/magsq"; const { sqrt } = Math; const FMT_EDGE = FLOAT_GRAY_RANGE(-24, 24); const FMT_SOBEL = FLOAT_GRAY_RANGE(-4, 4); const analyzeFeatures = (img) => { const { width, height } = img; const numPixels = width * height; const $img = img.format !== FLOAT_GRAY ? img.as(FLOAT_GRAY) : img; const imgEdge = convolveImage($img, { kernel: EDGE5 }); imgEdge.format = FMT_EDGE; const imgSobelX = convolveImage($img, { kernel: SOBEL_X }); imgSobelX.format = FMT_SOBEL; const imgSobelY = convolveImage($img, { kernel: SOBEL_Y }); imgSobelY.format = FMT_SOBEL; const edge = sqrt(magSq(imgEdge.data)) / numPixels; const sobX = magSq(imgSobelX.data); const sobY = magSq(imgSobelY.data); const sobelX = sqrt(sobX) / numPixels; const sobelY = sqrt(sobY) / numPixels; const sobel = sqrt(sobX + sobY) / numPixels; return { imgEdge, imgSobelX, imgSobelY, edge, sobelX, sobelY, sobel }; }; export { analyzeFeatures };