- 图像复原
图像复原是数字图像处理中的一种技术,旨在从受到噪声、模糊等退化因素影响的图像中恢复原始图像。图像复原的目标是尽可能地还原原始图像,以便于进一步处理和分析。
4.1. 噪声模型
定义:噪声是指图像中的随机信号干扰,它可能源于图像采集、传输等过程中的各种原因。常见的噪声模型有高斯噪声、椒盐噪声等。
数学公式:
高斯噪声:高斯噪声是一种具有正态分布(均值为μ,方差为σ²)的噪声。设原始图像为I(x, y),噪声为N(x, y),受噪声影响的图像为J(x, y),则有 J(x, y) = I(x, y) + N(x, y),其中N(x, y)服从正态分布。
椒盐噪声:椒盐噪声是一种随机出现的黑白噪点。设原始图像为I(x, y),受椒盐噪声影响的图像为J(x, y),则有 J(x, y) = I(x, y) 或 0 或 255,其中0和255分别表示黑点和白点。
用途:噪声模型可用于研究图像复原算法的性能,以及评估图像去噪方法的有效性。
代码实现(Python):
import cv2
import numpy as np
def add_gaussian_noise(image, mean=0, stddev=30):
noise = np.random.normal(mean, stddev, image.shape).astype(np.float32)
noisy_image = np.clip(image + noise, 0, 255).astype(np.uint8)
return noisy_image
def add_salt_and_pepper_noise(image, prob=0.01):
noisy_image = image.copy()
num_salt = int(prob * image.size)
coords = [np.random.randint(0, i - 1, num_salt) for i in image.shape]
noisy_image[tuple(coords)] = 255
num_pepper = int(prob * image.size)
coords = [np.random.randint(0, i - 1, num_pepper) for i in image.shape]
noisy_image[tuple(coords)] = 0
return noisy_image
# 读取图像
image = cv2.imread('path/to/your/image.jpg', cv2.IMREAD_GRAYSCALE)
# 添加高斯噪声和椒盐噪声
gaussian_noise_image = add_gaussian_noise(image)
salt_pepper_noise_image = add_salt_and_pepper_noise(image)
# 显示图像
cv2.imshow('Original Image', image)
cv2.imshow('Gaussian Noise Image', gaussian_noise_image)
cv2.imshow('Salt and Pepper Noise Image', salt_pepper_noise_image)
cv2.waitKey(0)
cv2.destroyAllWindows
4.2. 模糊模型
定义:模糊是指图像中的细节信息丢失,导致图像变得不清晰。常见的模糊模型有运动模糊、大气模糊等。
数学公式:
设原始图像为I(x, y),模糊函数为H(x, y),噪声为N(x, y),受模糊和噪声影响的图像为J(x, y),则有 J(x, y) = (I(x, y) * H(x, y)) + N(x, y),其中*表示卷积操作。
用途:模糊模型可用于研究图像复原算法的性能,以及评估图像去模糊方法的有效性。
代码实现(Python):生成运动模糊图像的例子
```python
import cv2
import numpy as np
def motion_blur(image, kernel_size=15, angle=0):
kernel = np.zeros((kernel_size, kernel_size))
kernel[int((kernel_size - 1) / 2), :] = np.ones(kernel_size)
kernel = cv2.getRotationMatrix2D((kernel_size / 2 - 0.5, kernel_size / 2 - 0.5), angle, 1)
kernel = cv2.warpAffine(kernel, (kernel_size, kernel_size))
kernel = kernel / kernel_size
blurred_image = cv2.filter2D(image, -1, kernel)
return blurred_image
# 读取图像
image = cv2.imread('path/to/your/image.jpg', cv2.IMREAD_GRAYSCALE)
# 添加运动模糊
motion_blurred_image = motion_blur(image, angle=45)
# 显示图像
cv2.imshow('Original Image', image)
cv2.imshow('Motion Blurred Image', motion_blurred_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
4.3. 图像复原算法
定义:图像复原算法是指从受到噪声和模糊等退化因素影响的图像中恢复原始图像的方法。常用的图像复原算法有逆滤波、维纳滤波等。
4.3.1. 逆滤波
数学公式:逆滤波基于退化模型 J(x, y) = (I(x, y) * H(x, y)) + N(x, y)。在频域中,逆滤波可以表示为 O(u, v) = J(u, v) / H(u, v),其中O(u, v)、J(u, v)和H(u, v)分别是输出图像、输入图像和模糊函数的傅里叶变换。
用途:逆滤波可用于从受到模糊影响的图像中恢复原始图像。然而,逆滤波对噪声非常敏感,因此在实际应用中往往需要与其他去噪方法结合使用。
代码实现(Python):逆滤波复原运动模糊图像的例子
import cv2
import numpy as np
def inverse_filter(image, kernel):
dft_image = cv2.dft(np.float32(image), flags=cv2.DFT_COMPLEX_OUTPUT)
dft_kernel = cv2.dft(np.float32(kernel), flags=cv2.DFT_COMPLEX_OUTPUT, nonzeroRows=kernel.shape[0])
dft_kernel_shift = np.fft.fftshift(dft_kernel)
eps = 1e-6
inverse_filtered_shift = dft_image / (dft_kernel_shift + eps)
inverse_filtered = cv2.idft(np.fft.ifftshift(inverse_filtered_shift))
restored_image = cv2.magnitude(inverse_filtered[:, :, 0], inverse_filtered[:, :, 1])
return restored_image
# 读取运动模糊图像
motion_blurred_image = cv2.imread('path/to/your/motion_blurred_image.jpg', cv2.IMREAD_GRAYSCALE)
# 逆滤波复原
kernel_size = 15
kernel = np.zeros((kernel_size, kernel_size))
kernel[int((kernel_size - 1) / 2), :] = np.ones(kernel_size)
kernel = kernel / kernel_size
restored_image = inverse_filter(motion_blurred_image, kernel)
# 显示图像
cv2.imshow('Motion Blurred Image', motion_blurred_image)
cv2.imshow('Restored Image', restored_image.astype(np.uint8))
cv2.waitKey(0)
cv2.destroyAllWindows()
4.3.2. 维纳滤波
数学公式:维纳滤波是一种线性最小均方误差估计方法,可以在复原过程中减小噪声的影响。在频域中,维纳滤波可以表示为 O(u, v) = J(u, v) * (H*(u, v) / (|H(u, v)|² + K)),其中O(u, v)、J(u, v)和H(u, v)分别是输出图像、输入图像和模糊函数的傅里叶变换,H*(u, v)表示H(u, v)的共轭,K是维纳滤波参数,用于控制去噪的程度。
用途:维纳滤波可用于从受到模糊和噪声影响的图像中恢复原始图像。
代码实现(Python):维纳滤波复原运动模糊图像的例子
import cv2
import numpy as np
def wiener_filter(image, kernel, K=0.01):
dft_image = cv2.dft(np.float32(image), flags=cv2.DFT_COMPLEX_OUTPUT)
dft_kernel = cv2.dft(np.float32(kernel), flags=cv2.DFT_COMPLEX_OUTPUT, nonzeroRows=kernel.shape[0])
dft_kernel_shift = np.fft.fftshift(dft_kernel)
wiener_filter_shift = np.conj(dft_kernel_shift) / (np.abs(dft_kernel_shift) ** 2 + K)
filtered_shift = dft_image * wiener_filter_shift
filtered = cv2.idft(np.fft.ifftshift(filtered_shift))
restored_image = cv2.magnitude(filtered[:, :, 0], filtered[:, :, 1])
return restored_image
读取运动模糊图像
motion_blurred_image = cv2.imread('path/to/your/motion_blurred_image.jpg', cv2.IMREAD_GRAYSCALE)
维纳滤波复原
kernel_size = 15
kernel = np.zeros((kernel_size, kernel_size))
kernel[int((kernel_size - 1) / 2), :] = np.ones(kernel_size)
kernel = kernel / kernel_size
restored_image = wiener_filter(motion_blurred_image, kernel, K=0.01)
显示图像
cv2.imshow('Motion Blurred Image', motion_blurred_image)
cv2.imshow('Restored Image', restored_image.astype(np.uint8))
cv2.waitKey(0)
cv2.destroyAllWindows()
扩展:
图像复原领域还有许多其他算法,如:最大似然估计、最大后验估计、正则化方法、基于稀疏表示的方法等。此外,深度学习方法(如卷积神经网络、自编码器等)在图像复原任务上也取得了显著的成果。可以根据实际需求和场景选择合适的图像复原方法。