Digital Image Processing--Image Filtering

Digital Image Processing – Image Filtering

1 main content

A, select the experimental image used in the experiment, complete the image reading and display, and add noise to the image; B
, write the filter functions of three kinds of image filters;
wherein, the general specific implementation steps of the mean value filter are:
1, select one ( 2n+l) x (2n+l) window (usually 3 x 3 or 5 x 5), and use this window to slide rows or columns along the image data; 2. Read the gray value of each corresponding pixel under the
window ;
3. Calculate the average gray value of these pixels to replace the original pixel gray value at the center of the window.
The general specific implementation steps of Gaussian filtering are:
1. Select a (2n+l) x (2n+l) window (usually 3 x 3 or 5 x 5), generate a two-dimensional Gaussian template, and use this window to move along the image data Slide the row or column;
2. Read the gray value of each corresponding pixel under the window;
3. Find the product of these pixels and the corresponding position elements of the two-dimensional Gaussian template and then sum, and use this value to replace the original value of the center position of the window Pixel grayscale value.
The general specific implementation steps of the median filter are:
1. Select a (2n+l)x(2n+l) window (usually 3x3 or 5x5), and use this window to slide rows or columns along the image data; 2
. Read the gray value of each corresponding pixel under the window;
3. Arrange these gray values ​​in a row from small to large, and replace the original pixel gray value in the center of the window with the sorted median value; C. Use the filter written by
yourself function to filter the experimental image;
D. Use the filter function that comes with Matlab to filter the experimental image;
E. Compare the experimental results and modify and optimize the filter function written by yourself.

2 source code

The code method is slightly clumsy

import cv2
import matplotlib.pyplot as plt
import numpy as np
from skimage import util
from  statistics import median
import scipy.signal as signal

#选取实验用的实验图像,完成图像读取和显示,给图像加上噪声;
rose = cv2.imread('2.png',cv2.IMREAD_GRAYSCALE)
print('rose:',rose)
#添加高斯噪声
rose_Gaussian = util.random_noise(rose,mode = 'Gaussian')
rose_Gaussian = rose_Gaussian*255
print('rose_Gaussion:',rose_Gaussian)

#添加椒盐噪声
rose_sp = util.random_noise(rose,mode = 's&p')
rose_sp = rose_sp*255
print('rose_sp:',rose_sp)

#绘图
#设置画布标题字体为中文,不设置的话,可能会出现乱码结果
plt.rcParams["font.sans-serif"]=["SimHei"]
plt.rcParams["axes.unicode_minus"]=False

plt.subplot(131),plt.imshow(rose,cmap = 'gray'),plt.title('原图')
plt.subplot(132),plt.imshow(rose_Gaussian,cmap = 'gray'),plt.title('高斯噪声')
plt.subplot(133),plt.imshow(rose_sp,cmap = 'gray'),plt.title('椒盐噪声')


#均值滤波
def filter_Average(x):
    x = rose_sp.shape
    #print('rose_sp.shape:',x)   #shape: (675, 1200)
    kernel = np.ones((3,3),dtype= int)
    print('A_kernel:',kernel)
    big_rose = np.zeros((x[0]+2,x[1]+2))
    #print('big_rose:',big_rose)
    #print('big_rose.shape:',big_rose.shape)

    big_rose[1:big_rose.shape[0]-1,1:big_rose.shape[1]-1] = rose_sp
    #print('big_rose:',big_rose)

    for i in range(big_rose.shape[0]-2):
        for j in range(big_rose.shape[1]-2):
            sum = big_rose[i+1][j+1]+big_rose[i+1+1][j+1]+\
                                 big_rose[i+1-1][j+1]+big_rose[i+1][j+1+1]+\
                                 big_rose[i+1][j+1-1]+big_rose[i+1-1][j+1-1]+\
                                 big_rose[i+1+1][j+1+1]+big_rose[i+1-1][j+1+1]+big_rose[i+1+1][j+1-1]
            big_rose[i+1][j+1] = sum/9
            sum = 0

    #print('After_big_rose:',big_rose)
    After_A_rose_sp = big_rose[1:big_rose.shape[0]-1,1:big_rose.shape[1]-1]
    print('After_A_rose_sp:', After_A_rose_sp)
    return After_A_rose_sp

