【问题标题】:How do you remove the unnecessary blackness from binary images (mainly caused by dark areas/noise)如何从二值图像中去除不必要的黑度(主要由暗区/噪点引起)
【发布时间】:2020-08-27 08:58:57
【问题描述】:

我正在处理教科书页面的图像,例如问题和手写笔记,并希望将二进制图像用于几个不同的任务,主要是 OCR。但问题是,如果图像有一点阴影或亮度级别不连续,就会给我的文字留下很多黑色区域。

我在我的图片上使用了from skimage.filters import try_all_threshold,发现有些图片适用于某些类型的图片,有些则不行。我不能使用本地阈值,我必须根据不同的图像更改参数,因为我想自动化 OCR 的过程。

img_path = DIR+str(11)+'.png'
sk_image = imread(img_path,as_gray=True)

fig,ax = try_all_threshold(sk_image,figsize=(20,15))
plt.savefig('threshold.jpeg',dpi=350)

为什么图像中会出现这个黑色区域,我该如何去除??

像Bilateral 或Gauss 这样的去噪滤波器可以吗?如果没有,请建议其他一些技术?

【问题讨论】:

  • 请始终单独发布您的输入图像,以便其他人可以使用它进行测试。我们不想从所有其他图像中裁剪您的输入。建议在阈值化之前使用自适应阈值化或除法归一化。
  • 在这里查看我的答案:stackoverflow.com/questions/22122309/…

标签: python opencv image-processing python-imaging-library scikit-image


【解决方案1】:

这是在 Python/OpenCV 中使用除法标准化的一种方法。

  • 读取输入
  • 转换为灰色
  • 高斯模糊平滑
  • 灰度图像除以平滑图像
  • 应用非锐化蒙版锐化
  • 应用大津阈值
  • 保存结果

输入:

import cv2
import numpy as np
import skimage.filters as filters

# read the image
img = cv2.imread('math.png')

# convert to gray
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# blur
smooth = cv2.GaussianBlur(gray, (95,95), 0)

# divide gray by morphology image
division = cv2.divide(gray, smooth, scale=255)

# sharpen using unsharp masking
sharp = filters.unsharp_mask(division, radius=1.5, amount=1.5, multichannel=False, preserve_range=False)
sharp = (255*sharp).clip(0,255).astype(np.uint8)

# threshold
thresh = cv2.threshold(sharp, 0, 255, cv2.THRESH_OTSU )[1] 

# save results
cv2.imwrite('math_division.jpg',division)
cv2.imwrite('math_division_sharp.jpg',sharp)
cv2.imwrite('math_division_thresh.jpg',division)

# show results
cv2.imshow('smooth', smooth)  
cv2.imshow('division', division)  
cv2.imshow('sharp', sharp)  
cv2.imshow('thresh', thresh)  
cv2.waitKey(0)
cv2.destroyAllWindows()

分区图:

锐化的图像:

阈值图像:

【讨论】:

  • 但这需要大量手动调整。如果我想为数千张图像运行 OCr,它不适合所有用例吗?不?像高斯滤波器的参数等。
  • 是的,它可以工作,因为高斯值很大。尝试各种图像并查看。
  • 好的。将尝试这样做。谢谢。还有其他建议吗?我基本上是为 OCR 做这个的。
猜你喜欢
  • 2016-02-28
  • 1970-01-01
  • 1970-01-01
  • 2021-02-28
  • 1970-01-01
  • 1970-01-01
  • 2013-08-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多