opencv4nodejs
opencv4nodejs copied to clipboard
Finding an image inside image
I want to know whether it's possible to use this lib to find if an image buffer is inside another image buffer. For example to identify objects in a screen screenshot. I have both buffers, one of a larger image and one of a smaller image.
I'm new to OpenCV and it would be really helpful if someone can guide me in the right direction.
Thanks
Yes, example below:
const haystack = cv.imdecodeAsync(screenshotImage);
const needle = cv.imdecodeAsync(templateImage)
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 you need to find all matches, just paint out matched matrix and calculate next max value with minMaxLocAsync
async function findAll({ haystack, needle, treshold, maxResults }) {
const result = [];
const matched = await haystack.matchTemplateAsync(
needle,
cv.TM_CCOEFF_NORMED
);
const w = needle.cols;
const h = needle.rows;
while (1) {
if (maxResults > 0 && result.length >= maxResults) {
break;
}
const minMax = await matched.minMaxLocAsync();
if (minMax.maxVal < treshold) {
break;
}
const { x, y } = minMax.maxLoc;
matched.drawRectangle(
new cv.Rect(x, y, w, h),
new cv.Vec3(0, 0, 0),
-1,
cv.LINE_8
);
result.push({ x, y, accuracy: minMax.maxVal });
}
return result;
}
Thanks a lot, I will try this out!