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

核能气质少年 提交于 2019-12-10 23:58:45

问题


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()

回答1:


From the docs:

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 ).

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

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

Or use /:

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

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

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


来源:https://stackoverflow.com/questions/27384489/attributeerror-nonetype-object-has-no-attribute-ravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!