A brief introduction to image denoising—with example code

A brief introduction to image denoising—with example code

Image denoising refers to removing noise from an image to make it clearer. In this tutorial, we will provide an in-depth introduction to the basic principles of image denoising, common noise types, and several commonly used denoising methods, including traditional filtering methods and deep learning methods.

Fundamentals of Denoising

Before explaining the method of denoising, we need to understand some basic principles. Noise is generally defined as any undesired component of a signal, which may come from electronic noise in the camera or sensor, signal interference during image transmission, or confusion caused by environmental conditions at the time of filming, such as high temperature, strong Light or color distortion, etc.

The main principle of denoising is to eliminate or suppress noise and preserve the characteristics of the original image as much as possible. In the denoising process, we need to distinguish the noise in the image from the useful image information, and try to restore the details and information of the original image.

Common Types of Noise

Before image denoising, it is important to understand the different types of noise and their statistical properties. Several common noise types are described below.

Gaussian noise

Gaussian noise is a type of noise that satisfies a normal distribution. It can form in various cameras, sensors, and communication channels, usually due to unavoidable random fluctuations.

salt and pepper noise

Salt and pepper noise is usually caused by the loss of pixel information during digital image transmission or storage. It randomly appears black or white pixels in the image, with a noticeable impact on image quality.

mosaic noise

Mosaic noise is usually caused by pixelation and compression. This noise makes the image appear to be composed of a group of small squares of the same color, which affects the quality and clarity of the image.

Impulse noise

Impulse noise is caused by bursty signals that occur over a short period of time. It usually appears as obvious high brightness/low brightness pixels, which greatly affects the quality and clarity of the image.

code to add noise

Add Gaussian noise

Here is a sample code for adding Gaussian noise to an image using Python and NumPy libraries:

import cv2
import numpy as np

# 读取图像
img = cv2.imread('input.jpg')

# 添加高斯噪声
mean = 0
var = 0.1
sigma = var ** 0.5
gaussian = np.random.normal(mean, sigma, img.shape)
gaussian = gaussian.reshape(img.shape)
noisy_img = img + gaussian

# 将像素值限制在 0 和 255 之间
noisy_img = np.clip(noisy_img, 0, 255)

# 显示带噪声的图像
cv2.imshow('noisy image', noisy_img.astype(np.uint8))
cv2.waitKey(0)
cv2.destroyAllWindows()

In this example, we first use cv2.imread()the function to read an input.jpgimage named . We then use np.random.normal()the function to generate a Gaussian noise array of the same size as the input image and add it to the input image. Finally, we use np.clip()the function to clamp pixel values ​​between 0 and 255, and cv2.imshow()the function to display the noisy image.

When adding Gaussian noise, we can adjust varthe parameter to control the strength of the noise. If varthe value is small, the noise will be less noticeable; if varthe value is large, the noise will be more noticeable.

Add salt and pepper noise

Here is an example code for adding salt and pepper noise to an image using the Python and NumPy libraries:

import cv2
import numpy as np

# 读取图像
img = cv2.imread('input.jpg')

# 添加椒盐噪声
p = 0.05  # 像素点被替换为椒盐噪声的概率
salt_vs_pepper = 0.5  # 椒盐噪声的比例
num_salt = np.ceil(p * img.size * salt_vs_pepper)
num_pepper = np.ceil(p * img.size * (1.0 - salt_vs_pepper))
coords = [np.random.randint(0, i - 1, int(num_salt)) for i in img.shape]
img[coords[0], coords[1], :] = 255
coords = [np.random.randint(0, i - 1, int(num_pepper)) for i in img.shape]
img[coords[0], coords[1], :] = 0

