zoukankan      html  css  js  c++  java
  • python数字图像处理(14):高级滤波

    本文提供更多更强大的滤波方法,这些方法放在filters.rank子模块内。

    这些方法需要用户自己设定滤波器的形状和大小,因此需要导入morphology模块来设定。

    1、autolevel

    这个词在photoshop里面翻译成自动色阶,用局部直方图来对图片进行滤波分级。

    该滤波器局部地拉伸灰度像素值的直方图,以覆盖整个像素值范围。

    格式:skimage.filters.rank.autolevel(image, selem)

    selem表示结构化元素,用于设定滤波器。

    from skimage import data,color
    import matplotlib.pyplot as plt
    from skimage.morphology import disk
    import skimage.filters.rank as sfr
    img =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,color
    import matplotlib.pyplot as plt
    from skimage.morphology import disk
    import skimage.filters.rank as sfr
    img =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,color
    import matplotlib.pyplot as plt
    from skimage.morphology import disk
    import skimage.filters.rank as sfr
    img =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,color
    import matplotlib.pyplot as plt
    from skimage.morphology import disk
    import skimage.filters.rank as sfr
    img =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,color
    import matplotlib.pyplot as plt
    from skimage.morphology import disk
    import skimage.filters.rank as sfr
    img =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,color
    import matplotlib.pyplot as plt
    from skimage.morphology import disk
    import skimage.filters.rank as sfr
    img =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))
  • 相关阅读:
    php与js 编码解码交互
    计算每页个数算法
    线程合并
    Sql调试
    并发问题
    context.Request方法总结
    元字符
    PHP留言小练习
    Git Shell Warning
    loading bar
  • 原文地址:https://www.cnblogs.com/denny402/p/5133086.html
Copyright © 2011-2022 走看看