您当前的位置:首页 > IT编程 > python
| C语言 | Java | VB | VC | python | Android | TensorFlow | C++ | oracle | 学术与代码 | cnn卷积神经网络 | gnn | 图像修复 | Keras | 数据集 | Neo4j | 自然语言处理 | 深度学习 | 医学CAD | 医学影像 | 超参数 | pointnet | pytorch | 异常检测 |

自学教程:python中如何提高图像质量

51自学网 2023-06-16 18:56:12
  python
这篇教程python中如何提高图像质量写得很实用,希望能帮到您。

python提高图像质量

概述

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

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

百度智能云

官方教程:链接

参考代码(方便的一塌糊涂):

from aip import AipImageProcessimport base64import osAPP_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_filefor 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 Imagefrom PIL import ImageEnhanceimport numpy as npimport matplotlib.pyplot as pltplt.rcParams['font.sans-serif'] = ['FangSong'] # 设置字体以便正确显示汉字plt.rcParams['axes.unicode_minus'] = False # 正确显示连字符# 原图image = Image.open('img/timg.jpg')# 亮度增强enh_bri = ImageEnhance.Brightness(image)brightness = 2image_brightened = enh_bri.enhance(brightness)# 色度增强(饱和度↑)enh_col = ImageEnhance.Color(image)color = 2image_colored = enh_col.enhance(color)# 对比度增强enh_con = ImageEnhance.Contrast(image)contrast = 2image_contrasted = enh_con.enhance(contrast)# 锐度增强enh_sha = ImageEnhance.Sharpness(image)sharpness = 4.0image_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实现

链接

python实现图像质量评价准则PSNR

计算PSNR的Python代码,网上有下面两种:

import cv2import numpy as npimport mathdef psnr1(img1, img2):
opencv实践项目之图像拼接详细步骤
python中pandas库的iloc函数用法解析
51自学网自学EXCEL、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。
京ICP备13026421号-1