I am trying to locate the coordinates of an image inside another image. Currently it works as long as the colours are the same, but I want it to not mind any colours.
I found this example which I tried to use applying a grayscale filter:
import cv from "@u4/opencv4nodejs";
const MIN_ACCURACY = 0.8;
export async function getImageCoordinates(source: Buffer, template: Buffer) {
const haystack = await cv.imdecodeAsync(source, cv.IMREAD_GRAYSCALE);
const needle = await cv.imdecodeAsync(template, cv.IMREAD_GRAYSCALE);
const matched = await haystack.matchTemplateAsync(
needle,
cv.TM_CCOEFF_NORMED
);
const minMax = await matched.minMaxLocAsync();
const { x, y } = minMax.maxLoc;
const accuracy = minMax.maxVal;
if (accuracy > MIN_ACCURACY) {
return { x, y };
}
return undefined;
}
The shade of gray is of course different when the colours are not the same, so unfortunately this does not work. It should also locate coordinates when the colours are different.