Wiener filter to deblur the image

What is image deblurring?

Image deblurring is an image processing technique designed to restore clarity to blurred or distorted images. In many cases, images may become blurred due to camera or handheld device movement, which can affect image quality and readability. Image deblurring technology can improve the quality and readability of images by restoring or restoring them to remove blur effects.

Image deblurring methods

Image deblurring can be achieved in a number of ways, some of the commonly used methods include:

  • Statistical methods: including Wiener filter, L0 gradient minimization, etc.
  • Fourier transform method: including inverse filter, Wiener filter, etc.
  • Non-blind methods: including Lucy-Richardson algorithm, constraint-based least squares method, etc.
  • Methods based on CNN, GAN, and deep learning.

In this tutorial, we will use the Wiener filter as an example to show how to deblur an image using Python and OpenCV.

Preparation

Before starting, you need to install Python and OpenCV, and you need to prepare a blurred image for testing. You can search the web and download a blurry image, or use the following code to generate a blurry image:

import cv2
import numpy as np

# 生成模糊的图像
image = np.zeros((512, 512), dtype=np.uint8)  # 创建一个 512x512 的黑色图像
cv2.circle(image, (256, 256), 100, 255, -1)  # 在图像中心生成一个半径为 100 的白色圆形
kernel = np.ones((5, 5), np.float32) / 25  # 创建一个 5x5 的均值滤波器
image = cv2.filter2D(image, -1, kernel)  # 对图像进行均值滤波
cv2.imwrite("blurry_image.jpg", image)  # 保存模糊的图像

This code will generate a circular area with a radius of 100, then use a 5x5 mean filter to blur the image, and save it as a file "blurry_image.jpg".

insert image description here
You can also directly blur your own picture, the code is as follows. You can also manually adjust the size of the averaging filter to change the blur of the picture.

import cv2
import numpy as np

# 生成模糊的图像
# image = np.zeros((512, 512), dtype=np.uint8)  # 创建一个 512x512 的黑色图像
image = cv2.imread('img.png')
# cv2.circle(image, (256, 256), 100, 255, -1)  # 在图像中心生成一个半径为 100 的白色圆形
kernel = np.ones((5, 5), np.float32) / 25  # 创建一个 5x5 的均值滤波器
image = cv2.filter2D(image, -1, kernel)  # 对图像进行均值滤波
cv2.imwrite("blurry_image.jpg", image)  # 保存模糊的图像

The generated fuzzy comparison chart is as follows:
insert image description here
insert image description here

Image deblurring steps

Here are the steps to deblur an image using a Wiener filter:

  1. Load a blurry image.
import cv2

# 加载模糊的图像
blurry_image = cv2.imread("blurry_image.jpg", 0)
  1. Computes the convolution kernel for the Wiener filter.
import numpy as np
from numpy.fft import fft2, ifft2

# 计算噪声功率谱
noise_power = 0.01
noise_image = np.random.randn(*blurry_image.shape) * np.sqrt(noise_power)
noise_spectrum = fft2(noise_image)

# 计算图像功率谱
image_spectrum = fft2(blurry_image)

# 计算复原滤波器
kernel = noise_spectrum / (image_spectrum + noise_spectrum)
kernel = np.real(ifft2(kernel))
kernel = np.fft.fftshift(kernel)

In this code, we first generate a random noise image and calculate its power spectrum. We then compute the power spectrum of the blurred image and use it to compute the convolution kernel for the restoration filter. This convolution kernel can be used to deblur the blurred image.

  1. Deblurs blurred images.
# 对模糊的图像进行去模糊处理
restored_image = cv2.filter2D(blurry_image, -1, kernel)

In this code, we use cv2.filter2Dthe function to deblur the blurred image, where the convolution kernel uses the restoration filter calculated in the previous step.

  1. Displays the original image and the deblurred image.
import matplotlib.pyplot as plt

# 显示原始图像和去模糊处理后的图像
plt.subplot(1, 2, 1)
plt.imshow(blurry_image, cmap="gray")
plt.title("Blurry Image")
plt.subplot(1, 2, 2)
plt.imshow(restored_image, cmap="gray")
plt.title("Restored Image")
plt.show()

In this code, we use matplotlib.pyplotthe library to display the original image and the deblurred image.

full code

Here is the complete Python code to deblur the image:

import cv2
import numpy as np
from numpy.fft import fft2, ifft2
import matplotlib.pyplot as plt

# 加载模糊的图像
blurry_image = cv2.imread("blurry_image.jpg", 0)

# 计算噪声功率谱
noise_power = 0.01  # 噪声功率
noise_image = np.random.randn(*blurry_image.shape) * np.sqrt(noise_power)  # 生成随机噪声图像
noise_spectrum = fft2(noise_image)  # 计算噪声功率谱

# 计算图像功率谱
image_spectrum = fft2(blurry_image)  # 计算模糊图像的功率谱

# 计算复原滤波器
kernel = noise_spectrum / (image_spectrum + noise_spectrum)  # 计算复原滤波器
kernel = np.real(ifft2(kernel))  # 将复原滤波器从频域转换到空域
kernel = np.fft.fftshift(kernel)  # 对滤波器进行中心化

# 对模糊的图像进行去模糊处理
restored_image = cv2.filter2D(blurry_image, -1, kernel)  # 使用复原滤波器对模糊图像进行去模糊处理

# 显示原始图像和去模糊处理后的图像
plt.subplot(1, 2, 1)
plt.imshow(blurry_image, cmap="gray")
plt.title("Blurry Image")
plt.subplot(1, 2, 2)
plt.imshow(restored_image, cmap="gray")
plt.title("Restored Image")
plt.show()

insert image description here

in conclusion

In this tutorial, we showed how to deblur an image using Python and OpenCV. We introduce the basic steps of image deblurring using the Wiener filter as an example. You can use this tutorial as a starter guide to learn more about image deblurring techniques and explore more methods and techniques.

Guess you like

Origin blog.csdn.net/qq_36693723/article/details/130478004