weixin_54329216
weixin_54329216
2021-03-20 21:07
采纳率: 0%
浏览 26

请问我用pycharm运行一个代码出问题能帮我看看出错的问题是什么吗?谢谢

我用的是pycharm运行一个程序出错,环境变量也加过了,opencv-python也加了,但还是出错,请求各位大佬帮帮忙看一下,谢谢。

这是我的代码:

import cv2
import numpy as np




def template_demo():
    tpl = cv2.imread("D:\\xuexiaotubiao\\zxc-4.jpg")#要找的目标(括号里面是路径,路径不能有中文)
    target = cv2.imread("D:\\xuexiaotubiao\\stop8.jpg")#场景图
    #cv2.imshow("template_image", tpl)
    #cv2.imshow("target image", target)
    #methods = [cv2.TM_CCOEFF_NORMED, cv2.TM_SQDIFF_NORMED, cv2.TM_CCORR_NORMED]
    
    th, tw = tpl.shape[:2]
    print(th)
    print(tw)

    result = cv2.matchTemplate(target, tpl, cv2.TM_CCOEFF_NORMED)#原始图像中查找并匹配图像模板中的内容,并设置阈值
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)

    tl = max_loc
    br = (tl[0] + tw, tl[1] + th)
    cv2.rectangle(target, tl, br, (0, 0, 255), 10)

    cv2.namedWindow('found', 0)#调节图片大小
    cv2.imshow('found',target)


if __name__ == "__main__":
    img = cv2.imread("D:\\xuexiaotubiao\\img1.jpg")
    # cv2.namedWindow("input image", cv2.WINDOW_AUTOSIZE)
    # cv2.imshow("input image", img)
    template_demo()
    cv2.waitKey(0)
    cv2.destroyAllWindows()

出错的信息是:

C:\Users\ASUS\PycharmProjects\untitled\venv\Scripts\python.exe D:/xuexiaotubiao/test.py
Traceback (most recent call last):
  File "D:\xuexiaotubiao\test.py", line 33, in <module>
    template_demo()
  File "D:\xuexiaotubiao\test.py", line 14, in template_demo
    th, tw = tpl.shape[:2]
AttributeError: 'NoneType' object has no attribute 'shape'

Process finished with exit code 1
各位大佬帮帮忙看看,谢谢各位

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • PY0312
    Nick Peng 2021-03-20 23:17

    tpl读取的结果是None呢

    点赞 评论
  • PY0312
    Nick Peng 2021-03-24 08:24

    点赞 评论

相关推荐