简体   繁体   English

OCR 的背景图像清理

[英]Background image cleaning for OCR

Through tesseract-OCR I am trying to extract text from the following images with a red background.通过 tesseract-OCR 我试图从以下红色背景的图像中提取文本。

在此处输入图像描述

I have problems extracting the text in boxes B and D because there are vertical lines.由于存在垂直线,我在提取框 B 和 D 中的文本时遇到问题。 How can I clean the background like this:我怎样才能像这样清理背景:

input:输入:

在此处输入图像描述

output: output:

在此处输入图像描述

some idea?一些想法? The image without boxes:没有框的图像: 在此处输入图像描述

Here are two methods to clean the image using Python OpenCV以下是使用 Python OpenCV 清理图像的两种方法

Method #1: Numpy thresholding方法 #1:Numpy 阈值

Since the vertical lines, horizontal lines, and the background are in red we can take advantage of this and use Numpy thresholding to change all red pixels above a threshold to white.由于垂直线、水平线和背景都是红色的,我们可以利用这一点并使用 Numpy 阈值将高于阈值的所有红色像素更改为白色。

在此处输入图像描述

import cv2
import numpy as np

image = cv2.imread('1.jpg')

image[np.where((image > [0,0,105]).all(axis=2))] = [255,255,255]

cv2.imshow('image', image)
cv2.waitKey()

Method #2: Traditional image processing方法#2:传统图像处理

For a more general approach if the lines were not red we can use simple image processing techniques to clean the image.对于更通用的方法,如果线条不是红色的,我们可以使用简单的图像处理技术来清理图像。 To remove the vertical and horizontal lines we can construct special kernels to isolate the lines and remove them using masking and bitwise operations.为了去除垂直和水平线,我们可以构造特殊的内核来隔离这些线,并使用掩码和按位操作来去除它们。 Once the lines are removed, we can use thresholding, morphological operations, and contour filtering to remove the red background.一旦线条被移除,我们可以使用阈值、形态学操作和轮廓过滤来移除红色背景。 Here's a visualization of the process这是该过程的可视化


First we construct vertical and horizontal kernels then cv2.morphologyEx() to detect the lines.首先我们构造垂直和水平内核然后cv2.morphologyEx()来检测线条。 From here we have individual masks of the horizontal and vertical lines then bitwise-or the two masks to obtain a mask with all lines to remove.从这里开始,我们有水平和垂直线的单独掩码,然后按位 - 或两个掩码以获得所有要删除的行的掩码。 Next we bitwise-or with the original image to remove all lines接下来我们用原始图像按位或删除所有行

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

Now that the lines are removed, we can work on removing the red background.现在线条已被移除,我们可以着手移除红色背景。 We threshold to obtain a binary image and perform morphological operations to smooth the text我们阈值以获得二值图像并执行形态学操作以平滑文本

在此处输入图像描述

There are still little dots so to remove them, we find contours and filter using a minimum threshold area to remove the small noise仍然有小点所以要去除它们,我们找到轮廓并使用最小阈值区域进行过滤以去除小噪声

在此处输入图像描述

Finally we invert the image to get our result最后我们反转图像得到我们的结果

在此处输入图像描述

import cv2

image = cv2.imread('1.jpg')

# Remove vertical and horizontal lines
kernel_vertical = cv2.getStructuringElement(cv2.MORPH_RECT, (1,50))
temp1 = 255 - cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel_vertical)
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (50,1))
temp2 = 255 - cv2.morphologyEx(image, cv2.MORPH_CLOSE, horizontal_kernel)
temp3 = cv2.add(temp1, temp2)
removed = cv2.add(temp3, image)

# Threshold and perform morphological operations
gray = cv2.cvtColor(removed, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 180, 255, cv2.THRESH_BINARY_INV)[1]
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2,2))
close = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=1)

# Filter using contour area and remove small noise
cnts = cv2.findContours(close, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    area = cv2.contourArea(c)
    if area < 10:
        cv2.drawContours(close, [c], -1, (0,0,0), -1)

final = 255 - close 
cv2.imshow('removed', removed)
cv2.imshow('thresh', thresh)
cv2.imshow('close', close)
cv2.imshow('final', final)
cv2.waitKey()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM