以下是使用 Python 提升图片清晰度的常见方法及实现代码,涵盖传统图像处理算法和深度学习方案:
![图片[1]_使用python提升图片清晰度的常见方法_知途无界](https://zhituwujie.com/wp-content/uploads/2025/04/d2b5ca33bd20250415100459.png)
一、传统图像增强方法
1. 锐化滤波
import cv2import numpy as npdef 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-pythonsr = 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 Imageimport torchfrom basicsr.archs.rrdbnet_arch import RRDBNetmodel = 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 RealESRGANerupsampler = 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 pywtdef wavelet_enhance(img):# RGB转YCrCbycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)y = ycrcb[:,:,0]# 小波分解coeffs = pywt.dwt2(y, 'haar')cA, (cH, cV, cD) = coeffs# 高频增强cH *= 1.5cV *= 1.5cD *= 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 imgdef 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 imgdef 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) | 通用场景 |
超分辨率模型 | 分辨率提升明显 | 需要匹配的预训练模型 | 低分辨率图像放大 |
六、实用技巧
- 预处理很重要:先进行去噪再增强
- 参数调优:根据图像类型调整锐化强度
- 混合策略:传统方法+深度学习组合使用
- 硬件加速:对于4K以上图像建议使用GPU
所有代码需要安装OpenCV、PyTorch等库,深度学习模型需提前下载预训练权重。建议从轻度处理开始尝试,逐步增加处理强度以避免引入伪影。
© 版权声明
文中内容均来源于公开资料,受限于信息的时效性和复杂性,可能存在误差或遗漏。我们已尽力确保内容的准确性,但对于因信息变更或错误导致的任何后果,本站不承担任何责任。如需引用本文内容,请注明出处并尊重原作者的版权。
THE END
暂无评论内容