Python中怎么样进行滤波操作,方法是什么
Admin 2022-08-06 群英技术资讯 1766 次浏览
在这篇文章中,我们来学习一下“Python中怎么样进行滤波操作,方法是什么”的相关知识,下文有详细的讲解,易于大家学习和理解,有需要的朋友可以借鉴参考,下面就请大家跟着小编的思路一起来学习一下吧。对图像进行滤波,可以有两种效果:一种是平滑滤波,用来抑制噪声;另一种是微分算子,可以用来检测边缘和特征提取。
skimage库中通过filters模块进行滤波操作。
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)

roberts算子和sobel算子一样,用于检测边缘
调用格式也是一样的:
edges = filters.roberts(img)
功能同sobel,调用格式:
edges = filters.scharr(img)
功能同sobel,调用格式:
edges = filters.prewitt(img)
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越小,边缘线条越细小。
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的结果图
多维的滤波器,是一种平滑滤波,可以消除高斯噪声。
调用函数为: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越大,过滤后的图像越模糊
中值滤波,一种平滑滤波,可以消除噪声。
需要用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()

从结果可以看出,滤波器越大,图像越模糊。
上边所举的例子都是进行全部边缘检测,有些时候我们只需要检测水平边缘,或垂直边缘,就可用下面的方法。
水平边缘检测: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()

上边左图为检测出的水平边缘,右图为检测出的垂直边缘。
可使用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)

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了手把手教你使用Python绘制时间序列图,本文将以股价数据集为例,指导你从Quandl下载股价数据集,并将这些数据绘制在价格和成交量图表上,需要的朋友可以参考下
本文详细讲解了python中函数的参数,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
本章节将一些Python3基础语法整理成手册,方便各位在日常使用和学习是查阅,包含了编码、标识符、保留字、注释、缩进、字符串等常用内容。
小伙伴们还记不记得,在高考数学题后面的大题总会出现对数函数,需要我们画成对数函数图才能解答。之前小编向大家介绍对数log函数的表示方法,其实一般我们在使用对数函数的时候,会和对数函数图配合使用解决实际问题。那你知不知道在python中也可以画对数函数图呢?本文小编就以代码的形式向大家演示在python中绘制对数函数图的过程。
这篇文章主要介绍了利用Python+Selenium+Pytesseract实现图片验证码识别,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008