skimage中的图片文字提取
Image text extraction in skimage
我有一张图片,我想对其进行过滤以将文本从背景中分离出来:
并在应用以下代码后:
from skimage import filters
from skimage.filters import threshold_otsu
from skimage import io as skimage_io # So as not to clash with builtin io
dir = r"image_path/a.jpg"
img = skimage_io.imread(dir, as_gray=True, plugin='imageio')
blurred = filters.gaussian(img, sigma=2.0)
sobel = filters.sobel_h(blurred)
blurred += sobel
blurred += sobel
thresh = threshold_otsu(blurred)
# skimage_io.imshow(blurred)
print(thresh)
binary = img < thresh-0.1
skimage_io.imshow(binary)
图片变成了
有什么方法可以让结果更好吗??
是的,你可以获得更好的结果:
您确实考虑了噪音,但您应用的高斯模糊太强了;它开始使您感兴趣的功能(字母)变钝。
正如@Ziri 在评论中指出的那样,您没有考虑图像中存在的不均匀曝光。有很多方法可以做到这一点;我将使用滚球过滤器来平滑背景并将其与全局阈值方法相结合。请注意,这是目前(2020 年 8 月)skimage 中的一个 PR,但希望很快会合并。
import numpy as np
import matplotlib.pyplot as plt
from skimage import util
from skimage import filters
from skimage import io
from skimage import exposure
# PR 4851; will hopefully be in the library soon(TM)
from skimage.morphology import rolling_ellipsoid
img = io.imread("test.jpg", as_gray=True)
img_inv = util.invert(util.img_as_float(img))
# blurr the image slightly to remove noise
blurred = filters.gaussian(img_inv, sigma=1.0)
# remove background
background = rolling_ellipsoid(blurred, kernel_size=(50, 50), intensity_vertex=0.1)
normalized = blurred - background
# re-normalize intensity
normalized = exposure.rescale_intensity(normalized)
# binarize
binary = normalized > 0.38
binary = util.invert(binary)
plt.imshow(binary, cmap="gray")
plt.gca().axis("off")
plt.show()
旁注:将未经审查的护照照片上传到任何人都可以自由访问的 SO 可能并不明智。
我有一张图片,我想对其进行过滤以将文本从背景中分离出来:
并在应用以下代码后:
from skimage import filters
from skimage.filters import threshold_otsu
from skimage import io as skimage_io # So as not to clash with builtin io
dir = r"image_path/a.jpg"
img = skimage_io.imread(dir, as_gray=True, plugin='imageio')
blurred = filters.gaussian(img, sigma=2.0)
sobel = filters.sobel_h(blurred)
blurred += sobel
blurred += sobel
thresh = threshold_otsu(blurred)
# skimage_io.imshow(blurred)
print(thresh)
binary = img < thresh-0.1
skimage_io.imshow(binary)
图片变成了
有什么方法可以让结果更好吗??
是的,你可以获得更好的结果:
您确实考虑了噪音,但您应用的高斯模糊太强了;它开始使您感兴趣的功能(字母)变钝。
正如@Ziri 在评论中指出的那样,您没有考虑图像中存在的不均匀曝光。有很多方法可以做到这一点;我将使用滚球过滤器来平滑背景并将其与全局阈值方法相结合。请注意,这是目前(2020 年 8 月)skimage 中的一个 PR,但希望很快会合并。
import numpy as np
import matplotlib.pyplot as plt
from skimage import util
from skimage import filters
from skimage import io
from skimage import exposure
# PR 4851; will hopefully be in the library soon(TM)
from skimage.morphology import rolling_ellipsoid
img = io.imread("test.jpg", as_gray=True)
img_inv = util.invert(util.img_as_float(img))
# blurr the image slightly to remove noise
blurred = filters.gaussian(img_inv, sigma=1.0)
# remove background
background = rolling_ellipsoid(blurred, kernel_size=(50, 50), intensity_vertex=0.1)
normalized = blurred - background
# re-normalize intensity
normalized = exposure.rescale_intensity(normalized)
# binarize
binary = normalized > 0.38
binary = util.invert(binary)
plt.imshow(binary, cmap="gray")
plt.gca().axis("off")
plt.show()
旁注:将未经审查的护照照片上传到任何人都可以自由访问的 SO 可能并不明智。