Template Matching with Multiple Objects C++ Code

asked 2020-11-02 16:55:50 -0600

moisu gravatar image

updated 2020-11-02 17:10:49 -0600

I am trying to implement template matching for multiple objects using C++, but I am unsure how to implement the example given in the documentation in C++. I have looked online for solutions, but none seem to approach it the same way the documentation does. Does anyone know how to write the example given in the documentation in C++ (code below)?

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img_rgb = cv.imread('mario.png')
img_gray = cv.cvtColor(img_rgb, cv.COLOR_BGR2GRAY)
template = cv.imread('mario_coin.png',0)
w, h = template.shape[::-1]
res = cv.matchTemplate(img_gray,template,cv.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where( res >= threshold)
for pt in zip(*loc[::-1]):
    cv.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)

Specifically, I am unsure how to translate the following lines from Python to C++:

w, h = template.shape[::-1]

loc = np.where( res >= threshold)

for pt in zip(*loc[::-1]):
edit retag flag offensive close merge delete

Comments

did you try this code

sturkmen gravatar imagesturkmen ( 2020-11-02 17:09:33 -0600 )edit