数字图像处理--图像滤波

数字图像处理–图像滤波

1 主要内容

A、选取实验用的实验图像,完成图像读取和显示,给图像加上噪声;
B、编写三种图像滤波器的滤波函数;
其中,均值滤波一般的具体实现步骤是:
1、选择一个(2n+l) x (2n+l)的窗口(通常为3 x 3或5 x 5),并用该窗口沿图像数据进行行或列的滑动;
2、读取窗口下各对应像素的灰度值;
3、求取这些像素的灰度平均值替代窗口中心位置的原始像素灰度值。
高斯滤波一般的具体实现步骤是:
1、选择一个(2n+l) x (2n+l)的窗口(通常为3 x 3或5 x 5),生成二维高斯模板,并用该窗口沿图像数据进行行或列的滑动;
2、读取窗口下各对应像素的灰度值;
3、求取这些像素与二维高斯模板对应位置元素的乘积再求和,用该值替代窗口中心位置的原始像素灰度值。
中值滤波一般的具体实现步骤是:
1、选择一个(2n+l)x(2n+l)的窗口(通常为3x3或5x5),并用该窗口沿图像数据进行行或列的滑动;
2、读取窗口下各对应像素的灰度值;
3、将这些灰度值从小到大排成一列,用排序所得的中值替代窗口中心位置的原始像素灰度值;
C、用自己编写的滤波函数分别对实验图像进行滤波;
D、用Matlab自带的滤波函数对实验图像分别进行滤波;
E、对比实验结果,对自己编写的滤波函数进行修改和优化。

2 源代码

代码手法略笨拙

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 实现结果

3.1 选本实验中,选取的实验图像是一个675*1200的灰度图像,并对该图像分别添加了高斯噪声和椒盐噪声,图像的读取和显示,以及添加过噪声之后的显示。如下图所示。
在这里插入图片描述

2.2 说明:对于图像边缘像素点的处理,本实验采取了填充0值,进而对每个像素点进行调整,并且均是在添加了椒盐噪声的图像上进行了实验,包括以下所述滤波器。
均值滤波:本实验中选择一个(3*3)的窗口,其窗口值均取值为1,并用该窗口沿图像数据进行遍历。其滤波器以及经过处理后的图片像素值如下图所示。
在这里插入图片描述

高斯滤波:实验中选择了(3*3)的窗口,生成二维高斯模板,并用该窗口沿图像数据进行行或列的滑动;其滤波器以及经过处理后的图片像素值如下图所示。
在这里插入图片描述

中值滤波:选择一个(3*3)的窗口,并用该窗口沿图像数据进行行或列的滑动;其滤波器以及经过处理后的图片像素值,如下图所示。
在这里插入图片描述

2.3 自己设计了一个窗口为(3*3)的滤波器,并完成了对实验图像进行滤波;其滤波器以及经过处理后的图片像素值,如下图所示。
在这里插入图片描述

2.4 本实验使用了python自带的中值滤波函数和高斯滤波函数对实验图像分别进行滤波;经过库函数处理后的图片像素值,如下图所示。
库自带高斯滤波结果图在这里插入图片描述
库自带中值滤波结果图
在这里插入图片描述

2.5 对比实验结果,可知自己没有考虑到与像素点直接相邻的像素点,于是调整了滤波函数,进行适量的优化。其优化后的滤波器以及经过处理后的图片像素值,如下图所示。
在这里插入图片描述
2.6 为了更好的显示实验结果,将所有滤波后图像放在了同一个窗口进行显示。如下图所示。
在这里插入图片描述
小白记录!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值