python数字图像处理之图像简单滤波实现

 更新时间:2022年06月28日 16:58:34   作者:denny402  
这篇文章主要为大家介绍了python数字图像处理之图像简单滤波实现示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

引言

对图像进行滤波,可以有两种效果:一种是平滑滤波,用来抑制噪声;另一种是微分算子,可以用来检测边缘和特征提取。

skimage库中通过filters模块进行滤波操作。

1、sobel算子

sobel算子可用来检测边缘

函数格式为:skimage.filters.sobel(image, mask=None)

from skimage import data,filters
import matplotlib.pyplot as plt
img = data.camera()
edges = filters.sobel(img)
plt.imshow(edges,plt.cm.gray)

2、roberts算子

roberts算子和sobel算子一样,用于检测边缘

调用格式也是一样的:

edges = filters.roberts(img)

3、scharr算子

功能同sobel,调用格式:

edges = filters.scharr(img)

4、prewitt算子

功能同sobel,调用格式:

edges = filters.prewitt(img)

5、canny算子

canny算子也是用于提取边缘特征,但它不是放在filters模块,而是放在feature模块

函数格式:skimage.feature.canny(image,sigma=1.0)

可以修改sigma的值来调整效果

from skimage import data,filters,feature
import matplotlib.pyplot as plt
img = data.camera()
edges1 = feature.canny(img)   #sigma=1
edges2 = feature.canny(img,sigma=3)   #sigma=3
plt.figure('canny',figsize=(8,8))
plt.subplot(121)
plt.imshow(edges1,plt.cm.gray)  
plt.subplot(122)
plt.imshow(edges2,plt.cm.gray)
plt.show()

从结果可以看出,sigma越小,边缘线条越细小。

6、gabor滤波

gabor滤波可用来进行边缘检测和纹理特征提取。

函数调用格式:skimage.filters.gabor_filter(image, frequency)

通过修改frequency值来调整滤波效果,返回一对边缘结果,一个是用真实滤波核的滤波结果,一个是想象的滤波核的滤波结果。

from skimage import data,filters
import matplotlib.pyplot as plt
img = data.camera()
filt_real, filt_imag = filters.gabor_filter(img,frequency=0.6)   
plt.figure('gabor',figsize=(8,8))
plt.subplot(121)
plt.title('filt_real')
plt.imshow(filt_real,plt.cm.gray)  
plt.subplot(122)
plt.title('filt-imag')
plt.imshow(filt_imag,plt.cm.gray)
plt.show()

以上为frequency=0.6的结果图。

以上为frequency=0.1的结果图

7、gaussian滤波

多维的滤波器,是一种平滑滤波,可以消除高斯噪声。

调用函数为:skimage.filters.gaussian_filter(image, sigma)

通过调节sigma的值来调整滤波效果

from skimage import data,filters
import matplotlib.pyplot as plt
img = data.astronaut()
edges1 = filters.gaussian_filter(img,sigma=0.4)   #sigma=0.4
edges2 = filters.gaussian_filter(img,sigma=5)   #sigma=5
plt.figure('gaussian',figsize=(8,8))
plt.subplot(121)
plt.imshow(edges1,plt.cm.gray)  
plt.subplot(122)
plt.imshow(edges2,plt.cm.gray)
plt.show()

可见sigma越大,过滤后的图像越模糊

8、median

中值滤波,一种平滑滤波,可以消除噪声。

需要用skimage.morphology模块来设置滤波器的形状。

from skimage import data,filters
import matplotlib.pyplot as plt
from skimage.morphology import disk
img = data.camera()
edges1 = filters.median(img,disk(5))
edges2= filters.median(img,disk(9))
plt.figure('median',figsize=(8,8))
plt.subplot(121)
plt.imshow(edges1,plt.cm.gray)  
plt.subplot(122)
plt.imshow(edges2,plt.cm.gray)
plt.show()

从结果可以看出,滤波器越大,图像越模糊。

9、水平、垂直边缘检测

上边所举的例子都是进行全部边缘检测,有些时候我们只需要检测水平边缘,或垂直边缘,就可用下面的方法。

