【python】提高图像质量

本文探讨了多种提高图像质量的方法,包括使用深度学习模型如微软的旧照片复原模型,虽然需要数据集微调且推理时间较长。另外,介绍了百度智能云的图像处理API,虽便捷但免费次数有限。此外,通过Python的PIL库实现了亮度、饱和度、对比度和锐度的增强,并展示了示例代码。最后,提到了使用OpenCV进行数字图像处理的相关资源链接。
摘要由CSDN通过智能技术生成

概述

调研了一些提高图像质量的方式

  1. 深度学习方法,如微软的Bringing-Old-Photos-Back-to-Life的模型等。存在一些问题,首先是使用深度学习方法没有刚好的模型,得在自己的数据集上微调,比较麻烦,其次是带来的推理时间开销会比较大,不是特别划算,毕竟只是一个小环节。
  2. 商用API,发现百度智能云提供了很多这方面的API,调用相当方便,但是免费次数有限啊。
  3. 基于python自带的PIL 实现图片亮度增强、饱和度增强、对比度增强以及锐度增强。
  4. 基于opencv实现数字图像处理!!!

百度智能云

官方教程:链接
参考代码(方便的一塌糊涂):

from aip import AipImageProcess
import base64
import os


APP_ID = ''
API_KEY = ''
SECRET_KEY = ''

client = AipImageProcess(APP_ID, API_KEY, SECRET_KEY)

""" 读取图片 """
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()


def get_all_file(path):
    all_file=[]
    for i in os.listdir(path):
        file_name=os.path.join(path,i)
        all_file.append(file_name)
    return all_file

for img_path in get_all_file('img'):
    image = get_file_content(img_path)

    """ 调用图像清晰度增强 """
    if not os.path.exists('output'):
        os.mkdir('output')
    response = client.imageDefinitionEnhance(image)
    imgdata = base64.b64decode(response['image'])
    file = open(os.path.join('output', img_path.split('\\')[-1]), 'wb')
    file.write(imgdata)
    file.close()

PIL实现

from PIL import Image
from PIL import ImageEnhance
import numpy as np
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['FangSong'] # 设置字体以便正确显示汉字
plt.rcParams['axes.unicode_minus'] = False # 正确显示连字符

# 原图
image = Image.open('img/timg.jpg')
# 亮度增强
enh_bri = ImageEnhance.Brightness(image)
brightness = 2
image_brightened = enh_bri.enhance(brightness)
# 色度增强(饱和度↑)
enh_col = ImageEnhance.Color(image)
color = 2
image_colored = enh_col.enhance(color)
# 对比度增强
enh_con = ImageEnhance.Contrast(image)
contrast = 2
image_contrasted = enh_con.enhance(contrast)
# 锐度增强
enh_sha = ImageEnhance.Sharpness(image)
sharpness = 4.0
image_sharped = enh_sha.enhance(sharpness)

fig,axes=plt.subplots(nrows=2,ncols=3,figsize=(10,8),dpi=100)
axes[0,0].imshow(np.array(image, dtype=np.uint8)[:,:,::-1])
axes[0,0].set_title("原图")
axes[0,1].imshow(np.array(image_brightened, dtype=np.uint8)[:,:,::-1])
axes[0,1].set_title("亮度增强")
axes[0,2].imshow(np.array(image_colored, dtype=np.uint8)[:,:,::-1])
axes[0,2].set_title("饱和度增强")
axes[1,0].imshow(np.array(image_contrasted, dtype=np.uint8)[:,:,::-1])
axes[1,0].set_title("对比度增强")
axes[1,1].imshow(np.array(image_sharped, dtype=np.uint8)[:,:,::-1])
axes[1,1].set_title("锐度增强")
axes[1,2].imshow(np.array(image_sharped, dtype=np.uint8)[:,:,::-1])
axes[1,2].set_title("锐度增强")
plt.show()

在这里插入图片描述

opencv实现

链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值