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

Din*_*lam 2 python opencv image-processing

我有一个模糊的图像,包含一些噪音.我从下面的例子中尝试过Image Deoising.

在此输入图像描述

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

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)
Run Code Online (Sandbox Code Playgroud)

上面代码的输出: 在此输入图像描述

上面的代码消除了一些噪音.但是这里有些数字模糊不清,桌面线条模糊不清.

任何人都可以建议我一个更好的解决方案,从上面的图像中删除模糊和噪音?

zin*_*rod 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()
Run Code Online (Sandbox Code Playgroud)

二进制

结果