首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何提高图像质量?

如何提高图像质量?
EN

Stack Overflow用户
提问于 2018-08-24 12:05:19
回答 2查看 23K关注 0票数 21

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

其中一幅剪裁的图片:

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

EN

回答 2

Stack Overflow用户

发布于 2018-08-24 14:33:34

“vasilisg”的答案。是个很好的解决办法。一种进一步改进的方法是使用形态学开放手术来去除剩余的斑点。但是,这只适用于比图像中数字的直线度更小的点。另一种选择是使用openCV连接组件模块删除小于N个像素的“孤岛”。例如,您可以这样做:

代码语言:javascript
运行
复制
# 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")

在本例中,我同时执行了开始和连接组件方法,但是如果使用连接组件方法,通常可以省略开始操作。

结果如下:

脱粒和开场后:

经过阈值化、开启和连接后的组件过滤:

票数 15
EN

Stack Overflow用户

发布于 2018-08-24 12:35:41

一种方法是将图像转换为灰度,然后使用一个阈值与每个像素进行比较,以决定它应该是黑色还是白色。枕头是一个库,可以用于这种类型的处理:

代码语言:javascript
运行
复制
from PIL import Image

black = (0,0,0)
white = (255,255,255)
threshold = (160,160,160)

# Open input image in grayscale mode and get its pixels.
img = Image.open("image.jpg").convert("LA")
pixels = img.getdata()

newPixels = []

# Compare each pixel 
for pixel in pixels:
    if pixel < threshold:
        newPixels.append(black)
    else:
        newPixels.append(white)

# Create and save new image.
newImg = Image.new("RGB",img.size)
newImg.putdata(newPixels)
newImg.save("newImage.jpg")

结果图像:

票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52004133

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档