0

I was trying to use cv2.matchTemplate.

My code :

!pip install opencv-Python

import cv2
import numpy as np

imgpath = ('E:\\nenv\\venv\\Images')

farm_img = cv2.imread(imgpath+'\\ImageFarm.PNG', cv2.IMREAD_COLOR)
mushray_img = cv2.imread(imgpath+'\\Mushray.PNG', cv2.IMREAD_COLOR)

# (farm_img and mushray_img both open and I see an image)
cv2.imshow('Farm', farm_img)
cv2.waitKey()
cv2.destroyAllWindows()

cv2.imshow('mush', mushray_img)
cv2.waitKey()
cv2.destroyAllWindows()

result = cv2.matchTemplate(farm_img, mushray_img, cv2.TM_CCOEFF_NORMED)

# (This doesn't work because I can't define 'result')
cv2.imshow ('Result', result)
cv2.waitKey()
cv2.destroyAllWindows()

I tried to change in my code :

farm_img = cv2.imread(imgpath+'\\ImageFarm.PNG', cv2.IMREAD_UNCHANGED)
mushray_img = cv2.imread(imgpath+'\\Mushray.PNG', cv2.IMREAD_UNCHANGED)

for

farm_img = cv2.imread(imgpath+'\\ImageFarm.PNG', cv2.IMREAD_COLOR)
mushray_img = cv2.imread(imgpath+'\\Mushray.PNG', cv2.IMREAD_COLOR)

Which didn't work

I have found the error: My template was bigger than the original image so it didn't work. Also, after some testing maybe the size of the template wasnt in the same aspect ratio. I tried to use a template1 (which was smaller and from the image1) with 1000x1000 image1 which worked. I also tried to use template1 with image2 which was bigger 2000x2000 and also maybe was less zoomed in(I was playing on wakfu) and it didn<T work.

Found it on this post : python cv2.matchTemplate stops working when I switch from image to video frame From the first comment by fmw42

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36

0 Answers0