# 显示带噪声的图像
cv2.imshow('noisy image', img.astype(np.uint8))
cv2.imwrite('noise_pic.jpg', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

In this example, we first use cv2.imread()the function to read an input.jpgimage named . We then use the random number function from the NumPy library to generate salt and pepper noise and add it to the original image. where pis the probability that a pixel is replaced by salt and pepper noise, salt_vs_pepperis the proportion of salt and pepper noise, num_saltand num_pepperis the number of salt and pepper noise points. Finally, we display the noisy image using cv2.imshow()the function .

Note that salt and pepper noise randomly replaces pixels with black or white values, so it can wreak havoc on image detail and features. Therefore, in order to make the noise more realistic, it is usually necessary to use some probability and scale parameters to control the amount and distribution of noise when adding salt and pepper noise.

Gaussian and salt and pepper noise are added at the same time, and the image is processed as follows.

import cv2
import numpy as np

# 读取图像
img = cv2.imread('pic_2.png')

# 添加高斯噪声
mean = 0
var = 0.1
sigma = var ** 0.5
# 生成符合高斯分布的随机噪声,均值为0,标准差为sigma
gaussian = np.random.normal(mean, sigma, img.shape)
# 将噪声的形状改为与原始图像相同
gaussian = gaussian.reshape(img.shape)
# 将随机噪声添加到原始图像中
noisy_img = img + gaussian

# 添加椒盐噪声
s_vs_p = 0.5
amount = 0.05
# 计算添加的椒盐噪声点的数量
num_salt = np.ceil(amount * img.size * s_vs_p)
num_pepper = np.ceil(amount * img.size * (1.0 - s_vs_p))
# 生成坐标数组,用于添加椒盐噪声
coords_salt = [np.random.randint(0, i - 1, int(num_salt)) for i in img.shape]
coords_pepper = [np.random.randint(0, i - 1, int(num_pepper)) for i in img.shape]
# 将椒盐噪声点的像素值分别设置为白色和黑色
noisy_img[coords_salt[0], coords_salt[1], :] = 255
noisy_img[coords_pepper[0], coords_pepper[1], :] = 0

# 将像素值限制在 0 和 255 之间
noisy_img = np.clip(noisy_img, 0, 255)

# 显示带噪声的图像
cv2.imshow('noisy image', noisy_img.astype(np.uint8))
cv2.imwrite('noise_pic.jpg', noisy_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Please add a picture description

Please add a picture description

The whole original picture is as follows:
Please add a picture description

Commonly used denoising methods

Several commonly used denoising methods are introduced below.

mean filtering

Mean filtering is a basic linear filtering algorithm, which refers to replacing the value of the current pixel by taking the average value of the surrounding pixels. This method works well for Gaussian noise in images. The more noise, the more neighborhood pixel values ​​need to be taken. The specific operation is as follows:

  1. First, we need to select a window of size m × n (m* and n* are generally odd numbers).
  2. Place the window on every pixel of the image.
  3. Takes the average of all pixels in the window and sets it as the value of the current pixel.
import cv2

# 读取图像
img = cv2.imread('noise_pic.jpg')

# 均值滤波
dst = cv2.blur(img, (3, 3))

# 保存处理后的图像
cv2.imwrite('output.jpg', dst)

median filter

Median filtering is a simple and effective image denoising method. It works by replacing the value of each pixel with the median of the values ​​of its surrounding pixels. This method is especially useful for removing salt and pepper noise, that is, random black and white pixels.

Median filtering is a nonlinear filtering method, which replaces the value of the current pixel by taking the median value of the neighboring pixel values. Compared with mean filtering, median filtering can better preserve the details of the image. Therefore, it is more suitable for noise types such as salt and pepper noise (where some pixels will be clearly assigned black or white). The specific operation is as follows:

  1. First, we need to select a window of size m × n* (m* and n* are usually odd numbers).
  2. Place the window on every pixel of the image.
  3. Takes the median value of all pixels in the window and sets it as the value of the current pixel.
import cv2

# 读取图像
img = cv2.imread('input.jpg')

# 中值滤波
dst = cv2.medianBlur(img, 3)

# 保存处理后的图像
cv2.imwrite('output.jpg', dst)

Gaussian filter

Gaussian filtering is a widely used image denoising method. It works by replacing the value of each pixel with a weighted average of the values ​​of its surrounding pixels, where the weights are calculated by a Gaussian function. A Gaussian filter can remove Gaussian noise and some other types of noise.

Here is a sample code for removing Gaussian noise from an image using Python and the OpenCV library:

import cv2
import numpy as np

# 读取带噪声的图像
img = cv2.imread('noisy_image.jpg')

# 去除高斯噪声
denoised_img = cv2.GaussianBlur(img, (5, 5), 0)

# 显示去噪后的图像
cv2.imshow('denoised image', denoised_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

In this example, we first use cv2.imread()the function to read an noisy_image.jpgimage named with Gaussian noise. We then apply a Gaussian blur using cv2.GaussianBlur()the function to remove noise from the image. where (5, 5)is the size of the Gaussian kernel, which can be adjusted according to the noise strength and size of the image. Finally, we display the denoised image using cv2.imshow()the function .

Note that Gaussian blur will also blur the image, so the size of the blur kernel needs to be chosen carefully. If the size of the blur kernel is too large, it may cause the image to lose detail and sharpness.

bilateral filtering

Bilateral filtering is a nonlinear filtering method that can remove noise while preserving image edge information. Its principle is to calculate the similarity between pixels based on the spatial distance between pixels and the difference between pixel values, and then replace each pixel value with the weighted average of its surrounding pixels.

import cv2

# 读取图像
img = cv2.imread('input.jpg')

# 双边滤波
dst = cv2.bilateralFilter(img, 7, 50, 50)

# 保存处理后的图像
cv2.imwrite('output.jpg', dst)

Image Denoising Based on Deep Learning

Image denoising methods based on deep learning use Convolutional Neural Networks (CNN) for image denoising. It regards the image denoising problem as a regression problem, using noisy images as input and clean images as output in the training phase. In the test phase, CNN can perform inference based on the noisy image to output a denoised image. Compared with traditional filtering methods, image denoising based on deep learning can better retain image details while removing noise.

Summarize

Image denoising is a broad field involving many techniques and approaches. In practical applications, we can choose different denoising methods according to different noise types and denoising requirements in order to obtain better denoising effects. In the future, with the continuous development of technology, the methods and algorithms of image denoising will be continuously improved and updated.

Image Source

Website: wallhaven

Guess you like

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