欢迎访问宙启技术站
智能推送

Python中使用skimage.util库生成随机低通滤波图像的方法

发布时间:2023-12-11 12:47:44

在Python中,可以使用skimage.util库中的random_noise函数来生成随机低通滤波图像。该函数可以用于在图像中添加随机噪声,包括高斯噪声、椒盐噪声等。通过调整参数,可以生成具有不同类型和强度的噪声图像。

下面是使用skimage.util库生成随机低通滤波图像的方法:

1. 导入所需的库和模块

from skimage import data
from skimage.util import random_noise
import matplotlib.pyplot as plt

2. 加载原始图像

image = data.camera()  # 使用skimage自带的相机图像作为例子

3. 添加随机噪声

noisy_image = random_noise(image, mode='gaussian', var=0.01)  # 添加高斯噪声

该函数的 个参数是原始图像,第二个参数指定噪声的类型,可以是'gaussian'、'salt'、'pepper'等,第三个参数是噪声的方差或概率。

4. 显示原始图像和添加噪声后的图像

fig, axes = plt.subplots(1, 2, figsize=(8, 4))
ax = axes.ravel()

ax[0].imshow(image, cmap=plt.cm.gray)
ax[0].axis('off')
ax[0].set_title('Original image')

ax[1].imshow(noisy_image, cmap=plt.cm.gray)
ax[1].axis('off')
ax[1].set_title('Noisy image')

plt.tight_layout()
plt.show()

通过上述代码,我们可以生成带有随机低通滤波噪声的图像,并可视化显示原始图像和添加噪声后的图像。

以下是一个完整的使用skimage.util库生成随机低通滤波图像的例子:

from skimage import data
from skimage.util import random_noise
import matplotlib.pyplot as plt

image = data.camera()

noisy_image = random_noise(image, mode='gaussian', var=0.01)

fig, axes = plt.subplots(1, 2, figsize=(8, 4))
ax = axes.ravel()

ax[0].imshow(image, cmap=plt.cm.gray)
ax[0].axis('off')
ax[0].set_title('Original image')

ax[1].imshow(noisy_image, cmap=plt.cm.gray)
ax[1].axis('off')
ax[1].set_title('Noisy image')

plt.tight_layout()
plt.show()

运行上述代码,将会生成原始图像和添加了高斯噪声的图像,并显示在两个子图中。 个子图显示原始图像,第二个子图显示添加噪声后的图像。

通过使用skimage.util库的random_noise函数,我们可以方便地生成具有不同类型和强度的随机低通滤波图像,并对其进行进一步处理和分析。