justadudewhohacks/opencv4nodejs

matchTemplate - multi object template matching.

animosi opened this issue · 1 comments

matchTemplate using python returns an array of possible matches which you can filter using threshhold. Opencv4nodejs's matchTemplate returns one Mat.
I can't find an example of multi-object template matching using JS anywhere on google, yourtube, github, or stackoverflow.

I see a similar thread opened in 2018 which was never resolved. Are there other Mat functions which I should be utilizing to achieve my goal.

const cv = require("opencv4nodejs");

async function findMatchingTemplate() {
  const threshold = 0.8;
  const bigImgMat = await cv.imreadAsync("./assets/source.png");
  const subImgMat = await cv.imreadAsync("./assets/coin.png");

  const matched = bigImgMat.matchTemplate(subImgMat, cv.TM_CCOEFF_NORMED);
  const dataList = matched.getDataAsArray();

  for (let y = 0; y < dataList.length; y++) {
    for (let x = 0; x < dataList[y].length; x++) {
      if (dataList[y][x] > threshold) {
        bigImgMat.drawRectangle(
          new cv.Rect(x, y, subImgMat.cols, subImgMat.rows),
          new cv.Vec3(0, 255, 0),
          2,
          cv.LINE_8
        );
      }
    }
  }

  cv.imshow('multiple matches', bigImgMat);
  cv.waitKey();
}

source.png

mario

coin.png
coin

result:
result