Python图像处理库PIL的滤波_ImageFilter

            Python图像处理库PIL的滤波_ImageFilter

ImageFilter模块提供了滤波器相关定义;这些滤波器主要用于Image类的filter()方法。

一、ImageFilter模块所支持的滤波器

当前的PIL版本中ImageFilter模块支持十种滤波器:

1、  BLUR

ImageFilter.BLUR为模糊滤波,处理之后的图像会整体变得模糊。

例子:

>>> from PIL importImageFilter

>>> im02 =Image.open("D://Code//Python//test//img//test02.jpg")

>>> im = im02.filter(ImageFilter.BLUR)

图像im02为原始图像,如下:

   Python图像处理库PIL的ImageFilter模块介绍_Python                          

图像im为模糊滤波之后的图像,明显比原始图像模糊了一些,见下图:

扫描二维码关注公众号,回复: 4840620 查看本文章

 Python图像处理库PIL的ImageFilter模块介绍_Python

2、  CONTOUR

ImageFilter.CONTOUR为轮廓滤波,将图像中的轮廓信息全部提取出来。

例子:

>>>from PIL import ImageFilter

>>>im02 = Image.open("D://Code//Python//test//img//test02.jpg")

>>> im= im02.filter(ImageFilter.CONTOUR)

图像im为轮廓图像,如下图:

 Python图像处理库PIL的ImageFilter模块介绍_Python

3、  DETAIL

ImageFilter.DETAIL为细节增强滤波,会使得图像中细节更加明显。

例子:

>>>from PIL import ImageFilter

>>>im02 = Image.open("D://Code//Python//test//img//test02.jpg")

>>> im= im02.filter(ImageFilter.DETAIL)

图像im为细节增强后的图像,如下图:

 Python图像处理库PIL的ImageFilter模块介绍_Python

4、 EDGE_ENHANCE

ImageFilter.EDGE_ENHANCE为边缘增强滤波,突出、加强和改善图像中不同灰度区域之间的边界和轮廓的图像增强方法。经处理使得边界和边缘在图像上表现为图像灰度的突变,用以提高人眼识别能力。

例子:

>>>from PIL import ImageFilter

>>>im02 = Image.open("D://Code//Python//test//img//test02.jpg")

>>> im= im02.filter(ImageFilter.EDGE_ENHANCE)

图像im为边缘增强后的图像,如下图:

 Python图像处理库PIL的ImageFilter模块介绍_Python

5、 EDGE_ENHANCE_MORE

ImageFilter.EDGE_ENHANCE_MORE为深度边缘增强滤波,会使得图像中边缘部分更加明显。

例子:

>>>from PIL import ImageFilter

>>>im02 = Image.open("D://Code//Python//test//img//test02.jpg")

>>> im= im02.filter(ImageFilter.EDGE_ENHANCE_MORE)

图像im为深度边缘增强后的图像,如下图:

 Python图像处理库PIL的ImageFilter模块介绍_Python

6、  EMBOSS

ImageFilter.EMBOSS为浮雕滤波,会使图像呈现出浮雕效果。

例子:

>>>from PIL import ImageFilter

>>>im02 = Image.open("D://Code//Python//test//img//test02.jpg")

>>> im= im02.filter(ImageFilter.EMBOSS)

图像im为浮雕效果的图像,如下图:

 Python图像处理库PIL的ImageFilter模块介绍_Python        

7、 FIND_EDGES

ImageFilter.FIND_EDGES为寻找边缘信息的滤波,会找出图像中的边缘信息。

例子:

>>>from PIL import ImageFilter

>>>im02 = Image.open("D://Code//Python//test//img//test02.jpg")

>>> im= im02.filter(ImageFilter.FIND_EDGES)

图像im为边缘信息的图像,如下图:

 Python图像处理库PIL的ImageFilter模块介绍_Python

8、  SMOOTH

ImageFilter.SMOOTH为平滑滤波,突出图像的宽大区域、低频成分、主干部分或抑制图像噪声和干扰高频成分,使图像亮度平缓渐变,减小突变梯度,改善图像质量。

例子:

>>>from PIL import ImageFilter

>>>im02 = Image.open("D://Code//Python//test//img//test02.jpg")