#中值滤波
def filter_Mid(x):
    #x = rose_sp
    x = rose_sp.shape
    #print('rose_sp.shape:', x)  # shape: (675, 1200)
    kernel = np.ones((3, 3), dtype=int)
    print('M_kernel:', kernel)
    big_rose = np.zeros((x[0] + 2, x[1] + 2))
    #print('big_rose:', big_rose)
    #print('big_rose.shape:', big_rose.shape)

    big_rose[1:big_rose.shape[0] - 1, 1:big_rose.shape[1] - 1] = rose_sp
    #print('big_rose:', big_rose)

    for i in range(big_rose.shape[0] - 2):
        for j in range(big_rose.shape[1] - 2):
            M_list = [big_rose[i + 1][j + 1] , big_rose[i + 1 + 1][j + 1] ,
                  big_rose[i + 1 - 1][j + 1] , big_rose[i + 1][j + 1 + 1] ,
                  big_rose[i + 1][j + 1 - 1] , big_rose[i + 1 - 1][j + 1 - 1] ,
                  big_rose[i + 1 + 1][j + 1 + 1] , big_rose[i + 1 - 1][j + 1 + 1] ,
                  big_rose[i + 1 + 1][j + 1 - 1]]
            mid = median(M_list)
            big_rose[i+1][j+1] = mid

    #print('After_big_rose:', big_rose)
    After_M_rose_sp = big_rose[1:big_rose.shape[0] - 1, 1:big_rose.shape[1] - 1]
    print('After_M_rose_sp:', After_M_rose_sp)
    return After_M_rose_sp

#高斯滤波:加权平均
def filter_Gauusian(x):
    x = rose_sp.shape

    big_rose = np.zeros((x[0] + 2, x[1] + 2))
    #print('big_rose:', big_rose)
    #print('big_rose.shape:', big_rose.shape)

    big_rose[1:big_rose.shape[0] - 1, 1:big_rose.shape[1] - 1] = rose_sp
    #print('big_rose:', big_rose)

    kernel = [[1, 2, 1], [2, 4, 2], [1, 2, 1]]
    new_list = []
    for i in range(len(kernel)):
        for j in range(len(kernel)):
            x = kernel[i][j] / 16
            new_list.append(x)

    new_list = np.matrix(new_list)
    kernel = new_list.reshape(3, 3)
    print('G_kernel:', kernel)

    for i in range(big_rose.shape[0] - 2):
        for j in range(big_rose.shape[1] - 2):
            sum = big_rose[i + 1][j + 1]*kernel[1,1] + big_rose[i + 1 + 1][j + 1]*kernel[2,1]+ \
                  big_rose[i + 1 - 1][j + 1]*kernel[0,1] + big_rose[i + 1][j + 1 + 1]*kernel[1,2] + \
                  big_rose[i + 1][j + 1 - 1] *kernel[1,0]+ big_rose[i + 1 - 1][j + 1 - 1]*kernel[0,0] + \
                  big_rose[i + 1 + 1][j + 1 + 1]*kernel[2,2] + big_rose[i + 1 - 1][j + 1 + 1]*kernel[0,2] + \
                  big_rose[i + 1 + 1][j + 1 - 1]*kernel[2,0]
            big_rose[i + 1][j + 1] = sum
            sum = 0

    #print('After_big_rose:', big_rose)
    After_rose_sp = big_rose[1:big_rose.shape[0] - 1, 1:big_rose.shape[1] - 1]
    print('After_G_rose_sp:', After_rose_sp)
    return After_rose_sp

