使用python提升图片清晰度的常见方法

以下是使用 Python 提升图片清晰度的常见方法及实现代码,涵盖传统图像处理算法和深度学习方案:

图片[1]_使用python提升图片清晰度的常见方法_知途无界

一、传统图像增强方法

1. 锐化滤波

import cv2
import numpy as np
def sharpen(img):
kernel = np.array([[0, -1, 0],
[-1, 5, -1],
[0, -1, 0]])
return cv2.filter2D(img, -1, kernel)
img = cv2.imread('blurry.jpg')
sharpened = sharpen(img)
cv2.imwrite('sharpened.jpg', sharpened)
import cv2
import numpy as np

def sharpen(img):
    kernel = np.array([[0, -1, 0],
                       [-1, 5, -1],
                       [0, -1, 0]])
    return cv2.filter2D(img, -1, kernel)

img = cv2.imread('blurry.jpg')
sharpened = sharpen(img)
cv2.imwrite('sharpened.jpg', sharpened)
import cv2 import numpy as np def sharpen(img): kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]]) return cv2.filter2D(img, -1, kernel) img = cv2.imread('blurry.jpg') sharpened = sharpen(img) cv2.imwrite('sharpened.jpg', sharpened)

2. 非局部均值去噪

def denoise_nlm(img):
return cv2.fastNlMeansDenoisingColored(img, None, 10, 10, 7, 21)
cleaned = denoise_nlm(img)
def denoise_nlm(img):
    return cv2.fastNlMeansDenoisingColored(img, None, 10, 10, 7, 21)

cleaned = denoise_nlm(img)
def denoise_nlm(img): return cv2.fastNlMeansDenoisingColored(img, None, 10, 10, 7, 21) cleaned = denoise_nlm(img)

3. 超分辨率重建(EDSR 模型)

# 安装依赖:pip install opencv-contrib-python
sr = cv2.dnn_superres.DnnSuperResImpl_create()
sr.readModel('EDSR_x4.pb') # 需下载预训练模型
sr.setModel('edsr', 4) # 4倍超分
result = sr.upsample(img)
# 安装依赖:pip install opencv-contrib-python
sr = cv2.dnn_superres.DnnSuperResImpl_create()
sr.readModel('EDSR_x4.pb')  # 需下载预训练模型
sr.setModel('edsr', 4)  # 4倍超分
result = sr.upsample(img)
# 安装依赖:pip install opencv-contrib-python sr = cv2.dnn_superres.DnnSuperResImpl_create() sr.readModel('EDSR_x4.pb') # 需下载预训练模型 sr.setModel('edsr', 4) # 4倍超分 result = sr.upsample(img)

二、基于深度学习的方案

1. 使用 ESRGAN

from PIL import Image
import torch
from basicsr.archs.rrdbnet_arch import RRDBNet
model = RRDBNet(num_in_ch=3, num_out_ch=3)
model.load_state_dict(torch.load('ESRGAN.pth'))
input_img = Image.open('input.jpg').convert('RGB')
output = model(input_img) # 需配置预处理
from PIL import Image
import torch
from basicsr.archs.rrdbnet_arch import RRDBNet

model = RRDBNet(num_in_ch=3, num_out_ch=3)
model.load_state_dict(torch.load('ESRGAN.pth'))
input_img = Image.open('input.jpg').convert('RGB')
output = model(input_img)  # 需配置预处理
from PIL import Image import torch from basicsr.archs.rrdbnet_arch import RRDBNet model = RRDBNet(num_in_ch=3, num_out_ch=3) model.load_state_dict(torch.load('ESRGAN.pth')) input_img = Image.open('input.jpg').convert('RGB') output = model(input_img) # 需配置预处理

2. 实用工具库 Real-ESRGAN

# 安装
pip install realesrgan
# 使用
from realesrgan import RealESRGANer
upsampler = RealESRGANer(scale=4, model_path='RealESRGAN_x4plus.pth')
output, _ = upsampler.enhance(img)
# 安装
pip install realesrgan

# 使用
from realesrgan import RealESRGANer
upsampler = RealESRGANer(scale=4, model_path='RealESRGAN_x4plus.pth')
output, _ = upsampler.enhance(img)
# 安装 pip install realesrgan # 使用 from realesrgan import RealESRGANer upsampler = RealESRGANer(scale=4, model_path='RealESRGAN_x4plus.pth') output, _ = upsampler.enhance(img)

三、频域增强方法

小波变换增强

import pywt
def wavelet_enhance(img):
# RGB转YCrCb
ycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
y = ycrcb[:,:,0]
# 小波分解
coeffs = pywt.dwt2(y, 'haar')
cA, (cH, cV, cD) = coeffs
# 高频增强
cH *= 1.5
cV *= 1.5
cD *= 1.2
# 重构
coeffs = cA, (cH, cV, cD)
y_enhanced = pywt.idwt2(coeffs, 'haar')
# 合并通道
ycrcb[:,:,0] = np.clip(y_enhanced, 0, 255)
return cv2.cvtColor(ycrcb, cv2.COLOR_YCrCb2BGR)
import pywt