>>> im= im02.filter(ImageFilter.SMOOTH)

图像im为平滑滤波的图像,如下图:

 Python图像处理库PIL的ImageFilter模块介绍_Python

9、  SMOOTH_MORE

ImageFilter.SMOOTH_MORE为深度平滑滤波,会使得图像变得更加平滑。

例子:

>>> from PIL importImageFilter

>>> im02 =Image.open("D://Code//Python//test//img//test02.jpg")

>>> im =im02.filter(ImageFilter.SMOOTH_MORE)

图像im为深度平滑滤波的图像,如下图:

 Python图像处理库PIL的ImageFilter模块介绍_Python

10、             SHARPEN

ImageFilter.SHARPEN为锐化滤波,补偿图像的轮廓,增强图像的边缘及灰度跳变的部分,使图像变得清晰。

例子:

>>>from PIL import ImageFilter

>>> im02 =Image.open("D://Code//Python//test//img//test02.jpg")

>>> im =im02.filter(ImageFilter.SHARPEN)

图像im为锐化滤波的图像,如下图:

 Python图像处理库PIL的ImageFilter模块介绍_Python

二、ImageFilter模块的函数

1、  Kernel

定义:Kernel(size,kernel, scale=None, offset=0)

含义:(New in 1.1.5)生成一个给定尺寸的卷积核。在当前的版本中,变量size必须为(3,3)或者(5,5)。变量kernel与变量size对应地必须为包含9个或者25个整数或者浮点数的序列。

如果设置了变量scale,那将卷积核作用于每个像素值之后的数据,都需要除以这个变量。默认值为卷积核的权重之和。

如果设置变量offset,这个值将加到卷积核作用的结果上,然后再除以变量scale。

例子: 

>>>from PIL import ImageFilter

>>> im02 =Image.open("D://Code//Python//test//img//test02.jpg")

>>> im=im02.filter(ImageFilter.Kernel((3,3),(1,1,1,0,0,0,2,0,2)))

图像im为经过卷积核处理过的图像,如下图:Python图像处理库PIL的ImageFilter模块介绍_Python

2、  RankFilter

定义:RankFilter(size,rank)

含义:(New in 1.1.5)生成给定尺寸的等级滤波器。对于输入图像的每个像素点,等级滤波器根据像素值,在(size,size)的区域中对所有像素点进行排序,然后拷贝对应等级的值存储到输出图像中。

例子:

>>>from PIL import ImageFilter

>>>im02 = Image.open("D://Code//Python//test//img//test02.jpg")

>>> im=im02.filter(ImageFilter.RankFilter(5,24))

图像im为等级滤波后的图像,在每个像素点为中心的5x5区域25个像素点中选择排序第24位的像素作为新的值。如下图:

 Python图像处理库PIL的ImageFilter模块介绍_Python

3、  MinFilter

定义:MinFilter(size=3)

含义:(New in 1.1.5)生成给定尺寸的最小滤波器。对于输入图像的每个像素点,该滤波器从(size,size)的区域中拷贝最小的像素值存储到输出图像中。

例子:

>>> from PIL importImageFilter

>>> im02 =Image.open("D://Code//Python//test//img//test02.jpg")

>>> im=im02.filter(ImageFilter.MinFilter(5))

图像im为最小滤波后的图像,在每个像素点为中心的5x5区域25个像素点中选择最小的像素作为新的值。如下图:

 Python图像处理库PIL的ImageFilter模块介绍_Python

4、  MedianFilter

定义:MedianFilter(size=3)

含义:(New in 1.1.5)生成给定尺寸的中值滤波器。对于输入图像的每个像素点,该滤波器从(size,size)的区域中拷贝中值对应的像素值存储到输出图像中。

例子:

>>>from PIL import ImageFilter

>>>im02 = Image.open("D://Code//Python//test//img//test02.jpg")

>>> im=im02.filter(ImageFilter.MedianFilter(5))

图像im为中值滤波后的图像,在每个像素点为中心的5x5区域25个像素点中选择中值作为新的值。如下图:

 Python图像处理库PIL的ImageFilter模块介绍_Python

5、  MaxFilter

定义:MaxFilter(size=3)

