【发布时间】:2020-09-28 09:22:37
【问题描述】:
我正在做 OCR 以从 ID 卡中提取信息。但是,准确度相当低。
我的假设是去除背景会使 OCR 更准确。
我使用身份证扫描机(link)获取下面的灰度图。看来机器使用的是IR而不是图像处理。
有谁知道如何使用 Opencv 或工具(photoshop、gimp 等)获得相同的结果?
提前致谢。
【问题讨论】:
标签: python opencv image-processing ocr scikit-image
我正在做 OCR 以从 ID 卡中提取信息。但是,准确度相当低。
我的假设是去除背景会使 OCR 更准确。
我使用身份证扫描机(link)获取下面的灰度图。看来机器使用的是IR而不是图像处理。
有谁知道如何使用 Opencv 或工具(photoshop、gimp 等)获得相同的结果?
提前致谢。
【问题讨论】:
标签: python opencv image-processing ocr scikit-image
编辑:
由于光照条件不同,这里增加了对比度调整。
我认为解决您的问题的简单方法是:由于不需要的背景颜色是绿色和红色,而所需的字体颜色是黑色,只需按如下方式抑制红色和绿色:
import numpy as np
import matplotlib.pyplot as plt
from skimage.io import imread, imsave
from skimage.color import rgb2gray
from skimage.filters import threshold_otsu
from skimage import exposure
def adjustContrast(img):
p2, p98 = np.percentile(img, (2, 98))
img_rescale = exposure.rescale_intensity(img, in_range=(p2, p98))
return img_rescale
# Read the image
img = imread('ID_OCR.jpg')
# Contrast Adjustment for each channel
img[:,:,0] = adjustContrast(img[:,:,0]) # R
img[:,:,1] = adjustContrast(img[:,:,1]) # G
img[:,:,2] = adjustContrast(img[:,:,2]) # B
# # Supress unwanted colors
img[img[...,0] > 100] = 255 # R
img[img[...,1] > 100] = 255 # B
# Convert the image to graylevel
img = rgb2gray(img)
# Rescale into 0-255
img = 255*img.astype(np.uint8)
# Save the results
imsave('Result.png', img)
图像将如下所示:
结果不是最佳的,因为您的图像分辨率不高。
最后,有很多解决方案和改进,你也可以使用 Morphology 让它看起来更好,这只是解决问题的简单建议。
【讨论】:
这里还有两种方法:自适应阈值化和除法归一化。
输入:
import cv2
import numpy as np
# read image
img = cv2.imread("green_card.jpg")
# convert img to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# do adaptive threshold on gray image
thresh1 = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 51, 25)
# write results to disk
cv2.imwrite("green_card_thresh1.jpg", thresh1)
# apply morphology
kernel = cv2.getStructuringElement(cv2.MORPH_RECT , (11,11))
morph = cv2.morphologyEx(gray, cv2.MORPH_DILATE, kernel)
# divide gray by morphology image
division = cv2.divide(gray, morph, scale=255)
# threshold
thresh2 = cv2.threshold(division, 0, 255, cv2.THRESH_OTSU )[1]
# write results to disk
cv2.imwrite("green_card_thresh2.jpg", thresh2)
# display it
cv2.imshow("thresh1", thresh1)
cv2.imshow("thresh2", thresh2)
cv2.waitKey(0)
自适应阈值结果:
除法归一化结果:
【讨论】: