在图像上使用猛击自适应阈值并获取输出



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

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显示。但是,我试图检索阈值图像的数组。当我尝试在变量binary_global上使用cv2.imwrite时,它不起作用。打印binary_global时 - 实际上是一个由假和真实值组成的数组,而不是数字。我不确定PLT如何使用并产生图像。无论如何,如何使用RGB值阈值并检索新的阈值图像的数组?

您首先需要将scikit映像转换为openCV,以便能够使用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进行写作等。

最新更新