
数字图像的空间域滤波和频域滤波.doc
13页数字图像的空间域滤波和频域滤波一、实验环境Pycharm2018.2 Anaconda3-5.3二、实验内容与要求1. 平滑空间滤波:1) 读出一幅图像,给这幅图像分别加入椒盐噪声和高斯噪声后并与前一张图 显示在同一图像窗口中2) 对加入噪声图像选用不同的平滑 (低通)模板做运算,对比不同模板所形成的效果,要求在同一窗口中显示3) 进行低通滤波,显示处理后的图像4) 显示均值处理后的图像5) 对加入椒盐噪声的图像分别采用均值滤波法,和中值滤波法对有噪声的图像做处 理,要求在同一窗口中显示结果2. 锐化空间滤波1) 读出blurry_moon.tif这幅图像,采用3X3的拉普拉斯算子 w = [ 1, 1, 1; 1 -8 1; 1, 1,1]对其进行滤波2) 编写函数w = genlaplacian(n),自动产生任一奇数尺寸 n的拉普拉斯算子,如 5X 5的拉普拉斯算子w =[111111111111-2411111113)分别采用11111]5 X 5,9X 9,15X15和25 X 25大小的拉普拉斯算子对 blurry_moon.tif 进z f (x, y)彳行锐化滤波,并利用式g(x, y)=f (x, y)完成图像的锐化增强,观察其有何不同,要求在同一窗口中显示。
4) 采用不同的梯度算子对 blurry_moon.tif进行锐化滤波,并比较其效果3. 傅立叶变换1) 读出woman.tif这幅图像,对其进行快速傅立叶变换,分别显示其幅度图像和相位 图像仅对相位部分进行傅立叶反变换后查看结果图像2) 仅对幅度部分进行傅立叶反变换后查看结果图像3) 将图像的傅立叶变换 F置为其共轭后进行反变换,比较新生成图像与原始图像的差 异三、实验过程1.平滑空间滤波:1)读出一幅图像,给这幅图像分别加入椒盐噪声和高斯噪声后并与前一张图显示在同一图 像窗口中椒盐噪声:def salt_pepperNoise(src):dst = src.copy()num = 1000 # 1000 个噪声点n dim = n p. ndim(src)row, col = np.shape(src)[ 0: 2]#随机生成噪声点位置0, 2)for i in range (num):x = n p.ra ndom.ra ndint( 0, row)y = n p.ra ndom.ra ndint( 0, col)in dicator = n p.ra ndom.ra ndint(# 灰度图像if n dim == 2:if in dicator == 0:dst[x, y] = 0else :dst[x, y] = 255#彩色图像elif n dim == 3:if in dicator == 0:dst[x, y, :] = 0else :dst[x, y, :] = 255return dst高斯噪声:def addGaussianNoise(image,sigma): mea n = 0.0row, col ,ch= image.shapegauss = n p.ra ndom. normal(mea n, sigma, (row, col,ch)) gauss = gauss.reshape(row, col,ch)no isy = image + gaussretur n no isy.astype (np.uin t8)• sattPepper — □ X ■ <- ie .i.m2)对加入噪声图像选用不同的平滑(低通)模板做运算,对比不同模板所形成的效果, 要求在同一窗口中显示。
加入椒盐噪声后图像的滤波:imgl =cv2.imread( "D: mote.jpg" , 0) img=img1[ 100: 300]src =salt_pepperNoise(img)cv2.imshow( "origin" ,src)dst = cv2.blur(src,( 3, 3)) #均值滤波模板cv2.imshow( "blur" ,dst)dst1 = cv2.media nBlur(src, 5) # 中值滤波cv2.imshow( "medianBlur" ,dst1)dst2 = cv2.GaussianBlur(src,( 3, 3), 0) #高斯滤波 cv2.imshow( "GaussianBlur" ,dst2)cv2.waitKey( 0)cv2.destroyAIIWi ndows()X3) 进行低通滤波,显示处理后的图像import cv2import numpy as npdef function(img):h,w=img.shapen ewimg=n p.zeros((h,w), np.uin t8)img2=n p.fft.fft2(img)fshift = np.fft.fftshift(img2)st=fshift.copy()h,w=fshift.shapesh=h/ 2sw=w/2r= 40for i in range (h):for j in range (w):if ((sh - i) * (sh - i) + (sw - j) * (sw - j)) <= r * r: n ewimg[i, j] = 255tmp = 1else :tmp = 0st[i, j] = tmp * fshift[i, j]sl=np.fft.ifftshift(st)x2=np.fft.ifft2(sl)x3=n p.ui nt8( np.real(x2))return n ewimg,x3img=cv2.imread( 'D: mote.jpg' , 0)img1,img2=f un cti on (img)cv2.imshow( "image" ,img)cv2.imshow( "low pass filtering" ,img2)cv2.waitKey( 0)□ a ■ low pass filtering — □ XL Wll.w4) 显示均值处理后的图像。
代码:import cv2import matplotlib.pyplot as pltimg = cv2.imread( 'D: mote.jpg' , 0) #直接读为灰度图像blur = cv2.blur(img,( 3, 5)) #模板大小 3*5plt.subplot( 1, 2, 1),plt.imshow(img, 'gray' )#默认彩色,另一种彩色 bgr plt.title( 'img')plt.xticks([]), plt.yticks⑪plt.subplot( 1,2,2),plt.imshow(blur, 'gray')plt.title( 'blur') plt.xticks([]), plt.yticks([]) plt.show()& Figure 1 — □ X5) 对加入椒盐噪声的图像分别采用均值滤波法,和中值滤波法对有噪声的图像做处理,要求在同一窗口中显示结果代码:import cv2import matplotlib.pyplot as pltimg = cv2.imread( 'D:/img/salt.jpg' , 0) #直接读为灰度图像blur = cv2.blur(img,( 3,5)) #模板大小 3*5mid =cv2.media nBlur(img, 5)plt.subplot( 1, 2,1),plt.imshow(mid, 'gray')plt.title( 'media nBlur')plt.xticks([]), plt.yticks([])plt.subplot( 1, 2,2),plt.imshow(blur, 'gray')plt.title( 'blur')plt.xticks([]), plt.yticks([])plt.show()基 Figure 1 — □ XmedianBlur blur2.锐化空间滤波1) 读出一幅图像,采用 3X 3的拉普拉斯算子 w = [ 1, 1, 1; 1 - 8 1; 1, 1,1]对其进行滤波。
定义函数,实现拉普拉斯算子def Laplace(src):template = np.ones(( 3, 3), dtype =np.float32) # 模板template] 1, 1] = - 8.0addBorder = cv2.copyMakeBorder(src, 1, 1, 1, 1, cv2.BORDER_REFLECT_101) row, col = src.shapedst = n p.zeros((row, col), dtype =n p.i nt16)for i in range (row):for j in range (col):temp = addBorder [i:i+ 3, j:j+ 3]dst[i, j] = n p.sum(template*temp)return dst2) 编与函数w - genlaplacian(n),自动产生任一奇数尺寸 n的拉普拉斯算子,如 5X 5的拉普拉斯算子w - [ 111111111111 -241 11111111111]import numpy as npimport pan das as pdimport cv2from scipy import ndimageker nel_5x5-n p.array([[1, 1, 1, 1,1],[1,1,1, 1, 1],[1,1,- 24, 1,1],[1,1,1, 1, 1,],[1,1,1, 1, 1]])img - cv2.imread( "D:mote.jpg" , 0)k5 = n dimage.c on volve(img, kern el_5x5) cv2.imshow( "5x5" , k5)cv2.waitKey() cv2.destroyAIIWi ndows()3) 分别采用5X 5, 9X 9, 15X 15和25X 25大小的拉普拉斯算子对 blurry_moon.tif进行锐化滤波,并利用式 完成图像的锐化增强,观察其有何不同,要求在同一窗口中显示。
代码:import cv2img=blurred = cv2.imread( "D: mote.jpg" , 0)#5X 5, 9X 9, 15X 15禾口 25X25blurred1 =cv2.Gaussia nBlur(img,( blurred2 =cv2.Gaussia nBlur(img,( blurred3 =cv2.Gaussia nBlur(img,( blurred4 =c。
