问题描述
我正在研究教科书页面的图像(例如问题和手写笔记),并且想要用于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
这样的去噪滤波器会起作用吗?如果没有,请提出其他建议?
解决方法
这是在Python / OpenCV中使用除法归一化的一种方法。
- 阅读输入内容
- 转换为灰色
- 具有高斯模糊的平滑
- 通过平滑图像划分灰度图像
- 应用锐化蒙版以锐化
- 应用Otsu阈值
- 保存结果
输入:
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,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()
部门图片:
图片锐化:
阈值图像: