繁体   English   中英

用于 OCR 的清洁图像

[英]Clean image for OCR

我一直在尝试为 OCR 清理此图像,但结果好坏参半:

在此处输入图像描述

我取得的最好成绩:

在此处输入图像描述

def image_smoothening(img):
    ret1, th1 = cv2.threshold(img, 180, 255, cv2.THRESH_BINARY)
    ret2, th2 = cv2.threshold(th1, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    blur = cv2.GaussianBlur(th2, (1, 1), 0)
    ret3, th3 = cv2.threshold(
        blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    return th3
    
def remove_noise_and_smooth(img):
    filtered = cv2.adaptiveThreshold(img.astype(
        np.uint8), 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 45, 3)
    kernel = np.ones((1, 1), np.uint8)
    opening = cv2.morphologyEx(filtered, cv2.MORPH_OPEN, kernel)
    closing = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel)
    img = image_smoothening(img)
    or_image = cv2.bitwise_or(img, closing)
    return or_image

关于我缺少什么的任何线索?

我的 MATLAB 代码来解决它。 我知道你写的是 Python 所以你必须翻译。

%Read in
im = imread('DuQy7.png');
%Convert to grayscale
img = rgb2gray(im);
img = rescale(img);
%Binarize with threshold of 0.7/1.0
imbw = imbinarize(img,0.7/1);
%Flip blacks/whites
imbw = imcomplement(imbw);
%Label, L is labelled image, n is # of labels
[L,n] = bwlabeln(imbw);

count = zeros(n,1);
[y,x] = size(L);

%Get count for each label
L = uint8(L);
for j=1:y
    for i=1:x
        if L(j,i) ~= 0
            count(L(j,i)) = count(L(j,i)) + 1;
        end
    end
end

%Find label with most values in image
max = 0;
maxi = 1;
for index=1:n
    if max < count(index)
        max = count(index);
        maxi = index;
    end
end

%Replace large region and color other labels to white
for j=1:y
    for i=1:x
        if L(j,i) == maxi
            L(j,i) = 0;
        elseif L(j,i) ~= 0
            L(j,i) = 256;
        end
    end
end

%view and save
imshow(L)
imwrite(L,'outputTXT.bmp');

输出-TXT.png

您可能可以更好地调整阈值以更好地去除包含的背景区域。 您还可以查找非常小的标记区域并将其删除,因为它们可能被错误地包含在内。

背景的某些部分将无法摆脱,因为它们与实际符号无法区分。 例如,在符号 x2,y1 和 x2,y2 之间有一个黑色背景区域,其轮廓为白色,与符号的值相同。 因此很难解析出来。

您可以在 Python/OpenCV 中进行“除法归一化”以去除背景。 但这无助于轮廓字体问题。

输入:

在此处输入图像描述

import cv2
import numpy as np

# read the image
img = cv2.imread('img.png')

# convert to gray
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# apply morphology
kernel = cv2.getStructuringElement(cv2.MORPH_RECT , (3,3))
smooth = cv2.morphologyEx(gray, cv2.MORPH_DILATE, kernel)

# alternate blur in place of morphology
#smooth = cv2.GaussianBlur(gray, (15,15), 0)

# divide gray by morphology image
division = cv2.divide(gray, smooth, scale=255)

# threshold
result = cv2.threshold(division, 0, 255, cv2.THRESH_OTSU )[1] 

# save results
cv2.imwrite('img_thresh.png',result)

# show results
cv2.imshow('smooth', smooth)  
cv2.imshow('division', division)  
cv2.imshow('result', result)  
cv2.waitKey(0)
cv2.destroyAllWindows()

结果:

在此处输入图像描述

暂无
暂无

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

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