这是一种可能的解决方案。这是在 Python 中,但对于 Java 端口来说应该足够清楚。我们将应用一种称为增益除法的方法。这个想法是您尝试构建背景模型,然后通过该模型对每个输入像素进行加权。在图像的大部分时间里,输出增益应该是相对恒定的。这将消除大部分背景颜色变化。我们可以使用morphological
链来清理一下结果,让我们看一下代码:
# imports:
import cv2
import numpy as np
# OCR imports:
from PIL import Image
import pyocr
import pyocr.builders
# image path
path = "D://opencvImages//"
fileName = "c552h.png"
# Reading an image in default mode:
inputImage = cv2.imread(path + fileName)
# Get local maximum:
kernelSize = 5
maxKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (kernelSize, kernelSize))
localMax = cv2.morphologyEx(inputImage, cv2.MORPH_CLOSE, maxKernel, None, None, 1, cv2.BORDER_REFLECT101)
# Perform gain division
gainDivision = np.where(localMax == 0, 0, (inputImage/localMax))
# Clip the values to [0,255]
gainDivision = np.clip((255 * gainDivision), 0, 255)
# Convert the mat type from float to uint8:
gainDivision = gainDivision.astype("uint8")
第一步是应用增益除法,您需要的操作很简单:closing
具有大矩形structuring element
和一些数据类型转换的形态学,小心后者。这是应用该方法后您应该看到的图像:
很酷,背景几乎没有了。让我们使用 Otsu 的阈值化获得二值图像:
# Convert RGB to grayscale:
grayscaleImage = cv2.cvtColor(gainDivision, cv2.COLOR_BGR2GRAY)
# Get binary image via Otsu:
_, binaryImage = cv2.threshold(grayscaleImage, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
这是二进制图像:
我们有一个很好的文本边缘图像。如果我们以白色为背景,我们可以获得黑色背景和白色文本Flood-Fill
。但是,我们应该小心字符,因为如果字符损坏,Flood-Fill
操作会将其删除。让我们首先通过应用形态学来确保我们的角色是封闭的closing
:
# Set kernel (structuring element) size:
kernelSize = 3
# Set morph operation iterations:
opIterations = 1
# Get the structuring element:
morphKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (kernelSize, kernelSize))
# Perform closing:
binaryImage = cv2.morphologyEx( binaryImage, cv2.MORPH_CLOSE, morphKernel, None, None, opIterations, cv2.BORDER_REFLECT101 )
这是生成的图像:
如您所见,边缘更坚固,最重要的是,它们是封闭的。现在,我们可以Flood-Fill
将背景设为白色。这里,Flood-Fill
种子点位于图像原点 ( x = 0
, y = 0
):
# Flood fill (white + black):
cv2.floodFill(binaryImage, mask=None, seedPoint=(int(0), int(0)), newVal=(255))
我们得到这个图像:
我们就快到了。如您所见,某些字符(例如,“a”、“d”和“o”)内部的孔没有被填充 - 这会对OCR
. 让我们尝试填充它们。我们可以利用这些洞都是父轮廓的子元素这一事实。我们可以隔离子轮廓,然后再次应用 aFlood-Fill
来填充它们。但首先,不要忘记反转图像:
# Invert image so target blobs are colored in white:
binaryImage = 255 - binaryImage
# Find the blobs on the binary image:
contours, hierarchy = cv2.findContours(binaryImage, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Process the contours:
for i, c in enumerate(contours):
# Get contour hierarchy:
currentHierarchy = hierarchy[0][i][3]
# Look only for children contours (the holes):
if currentHierarchy != -1:
# Get the contour bounding rectangle:
boundRect = cv2.boundingRect(c)
# Get the dimensions of the bounding rect:
rectX = boundRect[0]
rectY = boundRect[1]
rectWidth = boundRect[2]
rectHeight = boundRect[3]
# Get the center of the contour the will act as
# seed point to the Flood-Filling:
fx = rectX + 0.5 * rectWidth
fy = rectY + 0.5 * rectHeight
# Fill the hole:
cv2.floodFill(binaryImage, mask=None, seedPoint=(int(fx), int(fy)), newVal=(0))
# Write result to disk:
cv2.imwrite("text.png", binaryImage, [cv2.IMWRITE_PNG_COMPRESSION, 0])
这是生成的掩码:
酷,让我们应用OCR
. 我正在使用pyocr
:
txt = tool.image_to_string(
Image.open("text.png"),
lang=lang,
builder=pyocr.builders.TextBuilder()
)
print(txt)
输出:
Landorus