import numpy as np
import scipy.ndimage as ndi
import cv2
from skimage import measure
from skimage import color
import matplotlib.pyplot as plt
image = cv2.imread('F:/PythonFile/Q1.png')
labels = measure.label(image[:,:,0], connectivity=1) #4连通区域标记
dst = color.label2rgb(labels) #根据不同的标记显示不同的颜色
plt.imshow(dst)
print('regions number:', labels.max()+1)
运行之后提示错误:AttributeError: ‘NoneType’ object has no attribute ‘shape’
【原因】: 1.图片不存在(路径不存在, 路径包含中文无法识别) 2.读取的图片内容和默认读取时参数匹配不匹配。(默认读取的是3通道的彩色图)例如读取到的图片是灰度图,就会返回None。
【解决方法】:将’F:/PythonFile/Q1.png’改为’F:\PythonFile\question1.jpg’再运行就没有报错了。