Tôi đã rối tung xung quanh với các ràng buộc OpenCV cho python trong một thời gian bây giờ và tôi muốn thử mẫu phù hợp, tôi nhận được lỗi này và tôi không có ý tưởng tại saoPython OpenCV Template Matching lỗi
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
Bất cứ ai có bất kỳ manh mối nào về việc tại sao điều này có thể xảy ra không?
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()
Tôi đã chỉnh sửa mã nguồn thành câu hỏi (các liên kết có thể bị hỏng trong tương lai). Nếu bạn không muốn hiển thị toàn bộ mã, vui lòng chỉnh sửa câu hỏi để lại [mcve] (http://stackoverflow.com/help/mcve) – Miki
Bạn không nên áp dụng 'matchTemplate' vào' img2' thay vì ' img'? Bạn đang tạo ra một bot cho heartstone? : D – Miki
@Miki Bạn có thể bị bệnh nếu thấy nó hoạt động và viết và viết những phát hiện của tôi sau: D, không im không làm bot. Thực hiện theo repo github nếu bạn muốn, bạn có thể tìm thấy sản phẩm cuối cùng thú vị. – Frederikxyz