#自定义滤波器
def filter_Define(x):
    x = rose_sp.shape

    big_rose = np.zeros((x[0] + 2, x[1] + 2))
    #print('big_rose:', big_rose)
    #print('big_rose.shape:', big_rose.shape)

    big_rose[1:big_rose.shape[0] - 1, 1:big_rose.shape[1] - 1] = rose_sp
    #print('big_rose:', big_rose)

    kernel = [[1, 0, 1], [0, 6, 0], [1, 0, 1]]
    new_list = []
    for i in range(len(kernel)):
        for j in range(len(kernel)):
            x = kernel[i][j] / 10
            new_list.append(x)

    new_list = np.matrix(new_list)
    kernel = new_list.reshape(3, 3)
    print('D_kernel:', kernel)

    for i in range(big_rose.shape[0] - 2):
        for j in range(big_rose.shape[1] - 2):
            sum = big_rose[i + 1][j + 1]*kernel[1,1] + big_rose[i + 1 + 1][j + 1]*kernel[2,1]+ \
                  big_rose[i + 1 - 1][j + 1]*kernel[0,1] + big_rose[i + 1][j + 1 + 1]*kernel[1,2] + \
                  big_rose[i + 1][j + 1 - 1] *kernel[1,0]+ big_rose[i + 1 - 1][j + 1 - 1]*kernel[0,0] + \
                  big_rose[i + 1 + 1][j + 1 + 1]*kernel[2,2] + big_rose[i + 1 - 1][j + 1 + 1]*kernel[0,2] + \
                  big_rose[i + 1 + 1][j + 1 - 1]*kernel[2,0]
            big_rose[i + 1][j + 1] = sum
            sum = 0

    #print('After_big_rose:', big_rose)
    After_D_rose_sp = big_rose[1:big_rose.shape[0] - 1, 1:big_rose.shape[1] - 1]
    print('After_D_rose_sp:', After_D_rose_sp)
    return After_D_rose_sp

#优化自定义滤波器
def filter_Optimer_Define(x):
    x = rose_sp.shape

    big_rose = np.zeros((x[0] + 2, x[1] + 2))
    #print('big_rose:', big_rose)
    #print('big_rose.shape:', big_rose.shape)

    big_rose[1:big_rose.shape[0] - 1, 1:big_rose.shape[1] - 1] = rose_sp
    #print('big_rose:', big_rose)

    kernel = [[0, 0, 0], [2, 6, 2], [0, 0, 0]]
    new_list = []
    for i in range(len(kernel)):
        for j in range(len(kernel)):
            x = kernel[i][j] / 10
            new_list.append(x)

    new_list = np.matrix(new_list)
    kernel = new_list.reshape(3, 3)
    print('O_kernel:', kernel)

    for i in range(big_rose.shape[0] - 2):
        for j in range(big_rose.shape[1] - 2):
            sum = big_rose[i + 1][j + 1]*kernel[1,1] + big_rose[i + 1 + 1][j + 1]*kernel[2,1]+ \
                  big_rose[i + 1 - 1][j + 1]*kernel[0,1] + big_rose[i + 1][j + 1 + 1]*kernel[1,2] + \
                  big_rose[i + 1][j + 1 - 1] *kernel[1,0]+ big_rose[i + 1 - 1][j + 1 - 1]*kernel[0,0] + \
                  big_rose[i + 1 + 1][j + 1 + 1]*kernel[2,2] + big_rose[i + 1 - 1][j + 1 + 1]*kernel[0,2] + \
                  big_rose[i + 1 + 1][j + 1 - 1]*kernel[2,0]
            big_rose[i + 1][j + 1] = sum
            sum = 0

    #print('After_big_rose:', big_rose)
    After_O_rose_sp = big_rose[1:big_rose.shape[0] - 1, 1:big_rose.shape[1] - 1]
    print('After_O_rose_sp:', After_O_rose_sp)
    return After_O_rose_sp


