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

时间:2018-06-28 08:19:41

标签: python opencv image-processing

我的图像模糊并且包含一些噪点。我已经从以下示例尝试过Image Denoising

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

上面的代码消除了一些噪音。但是这里有些数字是模糊的,表行是模糊的。

有人能建议我一个更好的解决方案,以消除上述图像中的模糊和噪点吗?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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