Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 template matching: more than one template?
#1
Hello

i am very new to python and ML. I am trying to do some template mathing with opencv. I succeed doing template matching with one single template but how can I make it work with 2 or more templates? I guess I somehow have to insert all the tamplates in an array and then loop thru all of them while doing cv2.matchTemplate?

img_bgr = cv2.imread('images/image1.png')
img_gray = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY)

template = cv2.imread('templates/template1.png', 0)
w, h = template.shape[::-1]

result = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
# threshold
threshold = 0.8
loc = np.where(result >= threshold)

for pt in zip(*loc[::-1]):
	cv2.rectangle(img_bgr, pt, (pt[0]+w, pt[1]+h), (0, 255, 255), 1)

cv2.imshow('detected', img_bgr)
cv2.waitKey(0)
Quote
#2
You are likely needed to write a generator that return templates, e.g.

import glob, os

def get_templates(path='./templates', template_mask='png'):
    for f in glob.glob(os.path.join(path, '**', '*.' + template_mask)):
        yield cv2.imread(f, 0)

# now you can iterate over all templates 

for template in get_templates():
    result = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
    # do some stuff... 
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Match two tables to select exactly matching items klllmmm 1 824 Nov-25-2017, 05:42 PM
Last Post: heiner55
  Feature Matching trabis03 2 2,014 Jun-23-2017, 02:15 PM
Last Post: trabis03

Forum Jump:


Users browsing this thread: 1 Guest(s)