【问题标题】:How can I make the text of a photo list clearer?如何使照片列表的文本更清晰?
【发布时间】:2020-08-20 10:03:34
【问题描述】:

我有大约一百张不是很清晰的照片,我想让它们更清晰。

所以我用 python 创建了一个脚本,该脚本已经尝试过。我曾尝试使用 PIL、OpenCV 和 OCR 阅读器从图像中读取文本。

# 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("image3.png").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")

但它会返回:

我不希望它是黑白的,最好的输出是同时放大文本和图像的输出

【问题讨论】:

  • 图像的分辨率非常低。
  • @AlexAlex 是的,这就是问题所在。你知道我如何升级它吗?
  • 升级对您没有帮助。对于 OCR,至少需要 200-300 dpi 的分辨率。任何小于 200 的都是垃圾。
  • @AlexAlex 嗯,好的,我能做些什么让这段文字更整洁吗?
  • 您可以重新扫描页面。

标签: python-3.x image image-processing python-imaging-library


【解决方案1】:

如 cmets 中所述,质量非常差。这不是一个容易的问题。但是,您可以尝试一些技巧。

这似乎是由于对图像/扫描应用了一些抗锯齿。如果可能的话,我会尝试reversing anti-aliasing。如帖子中所述,步骤类似于:

  1. 应用低通滤波器
  2. 差异 = original_image - low_pass_image
  3. sharpened_image = original_image + alpha*difference

代码可能如下所示:

from skimage.filters import gaussian

alpha = 1   # Sharpening factor

low_pass_image = gaussian(image, sigma=1)
difference = original_image - low_pass_image
sharpened_image = original_image + alpha*difference

此外,scikit 图像具有 unsharp maskwiener filter 的实现。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-18
    • 2013-03-25
    • 1970-01-01
    • 2022-01-25
    • 2011-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多