天天看點

圖像預處理01—高斯濾波和中值濾波

使用jupyter notebook

import cv2
import numpy as np
import matplotlib.pyplot as plt

#輸入需要處理的圖像
img=cv2.imread("E:/199.bmp")
#添加椒鹽噪聲
for i in range(2000):
    #在輸入的圖像中随機選取2000個坐标點
    temp_x=np.random.randint(0,img.shape[0])
    temp_y=np.random.randint(0,img.shape[1])
    #指定這些坐标點的灰階值為255 即白色
    img[temp_x][temp_y]=255

#高斯濾波 卷積核為5*5 标準差為0
blur_1=cv2.GaussianBlur(img,(5,5),0)
#中值濾波 卷積核為奇數
blur_2=cv2.medianBlur(img,5)

#顯示圖像
plt.subplot(1,3,1)
plt.imshow(img)
plt.subplot(1,3,2)
plt.imshow(blur_1)
plt.subplot(1,3,3)
plt.imshow(blur_2)
plt.show()