当前位置:文档之家› 数字图像的空间域滤波和频域滤波

数字图像的空间域滤波和频域滤波

数字图像的空间域滤波和频域滤波三、实验过程1. 平滑空间滤波:1) 读出一幅图像,给这幅图像分别加入椒盐噪声和高斯噪声后并与前一张图显示在同一图像窗口中。

椒盐噪声:def salt_pepperNoise(src):dst = src.copy()num = 1000 # 1000个噪声点ndim = np.ndim(src)row, col = np.shape(src)[0:2]for i in range(num):x = np.random.randint(0, row) # 随机生成噪声点位置y = np.random.randint(0, col)indicator = np.random.randint(0, 2)# 灰度图像if ndim == 2:if indicator == 0:dst[x, y] = 0else:dst[x, y] = 255# 彩色图像elif ndim == 3:if indicator == 0:dst[x, y, :] = 0else:dst[x, y, :] = 255return dst高斯噪声:def addGaussianNoise(image,sigma):mean = 0.0row, col ,ch= image.shapegauss = np.random.normal(mean, sigma, (row, col,ch))gauss = gauss.reshape(row, col,ch)noisy = image + gaussreturn noisy.astype(np.uint8)2) 对加入噪声图像选用不同的平滑(低通)模板做运算,对比不同模板所形成的效果,要求在同一窗口中显示。

加入椒盐噪声后图像的滤波:img1 =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.medianBlur(src,5) #中值滤波cv2.imshow("medianBlur",dst1)dst2 = cv2.GaussianBlur(src,(3,3),0) #高斯滤波cv2.imshow("GaussianBlur",dst2)cv2.waitKey(0)cv2.destroyAllWindows()3) 进行低通滤波,显示处理后的图像。

import cv2import numpy as npdef function(img):h,w=img.shapenewimg=np.zeros((h,w),np.uint8)img2=np.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: newimg[i, j] = 255tmp = 1else:tmp = 0st[i, j] = tmp * fshift[i, j]sl=np.fft.ifftshift(st)x2=np.fft.ifft2(sl)x3=np.uint8(np.real(x2))return newimg,x3img=cv2.imread('D:\\mote.jpg',0)img1,img2=function(img)cv2.imshow("image",img)cv2.imshow("low pass filtering",img2)cv2.waitKey(0)4) 显示均值处理后的图像。

代码: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()5) 对加入椒盐噪声的图像分别采用均值滤波法,和中值滤波法对有噪声的图像做处理,要求在同一窗口中显示结果。

代码:import cv2import matplotlib.pyplot as pltimg = cv2.imread('D:/img/salt.jpg',0) #直接读为灰度图像blur = cv2.blur(img,(3,5))#模板大小3*5mid =cv2.medianBlur(img,5)plt.subplot(1,2,1),plt.imshow(mid,'gray')plt.title('medianBlur')plt.xticks([]), plt.yticks([])plt.subplot(1,2,2),plt.imshow(blur,'gray')plt.title('blur')plt.xticks([]), plt.yticks([])plt.show()2. 锐化空间滤波1) 读出一幅图像,采用3×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 = np.zeros((row, col), dtype=np.int16)for i in range(row):for j in range(col):temp = addBorder [i:i+3, j:j+3]dst[i, j] = np.sum(template*temp)return dst2) 编写函数w = genlaplacian(n),自动产生任一奇数尺寸n的拉普拉斯算子,如5×5的拉普拉斯算子w = [ 1 1 1 1 11 1 1 1 11 1 -24 1 11 1 1 1 11 1 1 1 1]import numpy as npimport pandas as pdimport cv2from scipy import ndimagekernel_5x5=np.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 = ndimage.convolve(img, kernel_5x5)cv2.imshow("5x5", k5)cv2.waitKey()cv2.destroyAllWindows()3) 分别采用5×5,9×9,15×15和25×25大小的拉普拉斯算子对blurry_moon.tif进行锐化滤波,并利用式完成图像的锐化增强,观察其有何不同,要求在同一窗口中显示。

代码:import cv2img=blurred = cv2.imread("D:\\mote.jpg",0)#5×5,9×9,15×15和25×25blurred1 =cv2.GaussianBlur(img, (5,5), 0)blurred2 =cv2.GaussianBlur(img, (9,9), 0)blurred3 =cv2.GaussianBlur(img, (15,15), 0)blurred4 =cv2.GaussianBlur(img, (25,25), 0)img5x5 = img - blurred1img9x9 = img - blurred2img15x15 = img - blurred3img25x25 = img - blurred4cv2.imshow("5x5",img5x5 )cv2.imshow("9x9",img9x9 )cv2.imshow("15x15",img15x15)cv2.imshow("25x25",img25x25 )cv2.waitKey(0)随着属性值的增大,图像锐化增强4) 采用不同的梯度算子对blurry_moon.tif进行锐化滤波,并比较其效果。

代码:import numpy as npimport cv2src = cv2.imread("D:\\mote.jpg",0)#sobel算法x =cv2.Sobel(src,cv2.CV_16S,1,0)y =cv2.Sobel(src,cv2.CV_16S,0,1)absX =cv2.convertScaleAbs(x)absY =cv2.convertScaleAbs(y)dst =cv2.addWeighted(absX,0.5,absY,0.5,0)cv2.imshow('Sobel',dst)#拉布拉斯算子lap =placian(src,cv2.CV_16S,3)dst1 =cv2.convertScaleAbs(lap)cv2.imshow("Laplacian",dst1)#Canny算子can =cv2.Canny(src,30,120)s= src-cansrc1 =cv2.GaussianBlur(s,(3,3),0)cv2.imshow("can",src1 )cv2.waitKey(0)3. 傅立叶变换1)读出woman.tif这幅图像,对其进行快速傅立叶变换,分别显示其幅度图像和相位图像。

相关主题