IT评测·应用市场-qidao123.com技术社区

标题: 使用opencv优化图片(画面变清晰) [打印本页]

作者: 笑看天下无敌手    时间: 2024-9-13 04:18
标题: 使用opencv优化图片(画面变清晰)
需求

   对图像进行优化,使其看起来更清晰,同时保持尺寸稳定,通常涉及到图像处理技能如锐化、降噪、对比度增强等
  影响照片清晰度的因素

影响照片清晰度的因素有许多,重要可以从以下几个方面来分析
1. 拍摄设备

2. 拍摄条件

3. 拍摄参数

4. 后期处理

5. 存储和传输

6. 观看介质
显示器质量:显示设备的分辨率、色彩还原本领等也会影响观看体验。显示器的亮度、对比度和分辨率同样影响图像清晰度。
其他知识
一寸照是一种标准的证件照片尺寸,在不同的国家和地区有不同的详细尺寸要求。在中国,一寸照片的常见规格如下

数码照片的像素尺寸

实现

   使用 OpenCV 让图像变得更加清晰,可以通过多种图像处理技能来实现。这些技能重要包括降噪、锐化、对比度增强等。
  降噪


中值滤波(Median Filter)
   原理:中值滤波是一种非线性滤波器,它通过对图像中的像素值进行排序并取中间值来进行滤波处理。对于图像中的每个像素,选取其四周一定地区内的全部像素值,并对其进行排序,然后将排序后的像素值的中位数赋予该像素。
  特点:中值滤波可以或许有效去除椒盐噪声和脉冲噪声,对图像中的孤立噪声点具有较强的抑制本领。同时,它可以或许较好地保留图像的边沿和细节信息,不会造成图像含糊。然而,中值滤波对于高斯噪声的去除结果不佳,且计算量相对较大
高斯滤波(Gaussian Filter)
   原理:高斯滤波是一种线性平滑滤波器,它使用高斯函数对图像进行加权平均,从而有效地去除噪声并平滑图像。高斯滤波通过卷积操作实现,卷积核的巨细和标准差决定了滤波的强度和结果
  特点:高斯滤波具有良好的平滑结果,可以或许有效地抑制图像中的噪声。同时,高斯滤波具有旋转稳定性和尺度稳定性,适用于各种方向和尺度的噪声。然而,高斯滤波会造成图像细节丢失,低落图像锐度,对椒盐噪声等非平滑噪声的去除结果不佳。
双边滤波(Bilateral Filter)
   原理:双边滤波是一种边沿保留滤波方法,它不但考虑像素的空间间隔,还考虑像素值的相似性。在滤波过程中,间隔中央点越近且像素值越相似的像素,其权重越大。这样,双边滤波可以有效地保留图像边沿,同时抑制噪声。
  特点:双边滤波结合了图像的空间邻近度和像素值相似度,通过对像素的空间间隔和颜色相似度进行加权平均来实现滤波。它常用于图像去噪、边沿增强、细节保留等。双边滤波可以或许较好地保留图像的边沿信息,但计算量相对较大。
测试代码

  1. import cv2  
  2. import numpy as np
  3. # 黑白
  4. def denoise_image(image_path, output_path):
  5.     img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
  6.     dst = cv2.fastNlMeansDenoising(img, None, 10, 7, 21)
  7.     #cv2.imwrite(output_path, dst)
  8.     cv2.imshow('Original', img)
  9.     cv2.imshow('dst', dst)
  10.     cv2.waitKey(0)  
  11.     cv2.destroyAllWindows()
  12. #彩色
  13. def denoise_color_image(image_path, output_path):
  14.     img = cv2.imread(image_path)
  15.     dst = cv2.fastNlMeansDenoisingColored(img, None, 10, 10, 7, 21)
  16.     #cv2.imwrite(output_path, dst)
  17.     cv2.imshow('Original', img)
  18.     cv2.imshow('dst', dst)
  19.     cv2.waitKey(0)  
  20.     cv2.destroyAllWindows()
  21. #denoise_image("test01.jpg", "test02.jpg")
  22. denoise_color_image("test01.jpg", "test02.jpg")
