python数字图像处理之高级滤波代码详解
Python  /  管理员 发布于 7年前   309
本文提供许多的滤波方法,这些方法放在filters.rank子模块内。
这些方法需要用户自己设定滤波器的形状和大小,因此需要导入morphology模块来设定。
1、autolevel
这个词在photoshop里面翻译成自动色阶,用局部直方图来对图片进行滤波分级。
该滤波器局部地拉伸灰度像素值的直方图,以覆盖整个像素值范围。
格式:skimage.filters.rank.autolevel(image, selem)
selem表示结构化元素,用于设定滤波器。
from skimage import data,colorimport matplotlib.pyplot as pltfrom skimage.morphology import diskimport skimage.filters.rank as sfrimg =color.rgb2gray(data.lena())auto =sfr.autolevel(img, disk(5)) #半径为5的圆形滤波器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(auto,plt.cm.gray)
2、bottomhat 与 tophat
bottomhat: 此滤波器先计算图像的形态学闭运算,然后用原图像减去运算的结果值,有点像黑帽操作。
bottomhat: 此滤波器先计算图像的形态学开运算,然后用原图像减去运算的结果值,有点像白帽操作。
格式:
skimage.filters.rank.bottomhat(image, selem)
skimage.filters.rank.tophat(image, selem)
selem表示结构化元素,用于设定滤波器。
下面是bottomhat滤波的例子:
from skimage import data,colorimport matplotlib.pyplot as pltfrom skimage.morphology import diskimport skimage.filters.rank as sfrimg =color.rgb2gray(data.lena())auto =sfr.bottomhat(img, disk(5)) #半径为5的圆形滤波器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(auto,plt.cm.gray)
3、enhance_contrast
对比度增强。求出局部区域的最大值和最小值,然后看当前点像素值最接近最大值还是最小值,然后替换为最大值或最小值。
函数: enhance_contrast(image, selem)
selem表示结构化元素,用于设定滤波器。
from skimage import data,colorimport matplotlib.pyplot as pltfrom skimage.morphology import diskimport skimage.filters.rank as sfrimg =color.rgb2gray(data.lena())auto =sfr.enhance_contrast(img, disk(5)) #半径为5的圆形滤波器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(auto,plt.cm.gray)
4、entropy
求局部熵,熵是使用基为2的对数运算出来的。该函数将局部区域的灰度值分布进行二进制编码,返回编码的最小值。
函数格式:entropy(image, selem)
selem表示结构化元素,用于设定滤波器。
from skimage import data,colorimport matplotlib.pyplot as pltfrom skimage.morphology import diskimport skimage.filters.rank as sfrimg =color.rgb2gray(data.lena())dst =sfr.entropy(img, disk(5)) #半径为5的圆形滤波器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)
5、equalize
均衡化滤波。利用局部直方图对图像进行均衡化滤波。
函数格式:equalize(image, selem)
selem表示结构化元素,用于设定滤波器。
from skimage import data,colorimport matplotlib.pyplot as pltfrom skimage.morphology import diskimport skimage.filters.rank as sfrimg =color.rgb2gray(data.lena())dst =sfr.equalize(img, disk(5)) #半径为5的圆形滤波器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)
6、gradient
返回图像的局部梯度值(如:最大值-最小值),用此梯度值代替区域内所有像素值。
函数格式:gradient(image, selem)
selem表示结构化元素,用于设定滤波器。
from skimage import data,colorimport matplotlib.pyplot as pltfrom skimage.morphology import diskimport skimage.filters.rank as sfrimg =color.rgb2gray(data.lena())dst =sfr.gradient(img, disk(5)) #半径为5的圆形滤波器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)
7、其它滤波器
滤波方式很多,下面不再一一详细讲解,仅给出核心代码,所有的函数调用方式都是一样的。
最大值滤波器(maximum):返回图像局部区域的最大值,用此最大值代替该区域内所有像素值。
dst =sfr.maximum(img, disk(5))
最小值滤波器(minimum):返回图像局部区域内的最小值,用此最小值取代该区域内所有像素值。
dst =sfr.minimum(img, disk(5))
均值滤波器(mean) : 返回图像局部区域内的均值,用此均值取代该区域内所有像素值。
dst =sfr.mean(img, disk(5))
中值滤波器(median): 返回图像局部区域内的中值,用此中值取代该区域内所有像素值。
dst =sfr.median(img, disk(5))
莫代尔滤波器(modal) : 返回图像局部区域内的modal值,用此值取代该区域内所有像素值。
dst =sfr.modal(img, disk(5))
otsu阈值滤波(otsu): 返回图像局部区域内的otsu阈值,用此值取代该区域内所有像素值。
dst =sfr.otsu(img, disk(5))
阈值滤波(threshhold): 将图像局部区域中的每个像素值与均值比较,大于则赋值为1,小于赋值为0,得到一个二值图像。
dst =sfr.threshold(img, disk(5))
减均值滤波(subtract_mean): 将局部区域中的每一个像素,减去该区域中的均值。
dst =sfr.subtract_mean(img, disk(5))
求和滤波(sum) :求局部区域的像素总和,用此值取代该区域内所有像素值。
dst =sfr.sum(img, disk(5))
总结
以上就是本文关于python数字图像处理之高级滤波代码详解的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:
Python中turtle作图示例
python通过opencv实现批量剪切图片
python好玩的项目―色情图片识别代码分享
如有不足之处,欢迎留言指出。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号