Python 属性错误:';非类型';对象没有属性';形状';
编译时出现以下错误:Python 属性错误:';非类型';对象没有属性';形状';,python,opencv,Python,Opencv,编译时出现以下错误: 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
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
回溯(最近一次呼叫最后一次):
文件“img.py”,第6行,在
掩码=np.0(img.shape[:2],np.uint8)
AttributeError:“非类型”对象没有属性“形状”
首先
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'
将无法按预期工作(从当前目录加载AB.jpg)。要加载的文件在提供的示例的第6行中硬编码:要按预期工作,它应该如下所示:
python img.py AB.jpg
返回错误是因为运行img.py的目录(当前工作目录)中不存在AB.jpg,并且在尝试读取丢失的文件之前没有验证该文件。正在应答,因为社区将其带回。添加我的两个对象(美分) 您看到该错误的唯一原因是,您试图获取信息或对一个根本不存在的对象执行操作。要进行检查,请尝试打印对象。比如,添加-
import sys
img = cv2.imread(sys.argv[1])
给你一个零。这意味着你没有正确阅读。您提供的图像名称不存在或路径错误。如果你发现这样一个错误,这里有一些快速的方法-
img没有。你确定你的图像加载正确吗?看看这个问题,了解如何检查图像加载是否正确:
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