复制代码
双边滤波
  1. def bilateral_filtering(image_path, output_path):
  2.      img = cv2.imread(image_path)
  3.      dst = cv2.bilateralFilter(img, 9, 75, 75)
  4.      cv2.imwrite(output_path, dst)
复制代码
中值滤波可以有效去除椒盐噪声,但对于高斯噪声结果有限
  1. def median_filtering(image_path, output_path):
  2.     img = cv2.imread(image_path)
  3.     dst = cv2.medianBlur(img, 5)
  4.     cv2.imwrite(output_path, dst)
复制代码
锐化


空间锐化

空间域锐化重要通过卷积操作实现
比如
Laplacian 锐化是一种基于二阶导数的锐化方法,它使用 Laplacian 算子来增强图像的边沿
  1. import cv2
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. def laplacian_sharpen(image_path):
  5.     img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
  6.     laplacian = cv2.Laplacian(img, cv2.CV_64F)
  7.     sharpened = cv2.convertScaleAbs(img - laplacian)
  8.     return sharpened
  9. sharp_laplacian = laplacian_sharpen(image_path)
复制代码
Unsharp Masking

是一种经典的锐化方法,通过减去稍微含糊后的图像来增强边沿
  1. def unsharp_mask(image, kernel_size=(5, 5), sigma=1.0, amount=1.0, threshold=0):
  2.     blurred = cv2.GaussianBlur(image, kernel_size, sigma)
  3.     unsharp_image = cv2.addWeighted(image, 1 + amount, blurred, -amount, 0)
  4.     return unsharp_image
  5. sharp_unsharp_mask = unsharp_mask(img, amount=1.5)
复制代码
频率域锐化

频率域锐化则是通过对图像进行傅里叶变换后,在频域中进行处理,再逆变换回到空域
通过增强高频成分来锐化图像
  1. def high_frequency_boost(image, kernel_size=(5, 5), sigma=1.0, amount=1.0):
  2.     blurred = cv2.GaussianBlur(image, kernel_size, sigma)
  3.     gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  4.     high_pass = gray - blurred
  5.     sharpened = cv2.addWeighted(gray, 1 + amount, high_pass, amount, 0)
  6.     return sharpened
  7. sharp_high_frequency = high_frequency_boost(img, amount=1.5)
复制代码
对比测试

  1. # 读取原始图像
  2. image_path = 'path/to/your/input_image.jpg'
  3. img = cv2.imread(image_path)
  4. img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)  # 转换为 RGB 格式以正确显示颜色
  5. # Laplacian 锐化
  6. sharp_laplacian = laplacian_sharpen(image_path)
  7. # Unsharp Masking
  8. sharp_unsharp_mask = unsharp_mask(img, amount=1.5)
  9. # High-Frequency Boosting
  10. sharp_high_frequency = high_frequency_boost(img, amount=1.5)
  11. # 展示图像
  12. plt.figure(figsize=(12, 12))
  13. plt.subplot(2, 2, 1)
  14. plt.imshow(img)
  15. plt.title('Original Image')
  16. plt.axis('off')
  17. plt.subplot(2, 2, 2)
  18. plt.imshow(sharp_laplacian, cmap='gray')
  19. plt.title('Laplacian Sharpened')
  20. plt.axis('off')
  21. plt.subplot(2, 2, 3)
  22. plt.imshow(sharp_unsharp_mask)
  23. plt.title('Unsharp Masking')
  24. plt.axis('off')
  25. plt.subplot(2, 2, 4)
  26. plt.imshow(sharp_high_frequency, cmap='gray')
  27. plt.title('High-Frequency Boosting')
  28. plt.axis('off')
  29. plt.tight_layout()
  30. plt.show()
