@ineentho/subimage0.0.8 • Public • Published
Fuzzy search for subimage within image. Tolerates color drift and bad pixels.
npm install @ineentho/subimage
yarn add @ineentho/subimage
How to use
const fs =subimage =let image = fstemplate = fsimage = await subimageutiltemplate = await subimageutillet results = await subimage
search(image, template, [options])
image and template
Image object should have the following example structure:
width: 10height: 10channels: 3data: <Buffer ff ff ff ...>
widthNumber - image width
heightNumber - image height
channelsNumber - optional, number of color channels in an image, possible values: 1-4
dataBuffer - image pixel data
channels is optional and is only used for
data length validation.
data should be of type buffer with pixel data arranged from top-leftmost to bottom-rightmost pixel. Possible channel orders are listed bellow:
- K (grayscale)
- KA (grayscale + alpha)
For example, let's say we have a 2x2 pixel image with red background and blue pixel on the bottom left corner. So the data buffer would look like this:
<Buffer ff 00 00 ff 00 00 00 00 ff ff 00 00>
Two options are supported:
colorToleranceNumber - the maximum range in color difference between two matched pixels to constitute a match.
pixelToleranceNumber - the number of not matching (bad) pixels to ignore and treat subimage as still matching.
pixelTolerance can be used together.
colorTolerance is combined for all color channels. For example, if
colorTolerance == 10, then the difference for R channel can be 6, G - 4, and B should match exactly, for the pixel color to be treated as matching.
The search function returns an array of result objects. If there were no matches of the subimage within the template, the result array will be empty. The result object has 3 properties:
accuracy. The later doesn't bear any strict meaning and is only used for ordinal comparison. The smaller the
accuracy value, the more accurate the match between the template and the subimage is.
x: 2 y: 2 accuracy: 0
Under the hood