Ask Your Question

Revision history [back]

Get coordinates template matching python

I used this code to do some template matching:

import cv2
import numpy

large_image  = cv2.imread('image1.png')
small_image = cv2.imread('image2.png')

null, w, h = small_image.shape[::-1]

res = cv2.matchTemplate(large_image, small_image, cv2.TM_CCOEFF_NORMED)
loc = numpy.where(res >= 0.7)

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

cv2.imwrite('something.png', suh)

large_image is this one: C:\fakepath\image1.png small_image is this one: C:\fakepath\image2.png

The rectangle was created so that part worked, but I just wanted the two coordinates of the top left either checker boards. I wanted to use the pt variable, but there are too many of them and I don't know what they mean. How can I get the location in pixels of either boxes?

Get coordinates template matching python

I used this code to do some template matching:

import cv2
import numpy

large_image  = cv2.imread('image1.png')
small_image = cv2.imread('image2.png')

null, w, h = small_image.shape[::-1]

res = cv2.matchTemplate(large_image, small_image, cv2.TM_CCOEFF_NORMED)
loc = numpy.where(res >= 0.7)

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

cv2.imwrite('something.png', suh)

large_image is this one: C:\fakepath\image1.png small_image is this one: C:\fakepath\image2.png

The rectangle was created so that part worked, but I just also wanted the two coordinates of the top left either checker boards. I wanted tried to use the pt variable, do a print(pt), but there are too many I got a few hundred sets of them and I don't know what they mean. numbers. How can I get the location in pixels of either both boxes?

Get coordinates template matching python

I used this code to do some template matching:

import cv2
import numpy

large_image  = cv2.imread('image1.png')
small_image = cv2.imread('image2.png')

null, w, h = small_image.shape[::-1]

res = cv2.matchTemplate(large_image, small_image, cv2.TM_CCOEFF_NORMED)
loc = numpy.where(res >= 0.7)

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

cv2.imwrite('something.png', suh)

large_image is this one: C:\fakepath\image1.png small_image is this one: C:\fakepath\image2.png

The rectangle was created so that part worked, but I also wanted the two coordinates of the top left either checker boards. I tried to do a print(pt), but I got a few hundred sets of numbers. How can I get the location in pixels of both boxes?