如何预处理低对比度图像以提高OCR质量并避免信息丢失?
我正在尝试对一张对比度很低的图片进行文字识别。
原始图片:
我现在使用的方法是:在处理之前先用这些滤镜:
img_yuv = cv2.cvtColor(img, cv2.COLOR_BGR2YUV)
img_yuv[:,:,0] = cv2.equalizeHist(img_yuv[:,:,0])
img_output = cv2.cvtColor(img_yuv, cv2.COLOR_YUV2BGR)
img = img.filter(ImageFilter.GaussianBlur)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh, im_bw = cv2.threshold(img, 210, 230, cv2.THRESH_BINARY)
data = Image.fromarray(im_bw)
经过处理后,我得到了这样的结果:
我该如何改进我的方法呢?
1 个回答
2
你的输入图像有很多像盐和胡椒一样的噪声(查看这里),所以你应该使用中值模糊,而不是高斯模糊。此外,图像的光照水平不均匀,因此你应该使用自适应阈值处理,以减少光照变化的影响。而且,因为图像噪声很严重,当你应用直方图均衡化时,也会放大噪声。
我把你的图像分成了三条横向的部分,然后用 Otsu Thresholding
处理它们,最后再合并起来。让我给你展示一下高斯模糊、中值模糊和经过处理的图像的直方图均衡化效果。
正如你所看到的,中值模糊更适合你的图像,因为整个图像都有盐和胡椒一样的噪声。
现在我们来看一下在噪声很大的图像上进行均衡化的效果。
这是经过YUV均衡化的图像:
所以你可以得到最佳效果,使用原始图像不进行均衡化,并且使用中值模糊。你还可以使用形态学操作来进一步改善图像。
这是我使用的代码:
import cv2
import numpy
def thresholder(img,fil):
h,w,c = img.shape
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
if fil == 'median':
blurred = cv2.medianBlur(gray,11)
elif fil == 'gauss':
blurred = cv2.GaussianBlur(gray,(11,11),0)
#Divide the image into 3 pieces and filter them seperately
p1,p2 = int(w/3),int(2*w/3)
_,thresh1 = cv2.threshold(blurred[0:h,0:p1],0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
_,thresh2 = cv2.threshold(blurred[0:h,p1:p2],0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
_,thresh3 = cv2.threshold(blurred[0:h,p2:w],0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
#Merge the seperately filtered images
thresh = cv2.hconcat([thresh1,thresh2,thresh3])
return thresh
img = cv2.imread('img/plate.png')
h,w,c = img.shape
img_yuv = cv2.cvtColor(img, cv2.COLOR_BGR2YUV)
img_yuv[:,:,0] = cv2.equalizeHist(img_yuv[:,:,0])
img_equalized = cv2.cvtColor(img_yuv, cv2.COLOR_YUV2BGR)
cv2.imshow('Original Image',img)
cv2.imshow('Equalized Image',img_equalized)
cv2.imshow('Original Median Blurred',thresholder(img,fil='median'))
cv2.imshow('Original Gaussin Blurred',thresholder(img,fil='gauss'))
cv2.imshow('Equalized Median Blurred',thresholder(img_equalized,fil='median'))
cv2.imshow('Equalized Gaussin Blurred',thresholder(img_equalized,fil='gauss'))
cv2.waitKey(0)