2016-02-29 38 views
0

我是Python新手,我正在嘗試在官方頁面上顯示該教程。我的目標是,使用Local Otsu Threshold方法分析我創建的圖片。以scikit-image的形式從文件系統加載自定義圖像

帶示例圖片的代碼工作正常,但我想讀取存儲在與*.py-文件相同的目錄中的自定義圖像。

這是代碼:

from skimage import data 
from skimage.morphology import disk 
from skimage.filters import threshold_otsu, rank 
from skimage.util import img_as_ubyte 

import matplotlib 
import matplotlib.pyplot as plt 

matplotlib.rcParams['font.size'] = 9 
img = img_as_ubyte(data.page()) 

radius = 15 
selem = disk(radius) 

local_otsu = rank.otsu(img, selem) 
threshold_global_otsu = threshold_otsu(img) 
global_otsu = img >= threshold_global_otsu 

fig, ax = plt.subplots(2, 2, figsize=(8, 5), sharex=True, sharey=True, 
         subplot_kw={'adjustable': 'box-forced'}) 
ax0, ax1, ax2, ax3 = ax.ravel() 

fig.colorbar(ax0.imshow(img, cmap=plt.cm.gray), 
      ax=ax0, orientation='horizontal') 
ax0.set_title('Original') 
ax0.axis('off') 

fig.colorbar(ax1.imshow(local_otsu, cmap=plt.cm.gray), 
      ax=ax1, orientation='horizontal') 
ax1.set_title('Local Otsu (radius=%d)' % radius) 
ax1.axis('off') 

ax2.imshow(img >= local_otsu, cmap=plt.cm.gray) 
ax2.set_title('Original >= Local Otsu' % threshold_global_otsu) 
ax2.axis('off') 

ax3.imshow(global_otsu, cmap=plt.cm.gray) 
ax3.set_title('Global Otsu (threshold = %d)' % threshold_global_otsu) 
ax3.axis('off') 

plt.show() 

更具體地說我想要加載我的形象的example.jpg代替

img = img_as_ubyte(data.page()) 

我怎樣才能做到這一點?

回答

3

使用io.imread功能

img = io.imread('/path/to/example.jpg') 
+0

非常感謝您! :) – John