【问题标题】:I am getting error when printing the pixel value of the read image in python-opencv, TypeError: 'NoneType' object has no attribute '__getitem__'在 python-opencv 中打印读取图像的像素值时出现错误,TypeError: 'NoneType' object has no attribute '__getitem__'
【发布时间】:2016-12-22 20:21:21
【问题描述】:

我刚刚安装了 opencv,并通过键入以下内容检查它是否正确安装:

pkg-config --modversion opencv

在命令终端。

我开始使用 pything-opencv 读取和显示图像,但是当我运行我的代码时,它会抛出一个错误:

TypeError: 'NoneType' object has no attribute '__getitem__'

我的代码非常少,但没有得到哪里有错误。

我正在运行的代码是:

import cv2
import numpy as np
from matplotlib import pyplot as plt
import argparse

img = cv2.imread('messi5.jpg')
print(img)
print("end of file")

它给出了输出:

None
end of file

当我这样写两行时:

px = img[100,100]
print(px)

然后它会抛出错误: 回溯(最近一次通话最后): 文件“testing_opencv_python.py”,第 23 行,在 像素 = img[100,100] TypeError:“NoneType”对象没有属性“getitem

相同的代码可以在其他系统上完美运行。

如果您能指出错误,我将不胜感激。

我基本上想安装 caffe,但是当我这样做时,我得到了错误,并且似乎它依赖于 opencv,那就是我安装了 opencv。

感谢和问候。

【问题讨论】:

    标签: python opencv caffe nonetype


    【解决方案1】:

    返回的图像是None(打印时可以看到),这导致了另一个错误。

    这很可能是由于指定了错误的图像路径('messi5.jpg')。在文档here 中,它指出:

    警告即使图片路径错误,也不会抛出任何错误,但是 print img 会给你 None

    要么提供“messi5.jpg”的正确路径,要么将图像复制到当前目录(执行 python 脚本的位置)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-25
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-15
      相关资源
      最近更新 更多