vscode中opencv导入图片提示错误error 215 is not subscriptable has no attribute ‘shape‘等

错误为

error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

解决方法

  1. 网上的解答是说图片路径有问题,所以将图片路径改为绝对路径

    ‘D:\appdata\vscode\python\ph.jpg’

    完整程序为

     import numpy as np
     import cv2
     img=cv2.imread('D:\\appdata\\vscode\\python\\ph.jpg',cv2.IMREAD_UNCHANGED)
     cv2.namedWindow('img',cv2.WINDOW_AUTOSIZE)
     cv2.imshow('img',img)
     cv2.waitKey(0)
    
  2. 修改vscode的路径搜索方式。在launch.json中python的configuration下添加

             {
         "name": "Python: 当前文件",
         "type": "python",
         "request": "launch",
         "program": "${file}",
         "cwd": "${fileDirname}",//是我是我看看我
         "console": "integratedTerminal",
         "justMyCode": true, 
     },
    

    划重点!!!如果是用的 coderuner的话,需要在设置中搜索File Directory As Cwd,勾选确定

参考文章
https://blog.csdn.net/sumaliqinghua/article/details/90404173
https://zhuanlan.zhihu.com/p/159435958

你可能感兴趣的:(python,其他,opencv,python)