Ask Your Question
-1

Get coordinates template matching python

asked 2018-09-09 11:17:41 -0500

sssyudhhh2321 gravatar image

updated 2018-09-09 14:51:55 -0500

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?

edit retag flag offensive close merge delete

Comments

you clearly did not understand the code, you were copypasting.

berak gravatar imageberak ( 2018-09-09 11:43:32 -0500 )edit

@berak I did not understand the code. And there is not much explanation on the code that I did copy. This is kind of a hybrid of many sources as the individual ones did not work. Wouldn't pt be a point though?

sssyudhhh2321 gravatar imagesssyudhhh2321 ( 2018-09-09 13:23:19 -0500 )edit

@berak I do intend on learning it, that is why I asked what do the numbers mean in the variable pt. I've watched just about every tutorial about image template matching I could find.

sssyudhhh2321 gravatar imagesssyudhhh2321 ( 2018-09-09 14:36:48 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-11-19 20:44:58 -0500

supra56 gravatar image

Try this:

res = cv2.matchTemplate(large_image, small_image, cv2.TM_CCOEFF_NORMED)
to
res = cv2.matchTemplate(large_image, small_image, cv2.TM_SQDIFF, mask=mask)
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-09-09 11:17:41 -0500

Seen: 93 times

Last updated: Nov 19