进行图像分割时如何解决“元组索引超出范围”?

时间:2019-01-09 14:04:36

标签: python image-processing tuples scikit-image

我正在学习该教程 http://scikit-image.org/docs/dev/auto_examples/segmentation/plot_label.html#sphx-glr-auto-examples-segmentation-plot-label-py

本教程中代码的开头是:

%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)结束中的含义仍不清楚。你能解释一下我吗?

1 个答案:

答案 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)