美文网首页
图像增强

图像增强

作者: echo_ye4 | 来源:发表于2020-01-03 13:16 被阅读0次

    图像增强包括空间域增强与频域图像增强。
    考虑以下图片:


    image.png

    首先考虑灰度图的处理,转化为灰度图片

    import numpy as np
    import matplotlib.pyplot as plt
    from PIL import Image
    
    #读取图片
    dir = 'hashiqi.jpb'
    pic = Image.open(dir)
    pic_l = pic.convert("L")
    im = np.asarray(pic)
    im_l = np.asarray(pic_l)
    plt.imshow(im_l, cmap="gray")
    
    image.png

    空间域图像增强

    基本灰度变换

    图像反转

    灰度级范围为[0, L-1],变换s=L-1-r

    im_new = 255 - im_l
    plt.imshow(im_new, cmap="gray")
    
    image.png
    对数变换

    变换s=clog(1+r),可用于扩展暗像素

    c = 255 / np.log(256)
    im_new = c * np.log(1.0 + im_l)
    im_new = np.array(im_new, dtype=np.uint8)
    plt.imshow(im_new, cmap="gray")
    
    image.png
    幂次变换

    s=cr^y,与对数变换类似,可以扩展暗像素或明像素,比对数变换更加灵活,如显示器的伽马较正

    分段线性变换

    扩展任意像素段,更加灵活

    直方图处理

    对比原图与对数变换后的像素分布,可以看到明亮的图倾向于灰度级高的一侧,对比度高的图覆盖了灰度级很宽的范围。


    image.png
    直方图均衡

    假设r为原始图像的灰度级,且归一化到[0,1]的区间中;
    考虑一个变换s = T(r),当0<=r<=1时,T(r)单调递增,且0<=T(r)<=1。
    将图像的灰度级看作[0,1]内的随机变量,p(r)和p(s)分别表示r和s的概率密度函数,p(s) = p(r)|\frac {dr}{ds}|
    考虑一个变换s = T(r) = \int_{0}^{r} p(w)dw,此时p(s)=1
    对于离散值,p(r_k) = \frac {n_k}{n}, k = 0, 1, ..., L-1s_k = T(r) = \sum_{j=0}^{k} p(r_j)

    def probability(x, im):
        w, h = im.shape
        return np.sum(im==x) / (w * h)
    pdf = []
    for i in range(256):
        pdf.append(probability(i, im_new))
    cdf = []
    s = 0
    for i in pdf:
        s += i
        cdf.append(s)
    def trans(x):
        return cdf[x] * 255
    im_bal = im_new.copy()
    for i in np.nditer(im_bal, op_flags=['readwrite']): 
        i[...]=trans(i)
    ax = plt.subplot(321)
    ax.imshow(im_bal, cmap="gray")
    ax2 = plt.subplot(322)
    ax2.hist(im_bal.flatten())
    ax3 = plt.subplot(323)
    ax3.imshow(im_new, cmap="gray")
    ax4 = plt.subplot(324)
    ax4.hist(im_new.flatten())
    ax5 = plt.subplot(325)
    ax5.imshow(im_l, cmap="gray")
    ax6 = plt.subplot(326)
    ax6.hist(im_l.flatten())
    
    image.png

    空间滤波

    空间滤波就是在待处理图像中逐点移动掩膜。
    对于3*3掩膜,在点(x, y),响应R=w(-1, -1)f(x-1, y-1) + w(-1, 0)f(x-1, y) + ... + w(0, 0)f(x, y) + ... + w(1, 1)f(x+1, y+1)
    一般来说,在MN的图像中,使用mn大小的掩膜进行线性滤波,有:g(x, y) = \sum_{s=-a}^{a}\sum_{t=-b}^{b}w(s, t)f(x+s, y+t), 其中a = (m-1)/2, b = (n-1)/2

    平滑空间滤波器

    平滑滤波器用于模糊处理和减小噪声。模糊处理经常用于预处理,例如在提取大的目标之前去除图像中的一些琐碎细节,桥接缝隙等。

    平滑线性滤波器

    均值滤波器,或带权均值滤波器,如:
    \frac 19 * \begin{bmatrix} 1 & 1 & 1\\ 1 & 1 & 1\\ 1 & 1 & 1\\ \end{bmatrix}
    ,或
    \frac 1{16} * \begin{bmatrix} 1 & 2 & 1\\ 2 & 4 & 2\\ 1 & 2 & 1\\ \end{bmatrix}

    统计排序滤波器

    统计滤波器是一种非线性滤波器,用统计排序结果代替中心像素的值,如中值滤波器,用于去噪效果很好

    from PIL import ImageFilter
    f = ImageFilter.Kernel((3,3),(1,1,1,1,1,1,1,1,1))
    f2 = ImageFilter.MedianFilter(5)
    ax = plt.subplot(131)
    ax.set_title("linear")
    ax.imshow(pic.filter(f))
    ax2 = plt.subplot(132)
    ax2.set_title("median")
    ax2.imshow(pic.filter(f2))
    ax3 = plt.subplot(133)
    ax3.set_title("origin")
    ax3.imshow(pic)
    
    image.png

    锐化空间滤波器

    锐化处理的主要目的是突出图像中的细节或增强被模糊的细节。图像微分增强了边缘和其他突变。
    一阶微分:
    \frac {df}{dx} = f(x+1) - f(x)

    二阶微分:
    \frac {d^2f}{dx^2} = f(x+1) + f(x-1) - 2f(x)

    梯度变换在灰度变化的区域的响应要比拉普拉斯变换强烈,而对噪声和小细节的响应比拉普拉斯弱。

    基于二阶微分的图像增强——拉普拉斯算子

    我们最关注的是一种各向同性滤波器,即滤波器是旋转不变的,对原图像先旋转再滤波与先滤波再旋转的结果相同。
    一个二元函数的拉普拉斯变换为:
    \nabla ^2 f = \frac {d^2f}{dx^2} + \frac {d^2f}{dy^2} = f(x+1, y) + f(x-1, y) + f(x, y+1) + f(x, y-1) - 4f(x, y)

    相当于掩膜:
    \begin{bmatrix} 0 & 1 & 0 \\ 1 & -4 & 1 \\ 0 & 1 & 0 \\ \end{bmatrix}

    考虑对角线方向,可使用掩膜:
    \begin{bmatrix} 1 & 1 & 1\\ 1 & -8 & 1\\ 1 & 1 & 1\\ \end{bmatrix}

    与原图像叠加可增强图像:g(x, y) = f(x, y) - \nabla ^2 f(x, y)

    相当于掩膜:
    \begin{bmatrix} 0 & -1 & 0 \\ -1 & 5 & -1 \\ 0 & -1 & 0 \\ \end{bmatrix}

    \begin{bmatrix} -1 & -1 & -1\\ -1 & 9 & -1\\ -1 & -1 & -1\\ \end{bmatrix}

    f = ImageFilter.Kernel((3,3),(1,1,1,1,-8,1,1,1,1), scale=1)
    f2 = ImageFilter.Kernel((3,3),(-1,-1,-1,-1,9,-1,-1,-1,-1), scale=1)
    ax = plt.subplot(131)
    ax.set_title("laplace")
    ax.imshow(pic.filter(f))
    ax2 = plt.subplot(132)
    ax2.set_title("enhance")
    ax2.imshow(pic.filter(f2))
    ax3 = plt.subplot(133)
    ax3.set_title("origin")
    ax3.imshow(pic)
    
    image.png
    基于一阶微分的图像增强——梯度法

    梯度处理经常用于检测缺陷。
    可用以下掩膜,第三行与第一行的差接近于x方向的微分,第三列与第一列的差相当于y方向的微分,系数总和为0,相当于灰度恒定区域的响应为0.
    \begin{bmatrix} -1 & -2 & -1\\ 0 & 0 & 0\\ 1 & 2 & 1\\ \end{bmatrix} \begin{bmatrix} -1 & 0 & 1\\ -2 & 0 & 2\\ -1 & 0 & 1\\ \end{bmatrix}

    f = ImageFilter.Kernel((3,3),(-2, -2, 0, -2, 0, 2, 0, 2, 2), scale=1)
    ax = plt.subplot(121)
    ax.set_title("laplace")
    ax.imshow(pic.filter(f))
    ax2 = plt.subplot(122)
    ax2.set_title("origin")
    ax2.imshow(pic)
    
    image.png

    在实际应用中,为了得到一个满意的结果,对给定对图像需要应用多种互补对图像增强技术。

    相关文章

      网友评论

          本文标题:图像增强

          本文链接:https://www.haomeiwen.com/subject/djezoctx.html