【问题标题】:PythonCV2 NoneType ObjectPython CV2 NoneType 对象
【发布时间】:2020-01-05 11:23:13
【问题描述】:
#!/usr/bin/env python

'''
 Usage:
./ssearch.py input_image (f|q)
 f=fast, q=quality
 Use "l" to display less rects, 'm' to display more rects, "q" to quit.
 '''

import sys
import cv2

if __name__ == '__main__':
# If image path and f/q is not passed as command
# line arguments, quit and display help message
if len(sys.argv) < 3:
    print(__doc__)
    sys.exit(1)

# speed-up using multithreads
cv2.setUseOptimized(True);
cv2.setNumThreads(4);
# read image
im = cv2.imread(sys.argv[1])
# resize image
newHeight = 200
newWidth = int(im.shape[1]*200/im.shape[0])
im = cv2.resize(im, (newWidth, newHeight))    

这里出错了

AttributeError   Traceback (most recent call last)
<ipython-input-7-b88f466ecb3b> in <module>
     25     # resize image
     26     newHeight = 200
     --->  27     newWidth = int(im.shape[1]*200/im.shape[0])
     28     im = cv2.resize(im, (newWidth, newHeight))
     29 

AttributeError: 'NoneType' object has no attribute 'shape'

并解释 cv2.imread(sys,argv[1]) 行

(2)第二个问题是我如何得到非类型错误,因为我没有 从我的命令行传递了我的图像路径 (3) cmd line没有提示,因为程序没有执行错误

很抱歉,我无法放置完整的代码

【问题讨论】:

  • "如果无法读取图像(由于缺少文件、权限不正确、格式不支持或无效),该函数将返回一个空矩阵( Mat::data==NULL )。" docs.opencv.org/master/d4/da8/…
  • 你检查图像的路径是否有效?在阅读图像之前,请尝试os.path.exists(sys.argv[1])

标签: python cv2 nonetype


【解决方案1】:

正确的做法是:

# read image
im = cv2.imread(sys.argv[1])
if im == None :
    sys.exit('Invalid or missing image: ' + sys.argv[1])

在你尝试对你的图片做任何事情之前im

【讨论】:

  • @sriharsha 好吧,显然你像./script.py -f 一样运行它,脚本认为-f 是它应该打开的文件名。
猜你喜欢
  • 2020-09-19
  • 2018-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-31
  • 2013-12-01
  • 2013-04-19
  • 2017-08-29
相关资源
最近更新 更多