复制代码
注意颜色通道题目
  1. 报错
  2. high_pass = gray - blurred  报错 ValueError: operands could not be broadcast together with shapes (413,295) (413,295,3)
复制代码

对比度增强

OpenCV 提供了多种方法来增强图像的对比度
常用算法


对比测试

  1. import cv2
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. # 定义直方图均衡化函数
  5. def histogram_equalization(image):
  6.     # 应用直方图均衡化
  7.     eq = cv2.equalizeHist(image)
  8.     return eq
  9. # 定义自适应直方图均衡化函数
  10. def adaptive_histogram_equalization(image):
  11.     # 创建 CLAHE 对象
  12.     clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
  13.     # 应用自适应直方图均衡化
  14.     cl = clahe.apply(image)
  15.     return cl
  16. # 定义 Gamma 校正函数
  17. def gamma_correction(image, gamma=1.0):
  18.     inv_gamma = 1.0 / gamma
  19.     table = np.array([((i / 255.0) ** inv_gamma) * 255 for i in range(256)]).astype("uint8")
  20.     # 应用 Gamma 校正查找表
  21.     adjusted = cv2.LUT(image, table)
  22.     return adjusted
  23. # 定义对比度拉伸函数
  24. def contrast_stretching(image, low_percentile=2, high_percentile=98):
  25.     # 计算灰度级的百分位数
  26.     low_val = np.percentile(image, low_percentile)
  27.     high_val = np.percentile(image, high_percentile)
  28.    
  29.     # 应用对比度拉伸
  30.     stretched = np.clip((image - low_val) * 255 / (high_val - low_val), 0, 255).astype(np.uint8)
  31.     return stretched
  32. # 读取原始图像
  33. image_path = 'test02.jpg'
  34. img_gray = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
  35. # 对灰度图像进行处理
  36. enhanced_eq = histogram_equalization(img_gray)
  37. enhanced_clahe = adaptive_histogram_equalization(img_gray)
  38. enhanced_gamma = gamma_correction(img_gray, gamma=0.8)
  39. enhanced_stretched = contrast_stretching(img_gray)
  40. # 将处理后的灰度图像转换为彩色图像
  41. enhanced_eq_color = cv2.cvtColor(enhanced_eq, cv2.COLOR_GRAY2RGB)
  42. enhanced_clahe_color = cv2.cvtColor(enhanced_clahe, cv2.COLOR_GRAY2RGB)
  43. enhanced_gamma_color = cv2.cvtColor(enhanced_gamma, cv2.COLOR_GRAY2RGB)
  44. enhanced_stretched_color = cv2.cvtColor(enhanced_stretched, cv2.COLOR_GRAY2RGB)
  45. # 展示图像
  46. plt.figure(figsize=(12, 12))
  47. plt.subplot(3, 2, 1)
  48. plt.imshow(cv2.cvtColor(img_gray, cv2.COLOR_GRAY2RGB))
  49. plt.title('Original Grayscale Image')
  50. plt.axis('off')
  51. plt.subplot(3, 2, 2)
  52. plt.imshow(enhanced_eq_color)
  53. plt.title('Histogram Equalization')
  54. plt.axis('off')
  55. plt.subplot(3, 2, 3)
  56. plt.imshow(enhanced_clahe_color)
  57. plt.title('Adaptive Histogram Equalization')
  58. plt.axis('off')
  59. plt.subplot(3, 2, 4)
  60. plt.imshow(enhanced_gamma_color)
  61. plt.title('Gamma Correction')
  62. plt.axis('off')
  63. plt.subplot(3, 2, 5)
  64. plt.imshow(enhanced_stretched_color)
  65. plt.title('Contrast Stretching')
  66. plt.axis('off')
  67. plt.tight_layout()
  68. plt.show()
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com技术社区 (https://dis.qidao123.com/) Powered by Discuz! X3.4