在图像上使用Skimage自适应阈值并获得输出 [英] Using Skimage adaptive thresholding on an image and getting the output

查看:782
本文介绍了在图像上使用Skimage自适应阈值并获得输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的图像上使用scikit-image的自适应阈值。我从这里测试了他们的示例代码

I am trying to use scikit-image's adaptive threshold on my image. I tested out their sample code from HERE

import matplotlib.pyplot as plt

from skimage import data
from skimage.filters import threshold_otsu, threshold_adaptive


image = data.page()

global_thresh = threshold_otsu(image)
binary_global = image > global_thresh

block_size = 35
binary_adaptive = threshold_adaptive(image, block_size, offset=10)

fig, axes = plt.subplots(nrows=3, figsize=(7, 8))
ax0, ax1, ax2 = axes
plt.gray()

ax0.imshow(image)
ax0.set_title('Image')

ax1.imshow(binary_global)
ax1.set_title('Global thresholding')

ax2.imshow(binary_adaptive)
ax2.set_title('Adaptive thresholding')

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

plt.show()

代码接收样本图像,对其进行阈值处理并使用plt显示它。但是,我试图检索阈值图像的numpy数组。当我尝试在变量 binary_global 上使用 cv2.imwrite 时,它不起作用。打印出 binary_global 时 - 它实际上是一个由False和True值组成的数组,而不是数字。我不确定plt如何使用它并生成图像。无论如何,我如何阈值图像并使用RGB值检索新的阈值图像的数组?

The code takes in a sample image, thresholds it and shows it using plt. However, I am trying to retrieve the numpy array of the thresholded image. When I tried using cv2.imwrite on the variable binary_global, it does not work. When printing out binary_global--it is actually an array consisting of False and True values rather than numbers. I am not sure how plt can use that and produce an image. Regardless, how can I threshold the image and retrieve the new thresholded image's array with the RGB values?

推荐答案

您首先需要转换scikit image to opencv能够使用 cv2.imwrite()

You first need convert the scikit image to opencv to be able to use cv2.imwrite().

添加以下更改 -

from skimage import img_as_ubyte
import matplotlib.pyplot as plt
from skimage import data
from skimage.filters import threshold_otsu, threshold_adaptive
import cv2


image = data.page()

global_thresh = threshold_otsu(image)
binary_global = image > global_thresh

block_size = 35
binary_adaptive = threshold_adaptive(image, block_size, offset=10)

fig, axes = plt.subplots(nrows=3, figsize=(7, 8))
ax0, ax1, ax2 = axes
plt.gray()

ax0.imshow(image)
ax0.set_title('Image')

ax1.imshow(binary_global)
ax1.set_title('Global thresholding')

ax2.imshow(binary_adaptive)
ax2.set_title('Adaptive thresholding')

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

plt.show()
img = img_as_ubyte(binary_global)
cv2.imshow("image", img)
cv2.waitKey(0)

然后您可以使用 img 进行写作等。

You can then use the img for writing etc.

这篇关于在图像上使用Skimage自适应阈值并获得输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