Python Pillow - 降噪



降噪,也称为去噪,是指减少图像中不需要的伪影的过程。图像中的噪点通常表现为亮度或颜色上的随机变化,这些变化并非原始场景或拍摄对象的一部分。图像去噪的目标是通过消除或减少这些不需要的、分散注意力的伪影来提高图像质量,使图像更清晰、更美观。

Python Pillow 库提供了一系列降噪滤镜,允许用户去除噪点图像中的噪点并恢复原始图像。在本教程中,我们将探索 GaussianBlur 和 Median 滤镜作为有效的降噪方法。

使用 GaussianBlur 滤镜去除噪点

使用高斯模糊滤镜去除图像噪点是一种广泛使用的技术。此技术通过对图像应用卷积滤镜来平滑像素值。

示例

这是一个使用 ImageFilter.GaussianBlur() 滤镜去除 RGB 图像噪点的示例。

from PIL import Image, ImageFilter

# Open a Gaussian Noised Image  
input_image = Image.open("Images/GaussianNoisedImage.jpg").convert('RGB')

# Apply Gaussian blur with some radius
blurred_image = input_image.filter(ImageFilter.GaussianBlur(radius=2))

# Display the input and the blurred image
input_image.show()
blurred_image.show()

输入噪点图像

GaussianNoisedImage

输出去噪图像

blurred image tp

使用中值滤波器去除噪点

中值滤波器是另一种降噪方法,尤其适用于噪点像小而分散的图像。此函数通过用其局部邻域内的中值替换每个像素值来工作。Pillow 库为此目的提供了 ImageFilter.MedianFilter() 滤镜。

示例

下面的示例使用 ImageFilter.MedianFilter() 去除图像噪点。

from PIL import Image, ImageFilter

# Open an image
input_image = Image.open("Images/balloons_noisy.png")

# Apply median filter with a kernel size 
filtered_image = input_image.filter(ImageFilter.MedianFilter(size=3))

# Display the input and the filtered image
input_image.show()
filtered_image.show()

输入噪点图像

balloons noisy

输出中值滤波图像

filtered image balloons

示例

此示例演示了如何使用中值滤波器减少灰度图像中的椒盐噪声。这可以通过使用中值滤波器、增强对比度以提高可见性,然后将图像转换为二进制模式以进行显示来完成。

from PIL import Image, ImageEnhance, ImageFilter, ImageOps

# Open the image and convert it to grayscale
input_image = ImageOps.grayscale(Image.open('Images/salt-and-pepper noise.jpg'))

# Apply a median filter with a kernel size of 5 to reduce noise
filtered_image = input_image.filter(ImageFilter.MedianFilter(5))

# Enhance the contrast of the filtered image
contrast_enhancer = ImageEnhance.Contrast(filtered_image)
high_contrast_image = contrast_enhancer.enhance(3)

# Convert the image to binary
binary_image = high_contrast_image.convert('1')

# Display the input and processed images
input_image.show()
binary_image.show()

输入噪点图像

salt and pepper noise

输出中值滤波图像

median filtered
广告
© . All rights reserved.