如何去除包含表格的图像中的模糊?

3
我有一张模糊且含有噪声的图片。我尝试了以下示例中的图像去噪

enter image description here

使用非局部均值去噪算法从彩色图像中去除高斯噪声的代码:

import numpy as np
import cv2
from matplotlib import pyplot as plt

img = cv2.imread("data_5/1.png")
b,g,r = cv2.split(img)           # get b,g,r
rgb_img = cv2.merge([r,g,b])     # switch it to rgb

# Denoising

dst = cv2.fastNlMeansDenoisingColored(img,None,10,10,7,21) 
b,g,r = cv2.split(dst)           # get b,g,r
rgb_dst = cv2.merge([r,g,b])     # switch it to rgb


cv2.imshow('denoising black and white', rgb_dst)
cv2.waitKey(0)

上述代码的输出结果: enter image description here 上述代码去除了一些噪点。但是一些数字和表格线条变得模糊了。
有没有人可以建议一个更好的解决方案来消除上面图像的模糊和噪音?

@zindarod 的目标是识别每一行的文本。 - Dinesh Sonachalam
为什么不使用OCR,而不是去除噪声并最终得到另一张图像呢? - user2261062
@SembeiNorimaki 我尝试了OCR,但由于这张图片中的文本质量不太好,它给出了不同的输出。 - Dinesh Sonachalam
@zindarod,我已经去除了图像中的噪点并得出了答案。但我需要让文本更清晰地平滑处理,消除噪点。你能否给我提供一个平滑输出图像的解决方案? - Dinesh Sonachalam
2个回答

5
import numpy as np
import cv2
from PIL import Image
from tesserocr import PyTessBaseAPI, RIL

if __name__ == '__main__':

    image = cv2.imread('image.png',cv2.IMREAD_UNCHANGED)

    image = cv2.resize(image, (0,0), fx=0.5, fy=0.5)

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    ret,binary = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY_INV)

    binary = cv2.medianBlur(binary, 3)

    (rows,cols) = image.shape[:2]

    H = cv2.Sobel(binary, cv2.CV_8U, 1, 0, ksize = 5)
    V = cv2.Sobel(binary, cv2.CV_8U, 0, 1, ksize = 5)

    _,contours,_ = cv2.findContours(V, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

    for cnt in contours:
        (x,y,w,h) = cv2.boundingRect(cnt)
        if w < cols/3 and h < rows/3:
            cv2.drawContours(V, [cnt], -1, 0, -1)

    _,contours,_ = cv2.findContours(H, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

    for cnt in contours:
        (x,y,w,h) = cv2.boundingRect(cnt)
        if w < cols/3 and h < rows/3:
            cv2.drawContours(H, [cnt], -1, 0, -1)

    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
    V = cv2.morphologyEx(V, cv2.MORPH_DILATE, kernel, iterations = 3)
    H = cv2.morphologyEx(H, cv2.MORPH_DILATE, kernel, iterations = 3)

    binary[V == 255] = 0
    binary[H == 255] = 0

    binary = cv2.bitwise_not(binary)

    api = PyTessBaseAPI()
    api.SetImage(Image.fromarray(binary))
    text = api.GetUTF8Text()
    text = text.split()

    boxes = api.GetComponentImages(RIL.TEXTLINE, True)

    for i, (_, box, _, _) in enumerate(boxes):
        (x,y,w,h) = box['x'], box['y'], box['w'], box['h']
        cv2.rectangle(image, (x,y), (x+w,y+h), (0,0,255))
        cv2.putText(image, text[i], (x,y), cv2.FONT_HERSHEY_PLAIN, 1, (255,0,0))

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

binary

result


2

我尝试了应用高斯模糊并使用自适应阈值处理它,结果消除了图像中的噪点和模糊。

import cv2 as cv

#input

img = cv.imread('data_5/1.png',0)

#gaussian Blur

img = cv.GaussianBlur(img, (15,15),0)

#adaptive threshold

th3 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,\

cv.THRESH_BINARY,11,2)

cv2.imshow('Noise Filtered Image', th3)
cv2.waitKey(0)
cv.imwrite('data_5/result.png',th3)

上述代码的输出结果如下图所示: enter image description here 有人能帮我把这张图片变得更加平滑吗?我想要一个与下面表格类似的输出质量,去除表格线也可以。
我的目标是得到一张清晰的文字图片。
下图为期望得到的输出效果: enter image description here

“输出质量与下面的表格类似”。抱歉,最后一张表格的质量比起最初的那一个要差得多。 - user2261062
@SembeiNorimaki 问题在于我需要更好的输出图像文本质量。有没有办法改善输出图像中的文本质量? - Dinesh Sonachalam
1
如果这不能解答您的问题,请勿将其作为答案发布,而是编辑您的问题并添加相关的尝试信息。这样问题会跳到顶部,更多人会看到它。如果它已经有了答案,人们可能会跳过它,因为它已经“回答”了。 - api55

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接