[Python图像处理] 基于离散余弦变换的图像去噪

基于离散余弦变换的图像去噪原理

在本节中,我们将学习如何使用离散余弦变换( Discrete Cosine Transform, DCT) 对带有噪声的 RGB 彩色图像执行去噪操作,得到纯净的原始图像。为了实现此操作,我们使用 OpenCV 库中的一个简单有效的去噪函数 dctDenoising(),该函数为了达到去噪的目的在内部使用了局部 DCT 阈值。算法在去相关之后,将阈值分别应用于每个颜色通道。由于该算法的简单性和高性能,通常被视为开发新算法时的比较基准和性能下限。函数 dctDenoising() 的调用方式如下所示:

cv2.xphoto.dctDenoising(src, sigma, psize)

该函数实现了简单的基于 DCT 的图像降噪。

利用 DCT 实现图像去噪

(1) 首先,我们从 scikit-image.restoration 模块中导入 estimate_sigma() 函数以估算噪声 σ σ σ,并导入其他相关库与函数:

from skimage import img_as_float
from skimage.restoration import estimate_sigma
import cv2
from skimage.io import imread
import numpy as np
import matplotlib.pylab as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
from matplotlib.ticker import LinearLocator, FormatStrFormatter
from scipy.fftpack import dct, idct

(2) 读取输入 RGB 彩色图像,并使用 np.random.standard_normal() 函数和参数 sigma = 0.25 在图像中添加噪声,然后,使用 np.random() 函数缩放每个通道的像素值至范围 [0,1] 之间:

im = img_as_float(imread('1.png'))
sigma = 0.25
noisy = im + sigma * np.random.standard_normal(im.shape)
noisy = np.clip(noisy, 0, 1)

(3) 使用 estimate_sigma() 函数估算带有噪声的图像的标准噪声差 sigmma ( σ σ σ):

sigma_est = np.mean(estimate_sigma(noisy, multichannel=True))
print("estimated noise standard deviation = {}".format(sigma_est))

(4) 使用 cv2.xphoto.dctDenoising() 函数获得降噪后的输出图像:

out = noisy.copy()
cv2.xphoto.dctDenoising(noisy, out, sigma_est)
out = np.clip(out, 0, 1)

(5) 通过使用 matplotlib.subplot() 绘制原始、带有噪声的图像和降噪后的输出图像:

plt.figure(figsize=(20,10))
plt.subplot(131), plt.imshow(im), plt.axis('off'), plt.title('original', size=10)
plt.subplot(132), plt.imshow(noisy), plt.axis('off'), plt.title('noisy', size=10)
plt.subplot(133), plt.imshow(out), plt.axis('off'), plt.title('denoised (DCT)', size=10)
plt.tight_layout()
plt.show()

获得的结果图像如下所示,在下图中可以看出使用 dctDenoising() 函数后得到的图像可以消除图像中的大部分图像噪声,得到较为纯净的图像,但相比原始图像,降噪后得到的图像中的细节和边缘会部分丢失:

基于DCT执行图像去噪

相关链接

Python图像处理【1】图像与视频处理基础
Python图像处理【2】探索Python图像处理库
Python图像处理【3】Python图像处理库应用
Python图像处理【4】图像线性变换
Python图像处理【5】图像扭曲/逆扭曲
Python图像处理【6】通过哈希查找重复和类似的图像
Python图像处理【7】采样、卷积与离散傅里叶变换
Python图像处理【8】使用低通滤波器模糊图像
Python图像处理【9】使用高通滤波器执行边缘检测
Python图像处理【10】基于离散余弦变换的图像压缩

猜你喜欢

转载自blog.csdn.net/qq_30167691/article/details/128434849