【问题标题】:skimage treshold_local does not work with pictures loaded using io.imreadskimage treshold_local 不适用于使用 io.imread 加载的图片
【发布时间】:2019-07-18 22:26:15
【问题描述】:

我正在试用 Scikit Image 网站上提供的示例 Python 脚本之一。此脚本演示了本地级别的 Otsu 分割。该脚本适用于使用

加载的图片

data.page()

但不使用

io.imread

。有什么建议吗?

https://scikit-image.org/docs/dev/auto_examples/applications/plot_thresholding.html#sphx-glr-auto-examples-applications-plot-thresholding-py

图片文件

实际输出 - 局部阈值窗口为空

如您所见,全局阈值处理有效。但局部阈值处理未能产生任何结果。

奇怪的是,如果我使用 data.page() 那么一切正常。

脚本

from skimage import io
from skimage.color import rgb2gray
import matplotlib.pyplot as plt
from skimage.filters import threshold_otsu,threshold_local
import matplotlib
from skimage import data
from skimage.util import img_as_ubyte

filename="C:\\Lenna.png"
mypic= img_as_ubyte (io.imread(filename))


#image = data.page()  #This works - why not io.imread ?
imagefromfile=io.imread(filename)
image = rgb2gray(imagefromfile)

global_thresh = threshold_otsu(image)
binary_global = image > global_thresh

block_size = 35
local_thresh = threshold_local(image, block_size, offset=10)
binary_local = image > local_thresh

fig, axes = plt.subplots(nrows=3, figsize=(7, 8))
ax = axes.ravel()
plt.gray()

ax[0].imshow(image)
ax[0].set_title('Original')

ax[1].imshow(binary_global)
ax[1].set_title('Global thresholding')

ax[2].imshow(binary_local)
ax[2].set_title('Local thresholding')

for a in ax:
    a.axis('off')

plt.show()

【问题讨论】:

    标签: scikit-image


    【解决方案1】:

    如果您加载 lenna.png 并打印它的形状,您将看到它是 4 通道 RGBA 图像而不是 3 通道 RGB 图像。

    print mypic.shape
    (512, 512, 4)
    

    我不确定您的代码的哪些部分适用于哪个图像,所以我不确定下一步该去哪里,但我想您只想获取 RGB 部分并丢弃 alpha:

    RGB = mypic[...,:3]
    

    【讨论】:

    • 奇怪的是,shape 给了我[512 512 3]dtype 给了我uint8。使用 rg2gray 转换为灰度后,shape 更改为 [512 512]。灰度中的所有值都在 0 和 1 之间。我重新缩放到 255,然后我能够使区域阈值起作用。
    • 函数rgb2gray 与使用imread 加载的Lenna 图像相比,从data.page() 加载的图像似乎表现出不同的行为。 Lenna 图像是标准化的,而前者不是。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-21
    • 1970-01-01
    • 2017-11-17
    • 2017-11-07
    • 2018-01-28
    • 1970-01-01
    • 2014-06-13
    相关资源
    最近更新 更多