如何提高图像质量? [关闭]

2024-06-24

我正在制作一个读取身份证的 OCR。使用 YOLO 获取感兴趣的区域后,我将裁剪后的区域交给 Tesseract 来读取它。由于这些裁剪后的图像非常小且模糊,Tesseract 无法读取它们。当它can阅读它们,它会给出错误的预测。我认为通过提高裁剪图像的图像质量,这些问题都可以得到解决。

One of the cropped images:A blurry cropped ID card image, reading 8798-7195-7831.

我的问题是,我将如何改进这些图像?


@vasilisg 的回答。是一个非常好的解决方案。进一步改进的一种方法是使用形态学开放操作去除剩余的斑点。但是,这仅适用于小于图像中数字线宽的点。另一种选择是使用 openCV 连接组件模块删除少于 N 像素的“孤岛”。例如,您可以执行以下操作:

# External libraries used for
# Image IO
from PIL import Image

# Morphological filtering
from skimage.morphology import opening
from skimage.morphology import disk

# Data handling
import numpy as np

# Connected component filtering
import cv2

black = 0
white = 255
threshold = 160

# Open input image in grayscale mode and get its pixels.
img = Image.open("image.jpg").convert("LA")
pixels = np.array(img)[:,:,0]

# Remove pixels above threshold
pixels[pixels > threshold] = white
pixels[pixels < threshold] = black


# Morphological opening
blobSize = 1 # Select the maximum radius of the blobs you would like to remove
structureElement = disk(blobSize)  # you can define different shapes, here we take a disk shape
# We need to invert the image such that black is background and white foreground to perform the opening
pixels = np.invert(opening(np.invert(pixels), structureElement))


# Create and save new image.
newImg = Image.fromarray(pixels).convert('RGB')
newImg.save("newImage1.PNG")

# Find the connected components (black objects in your image)
# Because the function searches for white connected components on a black background, we need to invert the image
nb_components, output, stats, centroids = cv2.connectedComponentsWithStats(np.invert(pixels), connectivity=8)

# For every connected component in your image, you can obtain the number of pixels from the stats variable in the last
# column. We remove the first entry from sizes, because this is the entry of the background connected component
sizes = stats[1:,-1]
nb_components -= 1

# Define the minimum size (number of pixels) a component should consist of
minimum_size = 100

# Create a new image
newPixels = np.ones(pixels.shape)*255

# Iterate over all components in the image, only keep the components larger than minimum size
for i in range(1, nb_components):
    if sizes[i] > minimum_size:
        newPixels[output == i+1] = 0

# Create and save new image.
newImg = Image.fromarray(newPixels).convert('RGB')
newImg.save("newImage2.PNG")

在此示例中,我执行了开运算和连通分量方法,但是如果使用连通分量方法,通常可以省略开运算。

结果如下:

After thresholding and opening: enter image description here

After thresholding, opening and connected component filtering: Image after thresholding, opening and connected component filtering

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何提高图像质量? [关闭] 的相关文章

随机推荐