Image Matching
nl-matcher
Next-level image matching for nut.js with multi-scale search, alpha masking, and match validation.
Overview
The @nut-tree/nl-matcher plugin provides a next-level image matching module for nut.js. Compared to the template-matcher, it offers Apple Silicon support, multiple instance detection via screen.findAll(), alpha-channel masking, and improved performance (~1.7x faster).
Multi-Scale Search
Search at multiple scales to handle resolution differences
searchMultipleScales: trueAlpha Masking
Use the alpha channel to mask irrelevant image regions
applyAlphaMask: trueMatch Validation
Verify matches to reduce false positives
validateMatches: trueInstallation
npm i @nut-tree/nl-matcherSubscription Required
Quick Reference
useNlMatcher
useNlMatcher()Activate the nl-matcher image matching provider
Usage
Activate nl-matcher by calling useNlMatcher() before performing any image searches:
import { screen, imageResource } from "@nut-tree/nut-js";
import { useNlMatcher } from "@nut-tree/nl-matcher";
useNlMatcher();
const img = await screen.findAll(imageResource("..."));Configuration
nl-matcher accepts a NlMatcherProviderData object via the providerData option to control matching behavior:
searchMultipleScales
Default: true. When enabled, nl-matcher searches for images at multiple scales. Disabling this searches only at the original scale, which improves speed when you know the scale is constant:
const img = await screen.findAll(imageResource("..."), {
providerData: {
searchMultipleScales: false,
},
});scaleSteps
Default: [0.9, 0.8, 0.7, 0.6, 0.5]. Defines the scale factors used when searchMultipleScales is enabled. The matcher always searches at scale 1.0 (original size) plus these additional factors:
const img = await screen.find(imageResource("..."), {
providerData: {
scaleSteps: [0.9, 0.8, 0.7],
},
});useGrayScale
Default: false. Converts images to grayscale before matching. This can improve search speed at the cost of potentially reduced accuracy:
const img = await screen.find(imageResource("..."), {
providerData: {
useGrayScale: true,
},
});applyAlphaMask
Default: false. Uses the alpha channel of the reference image to mask out irrelevant regions during matching. Useful for icons or elements on varying backgrounds:
const img = await screen.find(imageResource("icon-with-transparency.png"), {
providerData: {
applyAlphaMask: true,
},
});Performance Note
validateMatches
Default: false. Performs an additional verification step on each match to reduce false positives. This may slightly increase processing time and can affect confidence values:
const img = await screen.find(imageResource("submit-button.png"), {
providerData: {
validateMatches: true,
},
});Combining Options
All provider options can be combined in a single search:
const img = await screen.find(imageResource("button.png"), {
confidence: 0.9,
providerData: {
searchMultipleScales: true,
scaleSteps: [0.9, 0.8],
useGrayScale: false,
applyAlphaMask: true,
validateMatches: true,
},
});Options Reference
searchMultipleScales
searchMultipleScales?: booleanSearch at multiple scales. Disabling searches only at original scale.
scaleSteps
scaleSteps?: number[]Scale factors for multi-scale search. The original scale (1.0) is always included.
useGrayScale
useGrayScale?: booleanConvert images to grayscale before matching. Faster but potentially less accurate.
applyAlphaMask
applyAlphaMask?: booleanUse the alpha channel to mask irrelevant image regions during matching.
validateMatches
validateMatches?: booleanPerform additional verification to reduce false positives. May slightly affect confidence values.
Best Practices
Performance
- Disable
searchMultipleScaleswhen the target scale is known - Reduce
scaleStepsto only the sizes you expect - Use
searchRegionto narrow the search area - Enable
useGrayScalewhen color is not important
Accuracy
- Enable
validateMatchesfor critical interactions - Use
applyAlphaMaskfor icons on varying backgrounds - Capture reference images at the same resolution as the target screen