含义:(New in 1.1.5)生成给定尺寸的最大滤波器。对于输入图像的每个像素点,该滤波器从(size,size)的区域中拷贝最大的像素值存储到输出图像中。

例子:

>>>from PIL import ImageFilter

>>> im02 =Image.open("D://Code//Python//test//img//test02.jpg")

>>> im=im02.filter(ImageFilter.MaxFilter(5))

图像im为最大滤波后的图像,在每个像素点为中心的5x5区域25个像素点中选择最大的像素作为新的值。如下图:

 Python图像处理库PIL的ImageFilter模块介绍_Python

6、  ModeFilter

定义:ModeFilter(size=3)

含义:(New in 1.1.5)生成给定尺寸的模式滤波器。对于输入图像的每个像素点,该滤波器从(size,size)的区域中拷贝出现次数最多的像素值存储到输出图像中。如果没有一个像素值出现过两次极其以上,则使用原始像素值。

例子:

>>>from PIL import ImageFilter

>>>im02 = Image.open("D://Code//Python//test//img//test02.jpg")

>>> im=im02.filter(ImageFilter.ModeFilter(5))

图像im为模式滤波后的图像,在每个像素点为中心的5x5区域25个像素点中选择出现次数最多的像素作为新的值。如下图:

 Python图像处理库PIL的ImageFilter模块介绍_Python

Python代码实现

# -*- coding: utf-8 -*-

###############模糊滤波
from PIL import ImageFilter
from PIL import Image

im02 =Image.open("D://py_project//picture_process//175.jpg")
im = im02.filter(ImageFilter.BLUR)
im.show()

###############轮廓滤波
im02 = Image.open("D://py_project//picture_process//test02.jpg")
im= im02.filter(ImageFilter.CONTOUR)

################细节增强滤波
im02 = Image.open("D://py_project//picture_process//comic.bmp")
im= im02.filter(ImageFilter.DETAIL)
im.show()

#####################边缘增强滤波
im02 = Image.open("D://py_project//picture_process//comic.bmp")
im= im02.filter(ImageFilter.EDGE_ENHANCE)
im.show()

#####################深度边缘增强滤波
im02 = Image.open("D://py_project//picture_process//comic.bmp")
im= im02.filter(ImageFilter.EDGE_ENHANCE_MORE)
im.show()

#####################浮雕滤波
im02 = Image.open("D://py_project//picture_process//test02.jpg")
im= im02.filter(ImageFilter.EMBOSS)

######################寻找边缘信息的滤波
im02 = Image.open("D://py_project//picture_process//test02.jpg")
im= im02.filter(ImageFilter.FIND_EDGES)

#####################平滑滤波
im02 = Image.open("D://py_project//picture_process//test02.jpg")
im= im02.filter(ImageFilter.SMOOTH)

########################深度平滑滤波
im02 =Image.open("D://py_project//picture_process//comic.bmp")
im =im02.filter(ImageFilter.SMOOTH_MORE)
im.show()

#######################锐化滤波
im02 =Image.open("D://py_project//picture_process//comic.bmp")
im =im02.filter(ImageFilter.SHARPEN)
im.show()

##############################Kernel
im02 =Image.open("D://py_project//picture_process//test02.jpg")
im=im02.filter(ImageFilter.Kernel((3,3),(1,1,1,0,0,0,2,0,2)))

##########################给定尺寸的等级滤波器
im02 = Image.open("D://py_project//picture_process//test02.jpg")
im=im02.filter(ImageFilter.RankFilter(5,24))

############################最小滤波器
im02 =Image.open("D://py_project//picture_process//test02.jpg")
im=im02.filter(ImageFilter.MinFilter(5))

#########################中值滤波器
im02 = Image.open("D://py_project//picture_process//test02.jpg")
im=im02.filter(ImageFilter.MedianFilter(5))

#################################最大滤波器
im02 =Image.open("D://py_project//picture_process//test02.jpg")
im=im02.filter(ImageFilter.MaxFilter(5))

#################################给定尺寸的模式滤波器
im02 = Image.open("D://py_project//picture_process//test02.jpg")
im=im02.filter(ImageFilter.ModeFilter(5))

【转载】:https://m.aliyun.com/jiaocheng/470850.html

猜你喜欢

转载自blog.csdn.net/u013185349/article/details/85170541