在scikit imag中从文件系统加载自定义映像

2024-04-23 11:08:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python新手,我正在尝试完成官方页面上显示的教程。我的目标是,使用本地Otsu阈值方法分析我创建的图片。

带有示例图片的代码工作正常,但我想读取一个自定义图像,它与*.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())

我该怎么做?


Tags: fromimportimgthresholdaspltaxglobal
2条回答

使用io.imread函数

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

使用sklimage库加载图像。

如果未安装:pip install-U scikit映像

加载图像:

    from skimage import io
    img = io.imread('file_path')

相关问题 更多 >