数字图像处理实验(图像空域滤波)

调用的时候记得将路径改一下,我这里是我自己保存的路径

第一个实验:给图像进行高斯噪声处理

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()
  • 19
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值