我有大约一百张不太清晰的照片,我想让它们更清晰
因此,我用python创建了一个脚本,该脚本已经尝试过一个。我曾尝试使用PIL、OpenCV和OCR阅读器从图像中读取文本
#外部库用于
#图像IO
从PIL导入图像
#形态滤波
从skimage.morphology导入打开
来自skimage.pormology导入磁盘
#数据处理
将numpy导入为np
#连接组件过滤
导入cv2
黑色=0
白色=255
阈值=160
#以灰度模式打开输入图像并获取其像素。
img=图像打开(“image3.png”)转换(“LA”)
像素=np.array(img)[:,:,0]
#删除阈值以上的像素
像素[像素>阈值]=白色
像素[像素<阈值]=黑色
#形态开口
blobSize=1#选择要删除的斑点的最大半径
structureElement=disk(blobSize)#您可以定义不同的形状,这里我们取一个圆盘形状
#我们需要反转图像,使黑色为背景,白色为前景,以执行打开操作
pixels=np.reinvest(开口(np.reinvert(像素),结构元素)
#创建并保存新图像。
newImg=Image.fromarray(像素).convert(RGB)
newImg.save(“newImage1.PNG”)
#查找连接的组件(图像中的黑色对象)
#因为该函数在黑色背景上搜索白色连通组件,所以我们需要反转图像
nb_组件、输出、统计数据、质心=cv2.连通组件WithStats(np.reinvest(像素),连通性=8)
#对于图像中的每个连接组件,您可以从最后一个属性变量中获得像素数
#列。我们从尺寸中删除第一个条目,因为这是背景连接组件的条目
大小=统计值[1:,-1]
nb_组件-=1
#定义组件应包含的最小尺寸(像素数)
最小尺寸=100
#创建新图像
newPixels=np.ones(像素形状)*255
#迭代图像中的所有组件,只保留大于最小大小的组件
对于范围(1,nb_components)中的i:
如果大小[i]>;最小尺寸:
新像素[输出==i+1]=0
#创建并保存新图像。
newImg=Image.fromarray(newPixels).convert('RGB')
newImg.save(“newImage2.PNG”)
但它返回:
我宁愿它不是黑白的,最好的输出是同时放大文本和图像的输出
因此,我用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")
但它返回:
我更希望它不是黑白的,最好的输出是同时放大文本和图像的输出