print('=======均值滤波器=========')
After_A_rose_sp = filter_Average(rose_sp)
print('=======中值滤波器=========')
After_M_rose_sp = filter_Mid(rose_sp)
print('=======高斯滤波器=========')
After_G_rose_sp = filter_Gauusian(rose_sp)
print('=======自定义滤波器========')
After_D_rose_sp = filter_Define(rose_sp)
print('=======库自带高斯滤波器=====')
After_K_Gauss_rose_sp = cv2.GaussianBlur(rose_sp, (3, 3), 0)
print('After_K_Gauss_rose_sp:',After_K_Gauss_rose_sp)
print('=======库自带中值滤波器=====')
After_K_Average_rose_sp = signal.medfilt(rose_sp,(3,3))
print('After_K_Average_rose_sp',After_K_Average_rose_sp)
print('=======优化自定义滤波器======')
After_Optimer_rose_sp = filter_Optimer_Define(rose_sp)

# 用Matlab自带的滤波函数对实验图像分别进行滤波;

plt.figure(2)

plt.subplot(241),plt.imshow(rose_sp,cmap = 'gray'),plt.title('椒盐噪声图')
plt.subplot(242),plt.imshow(After_A_rose_sp,cmap = 'gray'),plt.title('均值滤波图')
plt.subplot(243),plt.imshow(After_M_rose_sp,cmap = 'gray'),plt.title('中值滤波图')
plt.subplot(244),plt.imshow(After_G_rose_sp,cmap = 'gray'),plt.title('高斯滤波图')
plt.subplot(245),plt.imshow(After_D_rose_sp,cmap = 'gray'),plt.title('自定义滤波图')
plt.subplot(246),plt.imshow(After_K_Gauss_rose_sp,cmap = 'gray'),plt.title('库自带高斯滤波图')
plt.subplot(247),plt.imshow(After_K_Average_rose_sp,cmap = 'gray'),plt.title('库自带中值滤波图')
plt.subplot(248),plt.imshow(After_Optimer_rose_sp,cmap = 'gray'),plt.title('优化自定义滤波图')
plt.show()

3 Achieving results

3.1 Selection In this experiment, the selected experimental image is a 675*1200 grayscale image, and Gaussian noise and salt and pepper noise are added to the image, image reading and display, and display after adding noise. As shown below.
insert image description here

2.2 Explanation: For the processing of image edge pixels, this experiment adopts filling 0 value, and then adjusts each pixel, and the experiment is carried out on the image with salt and pepper noise added, including the filter described below.
Mean filtering: In this experiment, a (3*3) window is selected, and the average value of the window is 1, and the window is used to traverse along the image data. The filter and the processed image pixel values ​​are shown in the figure below.
insert image description here

Gaussian filtering: In the experiment, a (3*3) window was selected to generate a two-dimensional Gaussian template, and the window was used to slide rows or columns along the image data; the filter and the processed image pixel values ​​are shown in the figure below.
insert image description here

Median filtering: select a (3*3) window, and use the window to slide rows or columns along the image data; the filter and the processed image pixel values ​​are shown in the figure below.
insert image description here

2.3 I designed a filter with a window of (3*3), and completed the filtering of the experimental image; the filter and the pixel value of the processed image are shown in the figure below.
insert image description here

2.4 This experiment uses the median filter function and Gaussian filter function that come with python to filter the experimental image respectively; the pixel value of the image processed by the library function is shown in the figure below.
The library comes with a Gaussian filter result map insert image description here
The library comes with a median filter result map
insert image description here

2.5 Comparing the experimental results, it can be seen that I did not consider the pixels directly adjacent to the pixels, so I adjusted the filter function and performed an appropriate amount of optimization. Its optimized filter and processed image pixel values ​​are shown in the figure below.
insert image description here
2.6 In order to better display the experimental results, all filtered images are displayed in the same window. As shown below.
insert image description here
Xiaobai records! ! !

Guess you like

Origin blog.csdn.net/MZYYZT/article/details/128088676