【发布时间】: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