水平边缘检测:sobel_h, prewitt_h, scharr_h

垂直边缘检测: sobel_v, prewitt_v, scharr_v

from skimage import data,filters
import matplotlib.pyplot as plt
img = data.camera()
edges1 = filters.sobel_h(img)  
edges2 = filters.sobel_v(img) 
plt.figure('sobel_v_h',figsize=(8,8))
plt.subplot(121)
plt.imshow(edges1,plt.cm.gray)  
plt.subplot(122)
plt.imshow(edges2,plt.cm.gray)
plt.show()

上边左图为检测出的水平边缘,右图为检测出的垂直边缘。

10、交叉边缘检测

可使用Roberts的十字交叉核来进行过滤,以达到检测交叉边缘的目的。这些交叉边缘实际上是梯度在某个方向上的一个分量。

其中一个核:

0   1
-1   0

对应的函数:

roberts_neg_diag(image)

 例:

from skimage import data,filters
import matplotlib.pyplot as plt
img =data.camera()
dst =filters.roberts_neg_diag(img) 
plt.figure('filters',figsize=(8,8))
plt.subplot(121)
plt.title('origin image')
plt.imshow(img,plt.cm.gray)
plt.subplot(122)
plt.title('filted image')
plt.imshow(dst,plt.cm.gray)

另外一个核:

1   0
0  -1

对应函数为:

roberts_pos_diag(image)

from skimage import data,filters
import matplotlib.pyplot as plt
img =data.camera()
dst =filters.roberts_pos_diag(img) 
plt.figure('filters',figsize=(8,8))
plt.subplot(121)
plt.title('origin image')
plt.imshow(img,plt.cm.gray)
plt.subplot(122)
plt.title('filted image')
plt.imshow(dst,plt.cm.gray)

以上就是python数字图像处理之图像简单滤波实现的详细内容,更多关于python数字图像处理简单滤波的资料请关注脚本之家其它相关文章!

相关文章

  • 详解Python中httptools模块的使用

    详解Python中httptools模块的使用

    httptools 是一个 HTTP 解析器,它首先提供了一个 parse_url 函数,用来解析 URL。这篇文章就来和大家聊聊它的用法吧,感兴趣的可以了解一下
    2023-03-03
  • Numpy的简单用法小结

    Numpy的简单用法小结

    这篇文章主要介绍了Numpy的简单用法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-08-08
  • python mac下安装虚拟环境的图文教程

    python mac下安装虚拟环境的图文教程

    这篇文章主要介绍了python mac下安装虚拟环境 的相关资料,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-04-04
  • Jupyter Notebook读取csv文件出现的问题及解决

    Jupyter Notebook读取csv文件出现的问题及解决

    这篇文章主要介绍了Jupyter Notebook读取csv文件出现的问题及解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-01-01
  • Python魔术方法专题

    Python魔术方法专题

    这篇文章主要介绍了Python魔术方法的的相关资料,文章讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-06-06
  • Python制作动态词频条形图的全过程

    Python制作动态词频条形图的全过程

    说起动态图表,最火的莫过于动态条形图了,下面这篇文章主要给大家介绍了关于Python制作动态词频条形图的全过程,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2021-11-11
  • Python快速生成随机密码超简单实现

    Python快速生成随机密码超简单实现

    这篇文章主要介绍了Python快速生成随机密码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-08-08
  • Python3删除排序数组中重复项的方法分析

    Python3删除排序数组中重复项的方法分析

    这篇文章主要介绍了Python3删除排序数组中重复项的方法,结合实例形式分析了Python3删除排序数组重复项的原理、相关遍历及删除操作技巧,需要的朋友可以参考下
    2019-01-01
  • Python和Anaconda和Pycharm安装教程图文详解

    Python和Anaconda和Pycharm安装教程图文详解

    PyCharm是一种PythonIDE,带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具,这篇文章主要介绍了Python和Anaconda和Pycharm安装教程,需要的朋友可以参考下
    2020-02-02
  • python中struct模块之字节型数据的处理方法

    python中struct模块之字节型数据的处理方法

    今天小编就为大家分享一篇python中struct模块之字节型数据的处理方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-08-08

最新评论