AttributeError:'NoneType'对象没有属性'ravel' [英] AttributeError: 'NoneType' object has no attribute 'ravel'

查看:185
本文介绍了AttributeError:'NoneType'对象没有属性'ravel'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我此代码有什么问题吗?我不断收到NoneType错误.我正在尝试创建直方图.

Can someone please tell me what is wrong with this code? I keep on getting a NoneType error. I am trying to create a histogram.

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

img = cv2.imread('C:\Pictures\naturalScene.bmp',0)
plt.hist(img.ravel(),256,[0,256]);
plt.show()

推荐答案

来自文档:

该函数imread从指定的文件加载图像并返回它.如果无法读取图像(由于缺少文件,权限不正确,格式不受支持或格式无效),该函数将返回一个空矩阵(Mat :: data == NULL).

The function imread loads an image from the specified file and returns it. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix ( Mat::data==NULL ).

您的路径不正确,您需要转义\n:

Your path is incorrect you need to escape the \n:

cv2.imread('C:\\Pictures\\naturalScene.bmp',0)

或使用/:

cv2.imread('C:/Pictures/naturalScene.bmp',0)

或者就像@Martijn Pieters评论的那样,使用原始字符串文字:

Or as @Martijn Pieters commented use a raw string literal:

cv2.imread(r'C:\Pictures\naturalScene.bmp',0)

这篇关于AttributeError:'NoneType'对象没有属性'ravel'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