调用的时候记得将路径改一下,我这里是我自己保存的路径
第一个实验:给图像进行高斯噪声处理
import cv2
import numpy as np
img_path = r"C:\MinGW\2e0e0c0a1edf2ce8f64a153085b9886d.jpg"//自己图片的路径
#读取图片
img = cv2.imread(img_path)
img_height, img_width, img_channels = img.shape
#设置高斯分布的均值和方差
mean = 0
#设置高斯分布的标准差
sigma = 300
#根据均值和标准差生成符合高斯分布的噪声
gauss = np.random.normal(mean,sigma,(img_height,img_width,img_channels))
#给图片添加高斯噪声
noisy_img = img + gauss
#设置图片添加高斯噪声之后的像素值的范围
noisy_img = np.clip(noisy_img,a_min=0,a_max=255)
#保存图片
cv2.imwrite("noisy_img.png",noisy_img )
cv2.imshow("noisy_img.png", noisy_img )
cv2.waitKey(0)
第二个实验:给图像进行椒盐噪声处理
import cv2
import numpy as np
img_path = r"C:\MinGW\2e0e0c0a1edf2ce8f64a153085b9886d.jpg"
#读取图片
img = cv2.imread(img_path)
#设置添加椒盐噪声的数目比例
s_vs_p = 0.5
#设置添加噪声图像像素的数目
amount = 0.04
noisy_img = np.copy(img)
#添加salt噪声
num_salt = np.ceil(amount * img.size * s_vs_p)
#设置添加噪声的坐标位置
coords = [np.random.randint(0,i - 1, int(num_salt)) for i in img.shape]
noisy_img[coords[0],coords[1],:] = [255,255,255]
#添加pepper噪声
num_pepper = np.ceil(amount * img.size * (1. - s_vs_p))
#设置添加噪声的坐标位置
coords = [np.random.randint(0,i - 1, int(num_pepper)) for i in img.shape]
noisy_img[coords[0],coords[1],:] = [0,0,0]
#保存图片
cv2.imwrite("noisy_img.png",noisy_img)
cv2.imshow("noisy_img.png", noisy_img )
cv2.waitKey(0)
第三个实验:设计3×3、5×5,7×7三种模板的算术均值滤波器对图像进行滤波,同屏显示加噪后图像和消噪图像
import cv2
import numpy as np
img_path = r"C:\MinGW\2e0e0c0a1edf2ce8f64a153085b9886d.jpg"
img = cv2.imread(img_path)
img_height, img_width, img_channels = img.shape
# 设置高斯分布的均值和方差
mean = 0
sigma = 30
# 根据均值和标准差生成符合高斯分布的噪声
gauss = np.random.normal(mean, sigma, (img_height, img_width, img_channels))
# 给图片添加高斯噪声
noisy_img = img + gauss
# 设置图片添加高斯噪声之后的像素值的范围
noisy_img = np.clip(noisy_img, a_min=0, a_max=255).astype(np.uint8)
# 保存图片
cv2.imwrite("noisy_img.png", noisy_img)
# 读取加噪的图像
noisy_img = cv2.imread("noisy_img.png")
# 创建3×3、5×5和7×7的算术均值滤波器模板
kernel_3x3 = np.ones((3, 3), np.float32) / 9
kernel_5x5 = np.ones((5, 5), np.float32) / 25
kernel_7x7 = np.ones((7, 7), np.float32) / 49
# 对加噪图像应用滤波器
smoothed_3x3 = cv2.filter2D(noisy_img, -1, kernel_3x3)
smoothed_5x5 = cv2.filter2D(noisy_img, -1, kernel_5x5)
smoothed_7x7 = cv2.filter2D(noisy_img, -1, kernel_7x7)
# 显示加噪图像和消噪图像
cv2.imshow("Noisy Image", noisy_img)
cv2.imshow("Smoothed 3x3", smoothed_3x3)
cv2.imshow("Smoothed 5x5", smoothed_5x5)
cv2.imshow("Smoothed 7x7", smoothed_7x7)
cv2.waitKey(0)
cv2.destroyAllWindows()
第四个实验:设计3×3、5×5,7×7三种模板的中值滤波器对图像进行滤波,同屏显示加噪后图像和消噪图像
import cv2
import numpy as np
img_path = r"C:\MinGW\2e0e0c0a1edf2ce8f64a153085b9886d.jpg"
#读取图片
img = cv2.imread(img_path)
#设置添加椒盐噪声的数目比例
s_vs_p = 0.5
#设置添加噪声图像像素的数目
amount = 0.04
noisy_img = np.copy(img)
#添加salt噪声
num_salt = np.ceil(amount * img.size * s_vs_p)
#设置添加噪声的坐标位置
coords = [np.random.randint(0,i - 1, int(num_salt)) for i in img.shape]
noisy_img[coords[0],coords[1],:] = [255,255,255]
#添加pepper噪声
num_pepper = np.ceil(amount * img.size * (1. - s_vs_p))
#设置添加噪声的坐标位置
coords = [np.random.randint(0,i - 1, int(num_pepper)) for i in img.shape]
noisy_img[coords[0],coords[1],:] = [0,0,0]
#保存图片
cv2.imwrite("noisy_img.png",noisy_img)#
# 读取加噪的图像
noisy_img = cv2.imread("noisy_img.png")
# 应用3×3、5×5和7×7中值滤波器
median_3x3 = cv2.medianBlur(noisy_img, 3)
median_5x5 = cv2.medianBlur(noisy_img, 5)
median_7x7 = cv2.medianBlur(noisy_img, 7)
# 显示加噪图像和消噪图像
cv2.imshow("Noisy Image", noisy_img)
cv2.imshow("Median 3x3", median_3x3)
cv2.imshow("Median 5x5", median_5x5)
cv2.imshow("Median 7x7", median_7x7)
cv2.waitKey(0)
cv2.destroyAllWindows()