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

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

问题描述

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

img = cv2.imread('AB.jpg')
mask = np.zeros(img.shape[:2] , np.uint8) 

bgdModel = np.zeros((1,65), np.float64)
fgdModel = np.zeros((1,65), np.float64)

rect = (300 , 120 , 470 , 350)

#this modifies mask
cv2.grabCut(img,mask,rect,bgdModel, fgdModel , 5 , cv2.GC_INIT_WITH_RECT)

#If mask==2 or mask==1 , mask2 get 0, otherwise it gets 1 as 'uint8' type
mask2 = np.where((mask==2) | (mask==0),0,1).astype('uint8')

#adding additional dimension for rgb to the mask, by default it gets 1
#multiply with input image to get the segmented image
img_cut = img*mask2[: , : , np.newaxis]

plt.subplot(211),plt.imshow(img)
plt.title('Input Image') , plt.xticks([]),plt.yticks([])
plt.subplot(212),plt.imshow(img_cut)
plt.title('Grab cut'), plt.xticks([]),plt.yticks([])
plt.show()

在编译时出现此错误:

python img.py AB.jpg
Traceback (most recent call last):
File "img.py", line 6, in <module>
mask = np.zeros(img.shape[:2] , np.uint8) 
AttributeError: 'NoneType' object has no attribute 'shape'

推荐答案

回答,因为社区把它带回来了.添加我的两个对象(美分).

Answering, because the community brought it back. Adding my two objects (cents).

您看到该错误的唯一原因是您试图获取信息或对最初不存在的对象执行操作.要检查,请尝试打印对象.喜欢,添加 -

The only reason you see that error is because you are trying to get information or perform operations on an object that doesn't exist in the first place. To check, try printing the object. Like, adding -

print img      # such as this case
print contours # if you are working with contours and cant draw one
print frame    # if you are working with videos and it doesn't show

给你一个 None.这意味着你没有正确阅读它.您提供的图像名称不存在或路径错误.如果您发现此类错误,请快速执行以下操作-

gives you a None. That means you haven't read it properly. Either the image name you gave does not exist or the path to it is wrong. If you find such an error here's the quick things to do-

  1. 检查路径或将您的图像带到工作目录
  2. 检查您提供的名称是否正确(包括扩展名 - .jpg、.png 等)
  3. 将整个代码放在一个带有对象的 if 语句中,如果为真,则继续执行代码
  4. 如果在评论中提出建议,将添加更多.

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

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