def wavelet_enhance(img):
    # RGB转YCrCb
    ycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
    y = ycrcb[:,:,0]

    # 小波分解
    coeffs = pywt.dwt2(y, 'haar')
    cA, (cH, cV, cD) = coeffs

    # 高频增强
    cH *= 1.5
    cV *= 1.5
    cD *= 1.2

    # 重构
    coeffs = cA, (cH, cV, cD)
    y_enhanced = pywt.idwt2(coeffs, 'haar')

    # 合并通道
    ycrcb[:,:,0] = np.clip(y_enhanced, 0, 255)
    return cv2.cvtColor(ycrcb, cv2.COLOR_YCrCb2BGR)
import pywt def wavelet_enhance(img): # RGB转YCrCb ycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb) y = ycrcb[:,:,0] # 小波分解 coeffs = pywt.dwt2(y, 'haar') cA, (cH, cV, cD) = coeffs # 高频增强 cH *= 1.5 cV *= 1.5 cD *= 1.2 # 重构 coeffs = cA, (cH, cV, cD) y_enhanced = pywt.idwt2(coeffs, 'haar') # 合并通道 ycrcb[:,:,0] = np.clip(y_enhanced, 0, 255) return cv2.cvtColor(ycrcb, cv2.COLOR_YCrCb2BGR)

四、完整处理流程示例

def full_enhance_pipeline(img_path):
# 读取
img = cv2.imread(img_path)
# 1. 去噪
img = cv2.fastNlMeansDenoisingColored(img, None, 5, 5, 3, 9)
# 2. 锐化
kernel = np.array([[-1,-1,-1],
[-1,9,-1],
[-1,-1,-1]])
img = cv2.filter2D(img, -1, kernel)
# 3. 超分辨率 (需提前下载模型)
sr = cv2.dnn_superres.DnnSuperResImpl_create()
sr.readModel('FSRCNN_x4.pb')
sr.setModel('fsrcnn', 4)
img = sr.upsample(img)
# 4. 对比度增强
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
l, a, b = cv2.split(lab)
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
l = clahe.apply(l)
img = cv2.merge((l,a,b))
img = cv2.cvtColor(img, cv2.COLOR_LAB2BGR)
return img
def full_enhance_pipeline(img_path):
    # 读取
    img = cv2.imread(img_path)

    # 1. 去噪
    img = cv2.fastNlMeansDenoisingColored(img, None, 5, 5, 3, 9)

    # 2. 锐化
    kernel = np.array([[-1,-1,-1], 
                      [-1,9,-1],
                      [-1,-1,-1]])
    img = cv2.filter2D(img, -1, kernel)

    # 3. 超分辨率 (需提前下载模型)
    sr = cv2.dnn_superres.DnnSuperResImpl_create()
    sr.readModel('FSRCNN_x4.pb')
    sr.setModel('fsrcnn', 4)
    img = sr.upsample(img)

    # 4. 对比度增强
    lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
    l, a, b = cv2.split(lab)
    clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
    l = clahe.apply(l)
    img = cv2.merge((l,a,b))
    img = cv2.cvtColor(img, cv2.COLOR_LAB2BGR)

    return img
def full_enhance_pipeline(img_path): # 读取 img = cv2.imread(img_path) # 1. 去噪 img = cv2.fastNlMeansDenoisingColored(img, None, 5, 5, 3, 9) # 2. 锐化 kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]]) img = cv2.filter2D(img, -1, kernel) # 3. 超分辨率 (需提前下载模型) sr = cv2.dnn_superres.DnnSuperResImpl_create() sr.readModel('FSRCNN_x4.pb') sr.setModel('fsrcnn', 4) img = sr.upsample(img) # 4. 对比度增强 lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB) l, a, b = cv2.split(lab) clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8)) l = clahe.apply(l) img = cv2.merge((l,a,b)) img = cv2.cvtColor(img, cv2.COLOR_LAB2BGR) return img

五、方法对比与选型建议

方法优势局限性适用场景
传统锐化计算快,实时性好可能放大噪声轻度模糊,实时处理
频域增强保留边缘细节参数调节复杂纹理丰富的图像
ESRGAN重建效果最佳需要GPU,速度慢严重退化图像
Real-ESRGAN平衡速度与质量模型体积大(>100MB)通用场景
超分辨率模型分辨率提升明显需要匹配的预训练模型低分辨率图像放大

六、实用技巧

  1. 预处理很重要:先进行去噪再增强
  2. 参数调优:根据图像类型调整锐化强度
  3. 混合策略:传统方法+深度学习组合使用
  4. 硬件加速:对于4K以上图像建议使用GPU

所有代码需要安装OpenCV、PyTorch等库,深度学习模型需提前下载预训练权重。建议从轻度处理开始尝试,逐步增加处理强度以避免引入伪影。

© 版权声明
THE END
喜欢就点个赞,支持一下吧!
点赞29 分享
No matter what happened in the past, you have to believe that the best is yet to come.
无论过去发生过什么,你都要相信,最好的尚未到来
评论 抢沙发
头像
欢迎您留下评论!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容