本教程中代码的开头是:
%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from skimage import data
from skimage.filters import threshold_otsu
from skimage.segmentation import clear_border
from skimage.measure import label, regionprops
from skimage.morphology import closing, square
from skimage.color import label2rgb
image = data.coins()[50:-50, 50:-50]
# apply threshold
thresh = threshold_otsu(image)
bw = closing(image > thresh, square(3))
,我想将其应用于 .jpg 的图像。 但这不起作用,我在IPython上收到一条很长的消息,结尾是:
IndexError:元组索引超出范围
我比较了
print(data.coins()[50:-50, 50:-50].shape)
(203L,284L)
和
import mahotas as mh
image=mh.imread('image.jpg')
print(image.shape)
(520L,704L,3L)
我是否认为差异来自尺寸差异?为了解决这个问题我该怎么办?
此外,即使现在我仍然读过http://scikit-image.org/docs/dev/api/skimage.morphology.html#skimage.morphology.binary_closing,对于我来说, square(3)在结束中的含义仍不清楚。你能解释一下我吗?
答案 0 :(得分:2)
加载后尝试转换为灰度:
from skimage.color import rgb2gray
image = rgb2gray(data.coins()[50:-50, 50:-50])
以下内容:
square(3)
仅表示一个1s的3x3方阵:
array([[1, 1, 1],
[1, 1, 1],
[1, 1, 1]], dtype=uint8)