gpt4 book ai didi

python - 如何提高图像质量?

转载 作者:行者123 更新时间:2023-12-01 07:23:33 26 4
gpt4 key购买 nike

关闭。这个问题需要更多 focused .它目前不接受答案。












想改进这个问题?更新问题,使其仅关注一个问题 editing this post .

3年前关闭。




Improve this question




我正在制作一个读取身份证的 OCR。通过使用 YOLO 获得感兴趣的区域后,我将该裁剪区域提供给 Tesseract 以读取它。由于这些裁剪后的图像非常小且模糊,Tesseract 无法读取它们。当它可以阅读它们时,它会给出错误的预测。我认为通过提高裁剪图像的图像质量,可以解决这些问题。
裁剪图像之一:A blurry cropped ID card image, reading 8798-7195-7831.
我的问题是,我将如何改进这些图像?

最佳答案

@vasilisg 的答案。是一个非常好的解决方案。进一步改进这一点的一种方法是使用形态学打开操作去除剩余的 Blob 。但是,这仅适用于小于图像中数字线厚的点。另一种选择是使用 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")

在这个例子中,我已经执行了打开和连接组件方法,但是如果您使用连接组件方法,您通常可以省略打开操作。

结果如下所示:

阈值化和打开后:
enter image description here

阈值化、打开和连通分量过滤后:
Image after thresholding, opening and connected component filtering

关于python - 如何提高图像质量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52004133/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com