1. Image noise addition
Noise can be defined theoretically as "unpredictable, random errors that can only be recognized by the method of probability and statistics", so image noise can be regarded as a multidimensional random process. Noise can be simulated by its probability distribution function and probability distribution density function. Common noises include salt and pepper noise and Gaussian noise.
To add noise to the image, use the random_noise function of the util module in skimage.
The calling format is:
skimage.util.random_noise(image,mode)
image: input image data;
mode: the category of added noise, which is srt type, and the noise categories include: gaussian (Gaussian additive noise), salt (salt noise), pepper (pepper noise), s&p (salt and pepper noise), poisson (Poisson loose noise), speckle (multiplicative noise), etc.
Example 1: Read in an image, add noise, and display the original image and the image after adding noise. The program code is as follows:
from skimage import util,data,io
import matplotlib.pyplot as plt
img=data.camera()
noise_gs_img=util.random_noise(img,mode='gaussian') #高斯噪声
noise_salt_img=util.random_noise(img,mode='salt') #盐噪声
noise_pepper_img=util.random_noise(img,mode='pepper') #胡椒噪声
noise_sp_img=util.random_noise(img,mode='s&p') #椒盐噪声
noise_speckle_img=util.random_noise(img,mode='speckle') #乘性噪声
plt.figure('noise')
plt.subplot(2,3,1)
io.imshow(img)
plt.title('original')
plt.axis('off')
plt.subplot(2,3,2)
io.imshow(noise_gs_img)
plt.title('gaussian')
plt.axis('off')
plt.subplot(2,3,3)
io.imshow(noise_salt_img)
plt.title('salt')
plt.axis('off')
plt.subplot(2,3,4)
io.imshow(noise_pepper_img)
plt.title('pepper')
plt.axis('off')
plt.subplot(2,3,5)
io.imshow(noise_sp_img)
plt.title('s&p')
plt.axis('off')
plt.subplot(2,3,6)
io.imshow(noise_speckle_img)
plt.title('speckle')
plt.axis('off')
plt.show()
output:
2. Image spatial smoothing
The filtering operation is performed through the filters module in the skimage library.
1. Median filtering
The function used by the median filter is median, and the function call format is:
filters.median(img,disk(3))
The median filter needs to use the disk in skimage.morphology to set the shape of the filter.
Example 2: Read in the image, add salt and pepper noise, use 3 3 and 5 5 windows to perform median filtering on the noisy image, and display the experimental results. The program code is as follows:
from skimage import data,filters,io,util
import matplotlib.pyplot as plt
from skimage.morphology import disk
img=data.camera()
noise_sp_img=util.random_noise(img,mode='s&p')
edges1=filters.median(noise_sp_img,disk(3))
edges2=filters.median(noise_sp_img,disk(5))
plt.figure('median',figsize=(8,8))
plt.subplot(221)
io.imshow(img)
plt.title('original')
plt.axis('off')
plt.subplot(222)
io.imshow(noise_sp_img)
plt.title('salt&peppernoise')
plt.axis('off')
plt.subplot(223)
io.imshow(edges1)
plt.title('3*3')
plt.axis('off')
plt.subplot(224)
io.imshow(edges2)
plt.title('5*5')
plt.axis('off')
plt.show()
output:
2. Gaussian filter
The function used by Gaussian filtering is gaussian, and the function calling format is:
skimage.filters.gaussian(image,sigma)
adjusts the filtering effect by adjusting the value of sigma
Example 3: Read in the image, add Gaussian noise, and perform Gaussian filtering on the noisy image. The sigma is set to 0.4 and 2 respectively, and the experimental results are displayed. The program code is as follows:
from skimage import data,filters,io,util
import matplotlib.pyplot as plt
img=data.camera()
noise_img=util.random_noise(img,mode='gaussian')
edges1=filters.gaussian(noise_img,sigma=0.4) #sigma=0.4
edges2=filters.gaussian(noise_img,sigma=2) #sigma=2
plt.figure('gaussian',figsize=(8,8))
plt.subplot(221)
io.imshow(img)
plt.title('original')
plt.axis('off')
plt.subplot(222)
io.imshow(noise_sp_img)
plt.title('gaussiannoise')
plt.axis('off')
plt.subplot(223)
io.imshow(edges1)
plt.title('sigma=0.4')
plt.axis('off')
plt.subplot(224)
io.imshow(edges2)
plt.title('sigma=2')
plt.axis('off')
plt.show()
output:
Daily "Big Pie":
Any restriction starts from your own heart