19

I've been messing around with the OpenCV bindings for python for a while now and i wanted to try template matching, i get this error and i have no idea why

C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\imgproc\src\templmatch.cpp:910: error: (-215) (depth == CV_8U || depth == CV_32F) && type == _templ.type() && _img.dims() <= 2 in function cv::matchTemplate

Anyone have any clues as to why this might be happening?

Source code:

import win32gui
from PIL import ImageGrab
import win32api, win32con
import numpy
deckVar = "deck.png" # Temporary

def click(x,y):
    win32api.SetCursorPos((x,y))
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)

margin = 10

def OOO(): # Order Of Operations
    print None

def main():
    deck = "test"

    img = ImageGrab.grab()

    HWNDHandle = win32gui.FindWindow(None, "Hearthstone");
    x,y,x2,y2 = win32gui.GetWindowRect(HWNDHandle)
    print x,y,x2,y2
    l = x2-x
    h = y2-y
    print l,h

    img_recog(img,"imgs/my_collection.png")

def img_recog(img,templ):
    import cv2
    import numpy as np
    from matplotlib import pyplot as plt


    img2 = numpy.array(img.getdata()).reshape(img.size[0], img.size[1], 3)
    template = cv2.imread(templ,0)
    w, h = template.shape[::-1]

    # All the 6 methods for comparison in a list
    methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR',
                'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED']


    img = img2.copy()
    method = eval(methods[1])

    # Apply template Matching
    try:
        res = cv2.matchTemplate(img,template,method)
    except Exception as e:
        print str(e)
        raw_input()
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)


    # If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
    if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
        top_left = min_loc
    else:
        top_left = max_loc
    bottom_right = (top_left[0] + w, top_left[1] + h)

    return cv2.rectangle(img,top_left, bottom_right, 255, 2)


main()  
3
  • I edited the source code into the question (links may broke in the future). If you don't want to display you full code, please edit the question leaving a mcve Commented Sep 15, 2015 at 18:38
  • Shouldn't you apply matchTemplate to img2 instead of img? Are you creating a bot for heartstone? :D Commented Sep 15, 2015 at 19:24
  • @Miki You might be right ill see if it works and write and write my findings later :D, no im not making a bot. Follow the github repo if you want, you might find the final product interesting. Commented Sep 16, 2015 at 6:22

2 Answers 2

29

Pay attention to the error message:

error: (-215) (depth == CV_8U || depth == CV_32F) && type == _templ.type() && _img.dims() <= 2 in function cv::matchTemplate

It means the data type of the image should be CV_8U or CV_32F, and it should have 3 or less channels.

If you don't know what CV_8U, CV_32F means, see this list.

Probably you are passing numpy objects other than np.uint8 or np.float32. you can easily convert numpy dtype to 8-bit or 32-bit using:

img.astype(np.float32)
img.astype(np.uint8)

Just pay attention that OpenCV expect CV_8U 8-bit data to be in the range 0..255 and CV_32F can be in any range.

Sign up to request clarification or add additional context in comments.

2 Comments

in order to check the depth of image or match template, print img.dtype/ template.dtype. as for channels, it's the third value in image.shape - to get rid of channels which should be 2 or less, not 3 or less, as mentioned in the comment - try to convert them to gray.
@fanny. That's useful!
1

I just change .png pic to .jpg type, and it went through.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.