Digital Image Processing--Image Filtering

Digital Image Processing – Image Filtering

1 main content

A. Select the experimental image for the experiment, complete image reading and display, and add noise to the image;
B. Write the filter functions of three image filters;
Among them, the general specific implementation steps of mean filtering are:
1. Select a (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 slide rows or columns along the image data;
2. Read the gray value of each corresponding pixel under the window;
3. Calculate 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 pixel gray value at the center of the window.
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 column from small to large, and replace the original pixel gray value at the center of the window with the sorted median;
C. Use the filter function written by yourself to filter the experimental image respectively;
D, filter the experimental image with the filter function that comes with Matlab;
E. Comparing the experimental results, 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

#Select the experimental image for the experiment, complete image reading and display, and add noise to the image;
rose = cv2.imread('2.png',cv2.IMREAD_GRAYSCALE)
print('rose:',rose)
#Add Gaussian noise
rose_Gaussian = util.random_noise(rose,mode = 'Gaussian')
rose_Gaussian = rose_Gaussian*255
print('rose_Gaussion:',rose_Gaussian)

#Add salt and pepper noise
rose_sp = util.random_noise(rose,mode = 's&p')
rose_sp = rose_sp*255
print('rose_sp:',rose_sp)

#drawing
#Set the canvas title font to Chinese, if not set, there may be garbled results
plt.rcParams["font.sans-serif"]=["SimHei"]
plt.rcParams["axes.unicode_minus"]=False

plt.subplot(131),plt.imshow(rose,cmap = 'gray'),plt.title('original image')
plt.subplot(132),plt.imshow(rose_Gaussian,cmap = 'gray'),plt.title('Gaussian noise')
plt.subplot(133),plt.imshow(rose_sp,cmap = 'gray'),plt.title('salt and pepper noise')


#mean filtering
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

#median filter
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

#Gaussian filtering: weighted average
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

#custom filter
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

#Optimize custom filters
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('=======mean filter=========')
After_A_rose_sp = filter_Average(rose_sp)
print('=======median filter=========')
After_M_rose_sp = filter_Mid(rose_sp)
print('=======Gaussian filter=========')
After_G_rose_sp = filter_Gauusian(rose_sp)
print('=======custom filter========')
After_D_rose_sp = filter_Define(rose_sp)
print('=======The library comes with a Gaussian filter=====')
After_K_Gauss_rose_sp = cv2.GaussianBlur(rose_sp, (3, 3), 0)
print('After_K_Gauss_rose_sp:',After_K_Gauss_rose_sp)
print('=======The library comes with a median filter=====')
After_K_Average_rose_sp = signal.medfilt(rose_sp,(3,3))
print('After_K_Average_rose_sp',After_K_Average_rose_sp)
print('=======Optimize custom filters======')
After_Optimer_rose_sp = filter_Optimer_Define(rose_sp)

# Use the filter function that comes with Matlab to filter the experimental images respectively;

plt.figure(2)

plt.subplot(241),plt.imshow(rose_sp,cmap = 'gray'),plt.title('Salt and Pepper Noise Map')
plt.subplot(242),plt.imshow(After_A_rose_sp,cmap = 'gray'),plt.title('mean filter map')
plt.subplot(243),plt.imshow(After_M_rose_sp,cmap = 'gray'),plt.title('median filter map')
plt.subplot(244),plt.imshow(After_G_rose_sp,cmap = 'gray'),plt.title('Gaussian filter map')
plt.subplot(245),plt.imshow(After_D_rose_sp,cmap = 'gray'),plt.title('Custom filter map')
plt.subplot(246),plt.imshow(After_K_Gauss_rose_sp,cmap = 'gray'),plt.title('The library comes with a Gaussian filter map')
plt.subplot(247),plt.imshow(After_K_Average_rose_sp,cmap = 'gray'),plt.title('The library comes with a median filter map')
plt.subplot(248),plt.imshow(After_Optimer_rose_sp,cmap = 'gray'),plt.title('Optimize custom filter map')
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.

2.2 Explanation: For the processing of image edge pixels, this experiment adopts the filling of 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.

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.

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.

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.

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 after processing by the library function is shown in the figure below.
The library comes with a Gaussian filter result map
The library comes with a median filter result map

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 the processed image pixel values ​​are shown in the figure below.

2.6 In order to better display the experimental results, all filtered images are displayed in the same window. As shown below.

Xiaobai records! ! !

Tags: Python OpenCV Computer Vision image processing

Posted by jd57 on Tue, 29 Nov 2022 05:01:34 +0300