[Python Image Processing] Eliminate random noise based on image mean

Eliminate random noise based on image mean

Preface

In this section, we will learn how to estimate a noise-free image from a set of noisy input images. All images are created by adding i.i.d. random noise using the original (noise-free) image pixels, only The mean/median value of the noisy image needs to be calculated.

Image mean calculation

Order XXX is the sizeh × wh × wh×The original input image of w and added by addingnnn different random noise matrices generatenn noise output images,E k E_kEkis the size hxw hxwA random noise matrix of h x w , so the image with noise isX + E k X +E_kX+Ek, where k = 1, 2, 3… k = 1,2,3…k=1,2,3 .
We assume that the noise matrixE k E_kEkEach element in is 0a Gaussian function with mean that satisfies independent and identical distribution, E k ( i , j ) E^{(i,j)}_kEk(i,j)represents the kthk elements of the noise matrix. According to the central limit theorem:

lim ⁡ x → ∞ E ˉ ( i , j ) σ n → 0 {\lim_{x \to \infty}} \frac {\bar E ^{(i,j)}} {\frac {\sigma} {\sqrt n}}\to 0 xlimn pEˉ(i,j)0

Therefore, the following results can be obtained:

E ˉ ( i , j ) = ∑ k = 1 n E k ( i , j ) n {\bar E ^{(i,j)}} =\frac {\sum_{k=1}^n E_k^{(i,j)}} {n} Eˉ(i,j)=nk=1nEk(i,j)

E ˉ ( i , j ) {\bar E ^{(i,j)}}Eˉ( i , j ) corresponds to(i, j) (i,j)(i,j ) average value of elements.
This means that if we have a lot of noisy images, and assuming that the noise satisfies independent and identical distribution, the noise in the image can be removed by simply calculating the mean of the noisy images. If we have a colored noisy image and calculate the mean on each color channel, then by the independent and identical distribution assumption we can obtain an (almost) noise-free image. The theory applies to any distributed noise with zero mean, and does not need to be Gaussian.

Remove image noise

We can also use the median instead of the mean to estimate the noise-free image from the noisy image (which satisfies independent and identical distribution), and we can also get similar results.

(1) Import all necessary libraries, modules and functions to read the input image from disk and convert it to floating point type:

from skimage import img_as_float
from skimage.util import random_noise
from skimage.metrics import peak_signal_noise_ratio
from skimage.io import imread
import matplotlib.pylab as plt
import numpy as np
def plot_image(image, title=None, sz=10):
    plt.imshow(image)
    plt.title(title, size=sz)
    plt.axis('off')

im = img_as_float(imread('1.png')) # original image 

(2)n Create a noisy image by adding independent and identically distributed Gaussian noise (with zero mean) to the original image :

n = 100
images = np.zeros((n, im.shape[0], im.shape[1], im.shape[2]))
sigma = 0.2
for i in range(n):
    images[i,...] = random_noise(im, var=sigma**2)

(3) Use scikit-imagethe library function to calculate the average and median of the noise image:

im_mean = images.mean(axis=0)
im_median = np.median(images, axis=0)

(4) Compare the output image PSNRwith the original noise-free image. Plot the original image, the noisy image, and the output image:

plt.figure(figsize=(10,10))
plt.subplots_adjust(left=.02, right=.98, bottom=.001, top=.96, wspace=.05, hspace=.01)
plt.subplot(221), plot_image(im, 'Original image')
plt.subplot(222), plot_image(images[0], 'Noisy PSNR: ' + str(round(peak_signal_noise_ratio(im, images[0]),3)))
plt.subplot(223), plot_image(im_mean, 'Mean PSNR: ' + str(round(peak_signal_noise_ratio(im, im_mean),3)))
plt.subplot(224), plot_image(im_median, 'Median PSNR: ' + str(round(peak_signal_noise_ratio(im, im_median),3)))
plt.show()

Mean denoising

As can be seen from the above figure, the estimated noise-free image is visually similar to the original noise-free image, and also has high performance PSNR.

(5) Draw a color channel histogram based on a given image with noise, draw the true value of the pixel as a solid line, and draw the estimated pixel value of each color channel obtained by calculating the mean value as a dotted line:

plt.figure(figsize=(10,5))
plt.hist(images[:,100,100,0], color='red', alpha=0.2, label='red')
plt.hist(images[:,100,100,1], color='green', alpha=0.2, label='green')
plt.hist(images[:,100,100,2], color='blue', alpha=0.2, label='blue')
plt.vlines(im[100,100,0], 0, 20, color='red', label='original')
plt.vlines(im[100,100,1], 0, 20, color='green', label='original')
plt.vlines(im[100,100,2], 0, 20, color='blue', label='original')
plt.vlines(im_mean[100,100,0], 0, 20, color='red', linestyles='dashed', label='estimated')
plt.vlines(im_mean[100,100,1], 0, 20, color='green', linestyles='dashed', label='estimated')
plt.vlines(im_mean[100,100,2], 0, 20, color='blue', linestyles='dashed', label='estimated')
plt.legend()
plt.grid()
plt.show()

Histogram

Related Links

Python image processing [1] Basics of image and video processing
Python image processing [2] Explore Python image processing library
Python image processing [3] Python image processing library application
Python image processing [4] Image linear transformation
Python image processing [5] Image distortion /Inverse twist
Python image processing [6] Find duplicate and similar images by hashing
Python image processing [7] Sampling, convolution and discrete Fourier transform
Python image processing [8] Blur the image using a low-pass filter
Python image processing [9] Perform edge detection using high-pass filter
Python image processing [10] Image compression based on discrete cosine transform
Python image processing [11] Perform image deblurring using deconvolution
Python image processing [12] Perform image denoising based on wavelet transform
Python Image Processing [13] Perform image denoising using PIL
Python Image Processing [14] Image denoising based on nonlinear filters
Python Image Processing [15] Sharpen image based on unsharp mask

Guess you like

Origin blog.csdn.net/qq_30167691/article/details/134278515