练习图像匹配,python3.6,使用 Image.open(screen,mode=“r”)打开图片失败,报错’numpy.ndarray’ object has no attribute ‘read’
import cv2,os
from PIL import Image
base_path = os.path.join(os.getcwd(), 'src')
screen= cv2.imread(os.path.join(base_path,"screen.png"))
def compare():
# standard = cv2.imread(os.path.join(base_path,"screen.png"))
pice = cv2.imread(os.path.join(base_path,"pice.png"))
result = cv2.matchTemplate(screen,pice,cv2.TM_CCOEFF_NORMED)
min,max,minLoc,maxLoc = cv2.minMaxLoc(result)
print(min,max,minLoc,maxLoc)
print(pice.shape)
pos_x = maxLoc[0] + int(pice.shape[1] / 2)
pos_y = maxLoc[1] + int(pice.shape[0] / 2)
if max > 0.8:
return pos_x,pos_y
else:
return 0,0
def getCut():
img = Image.open(screen,mode="r")
w, h = img.size
print(w, h)
# newScreen = img.resize(())
if __name__ == '__main__':
getCut()
# x, y = compare()
# print(x, y)
pass
遇到问题 Assertion failed (corrsize.height <= img.rows + templ.rows - 1 && corrsize.width <= img.cols + templ.cols - 1)
通过以下办法已解决
opencv 使用常见问题
链接:https://blog.csdn.net/weixin_34411563/article/details/89579702