05锐化滤波

锐化滤波器

主要应用

  • 图像识别中,分割前的边缘提取

  • 原始图像细节模糊,需要突出图像中的细节;

  • 弥补扫描对图像的钝化;

  • 超声探测成像,分辨率低,边缘模糊,通过锐化来改善;

  • 处理曝光不足的图像

  • 尖端武器的目标识别、定位

求邻域的灰度均值,可以产生钝化的效果,而均值与积分相似。所以可以用微分(一阶微分、二阶微分)产生相反效果,实现锐化。

边缘和细节都位于灰度突变的地方。图像微分突出了灰度突变的区域(边缘和其它突变如噪声),削弱了灰度变化缓慢的区域。最简单的锐化空间滤波器:基于掩模操作一阶和二阶微分算子。

一维

一维离散函数的微分(差分)算子:

  • 一阶(前向)差分:

∂ f ∂ x ≈ Δ f Δ x = f ( x + 1 ) − f ( x ) \frac{\partial f}{\partial x}\approx \frac{\Delta f}{\Delta x} = f(x+1)-f(x) xfΔxΔf=f(x+1)f(x)

  • 二阶差分

∂ 2 f ∂ x 2 ≈ f ‘ ( x ) − f ‘ ( x − 1 ) = [ f ( x + 1 ) − f ( x ) ] − [ f ( x ) − f ( x − 1 ) ] = f ( x + 1 ) + f ( x − 1 ) − 2 f ( x ) \frac{\partial^2 f}{\partial x^2}\approx f^{`}(x)-f^`(x-1)\\ =[f(x+1)-f(x)]-[f(x)-f(x-1)]\\ =f(x+1)+f(x-1)-2f(x) x22ff(x)f(x1)=[f(x+1)f(x)][f(x)f(x1)]=f(x+1)+f(x1)2f(x)

基于一阶微分的图像增强— 梯度算子:

∇ f = [ G x G y ] = [ ∂ f ∂ x ∂ f ∂ y ] \nabla f = \begin{bmatrix} G_x \\ G_y \\ \end{bmatrix}= \begin{bmatrix} \frac{\partial f}{\partial x} \\ \frac{\partial f}{\partial y} \\ \end{bmatrix} f=[GxGy]=[xfyf]

( x , y ) (x,y) (x,y)点处的梯度,方向是 f ( x , y ) f(x,y) f(x,y) 在这点变化率最大的方向,其大小等于 f ( x , y ) f (x,y) f(x,y) 的最大变化率:
∣ ∇ f ( x , y ) ∣ = [ ( ∂ f ∂ x ) 2 + ( ∂ f ∂ y ) 2 ] 1 2 |\nabla f(x,y)|= \begin{bmatrix} (\frac{\partial f}{\partial x})^2+(\frac{\partial f}{\partial y})^2 \end{bmatrix}^\frac{1}{2} f(x,y)=[(xf)2+(yf)2]21

二维

对于二维离散函数 f ( i , j ) f(i,j) f(i,j),用有限差分作为梯度大小的近似:
∣ ∇ f ( i , j ) ∣ = { [ f ( i + 1 , j ) − f ( i , j ) ] 2 + [ f ( i , j + 1 ) − f ( i , j ) ] 2 } 1 2 |\nabla f(i,j)|=\{[f(i+1,j)-f(i,j)]^2+[f(i,j+1)-f(i,j)]^2\}^\frac{1}{2} f(i,j)={[f(i+1,j)f(i,j)]2+[f(i,j+1)f(i,j)]2}21
上式包含平方和开方,运算量大,使用绝对差分近似:
∣ ∇ f ( i , j ) ∣ = ∣ f ( i + 1 , j ) − f ( i , j ) ∣ + ∣ f ( i , j + 1 ) − f ( i , j ) ∣ |\nabla f(i,j)|=|f(i+1,j)-f(i,j)|+|f(i,j+1)-f(i,j)| f(i,j)=f(i+1,j)f(i,j)+f(i,j+1)f(i,j)

实际使用中常采用另一种近似梯度—Robert交叉梯度
∣ ∇ f ( i , j ) ∣ = ∣ f ( i + 1 , j + 1 ) − f ( i , j ) ∣ + ∣ f ( i , j + 1 ) − f ( i + 1 , j ) ∣ |\nabla f(i,j)|=|f(i+1,j+1)-f(i,j)|+|f(i,j+1)-f(i+1,j)| f(i,j)=f(i+1,j+1)f(i,j)+f(i,j+1)f(i+1,j)

Robert交叉梯度模板

w 1 = [ − 1 0 0 − 1 ] 对 45 ° 边 缘 有 较 强 响 应 , 但 是 对 − 45 ° 边 缘 没 有 响 应 w 2 = [ 0 − 1 − 1 0 ] 对 − 45 ° 边 缘 有 较 强 响 应 但 是 对 45 ° 边 缘 没 有 响 应 w1= \begin{bmatrix} -1&0\\ 0&-1\\ \end{bmatrix}对45°边缘有较强响应,但是对-45°边缘没有响应\\ w2= \begin{bmatrix} 0&-1\\ -1&0\\ \end{bmatrix}对-45°边缘有较强响应但是对45°边缘没有响应\\ w1=[1001]45°,45°w2=[0110]45°45°

最终的Robert 模板锐化图像为 w 1 w1 w1 w 2 w2 w2 两个模板运算结果之和。

Code

import matplotlib.pyplot as plt
import numpy as np
import cv2 as cv

# Robert 锐化
def robert(img,w):
    """
    :param img: 输入灰度图像
    :param w: Robert 模板
    :return : 锐化图像
    """
    cp_img = np.copy(img)
    re_img = np.copy(img)

    for i in range(cp_img.shape[0]-1):
        for j in range(cp_img.shape[1]-1):

            temp = w*cp_img[i:i+2,j:j+2]
            re_img[i][j] = np.abs(temp[1][1]-temp[0][0])+np.abs(temp[1][0]-temp[0][1])

    return re_img

def show(img,title,sub):
    plt.subplot(sub[0],sub[1],sub[2])
    plt.imshow(img,cmap="gray")
    plt.axis('off') # 去掉坐标轴
    plt.title(title)

if __name__=="__main__":
    img = cv.imread('12.png')
    img = cv.cvtColor(img,cv.COLOR_RGB2GRAY)

    w1 = np.array([[-1,0],[0,1]]) # 对 45°边缘有较强响应
    w2 = np.array([[0,-1],[1,0]]) # 对 -45°边缘有较强响应
    w = w1+w2                     # 交叉梯度模板
    re_img1 = robert(img, w1)
    re_img2 = robert(img, w2)
    re_img3 = robert(img, w)
    show(img, 'original img', (1, 4, 1))
    show(re_img1, 'w1', (1, 4, 2))
    show(re_img2, 'w2', (1, 4, 3))
    show(re_img3, 'w', (1, 4, 4))
    plt.show()

在这里插入图片描述

Sobel梯度模板

S o b e l Sobel Sobel 梯度定义为:
m a s k = [ z 1 z 2 z 3 z 4 z 5 z 6 z 7 z 8 z 9 ] S x = ( z 7 + 2 z 8 + z 9 ) − ( z 1 + 2 z 2 + z 3 ) S y = ( z 3 + 2 z 6 + z 9 ) − ( z 1 + 2 z 4 + z 7 ) M ( i , j ) = ∣ S x ∣ + ∣ S y ∣ 使 用 权 重 2 的 目 的 : 突 出 中 心 点 mask = \begin{bmatrix} z_1&z_2&z_3\\ z_4&z_5&z_6\\ z_7&z_8&z_9\\ \end{bmatrix}\\ S_x = (z_7+2z_8+z_9)-(z_1+2z_2+z_3)\\ S_y = (z_3+2z_6+z_9)-(z_1+2z_4+z_7)\\ M(i,j) = |S_x|+|S_y| 使用权重2的目的:突出中心点 mask=z1z4z7z2z5z8z3z6z9Sx=(z7+2z8+z9)(z1+2z2+z3)Sy=(z3+2z6+z9)(z1+2z4+z7)M(i,j)=Sx+Sy使2
在这里插入图片描述
S x = [ − 1 − 2 − 1 0 0 0 1 2 1 ] S y = [ − 1 0 1 − 2 0 2 − 1 0 1 ] S_x = \begin{bmatrix} -1&-2&-1\\ 0&0&0\\ 1&2&1\\ \end{bmatrix} S_y = \begin{bmatrix} -1&0&1\\ -2&0&2\\ -1&0&1\\ \end{bmatrix} Sx=101202101Sy=121000121
S x S_x Sx对水平边缘有较强响应, S y S_y Sy 对竖直边缘有较强响应。微分模板的所有系数之和为0,保证了灰度恒定区域的响应为0。

Code

# Sobel锐化滤波器
def sobel(img):
    """
    :param img: 输入灰度图像
    :return: 返回锐化后的图像
    """
    cp_img = np.copy(img)

    Sx = np.array([[-1,-2,-1],[0,0,0],[1,2,1]])
    Sy = np.array([[-1,0,1],[-2,0,2],[-1,0,1]])

    row, col = Sx.shape
    # 边界补零处理
    r = (row-1)//2
    c = (col-1)//2
    rb = np.zeros((r,cp_img.shape[1]))
    temp = np.vstack([rb,cp_img,rb])
    cb = np.zeros((temp.shape[0],c))
    last = np.hstack([cb,temp,cb])
    mem = np.zeros(cp_img.shape)
    # 加权求和
    for i in range(r,last.shape[0]-r):
        for j in range(c,last.shape[1]-c):
            mem[i-r][j-c] = np.abs(np.sum(Sx*last[i-r:i+r+1,j-c:j+c+1]))+ np.abs(np.sum(Sy*last[i-r:i+r+1,j-c:j+c+1]))

    return mem
    
def show(img,title,sub):
    plt.subplot(sub[0],sub[1],sub[2])
    plt.imshow(img,cmap="gray")
    plt.axis('off') # 去掉坐标轴
    plt.title(title)

if __name__=="__main__":
    img = cv.imread('16.jpg',0)
    re_img = sobel(img)

    sobelx = cv.Sobel(img, cv.CV_8U, 1, 0, ksize=3)
    sobely = cv.Sobel(img, cv.CV_8U, 0, 1, ksize=3)

    show(re_img, 'sobel', (1, 3, 1))
    show(img, 'img', (1, 3, 2))
    show((sobely+sobelx), 'cv.sobel', (1, 3, 3))
    plt.show()

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

拉普拉斯算子

f ( x , y ) f(x,y) f(x,y) 函数的拉普拉斯运算:
∇ 2 f = ∂ 2 f ∂ x 2 + ∂ 2 f ∂ y 2 \nabla ^2f = \frac{\partial ^2f}{\partial x^2}+\frac{\partial ^2f}{\partial y^2} 2f=x22f+y22f
差分形式为:
∂ 2 f ∂ x 2 = f ( x + 1 , y ) + f ( x − 1 , y ) − 2 f ( x , y ) ∂ 2 f ∂ y 2 = f ( x , y + 1 ) + f ( x , y − 1 ) − 2 f ( x , y ) ∇ 2 f = [ f ( x + 1 , y ) + f ( x − 1 , y ) + f ( x , y + 1 ) + f ( x , y − 1 ) ] − 4 f ( x , y ) \frac{\partial ^2f}{\partial x^2}=f(x+1,y)+f(x-1,y)-2f(x,y) \\ \frac{\partial ^2f}{\partial y^2}=f(x,y+1)+f(x,y-1)-2f(x,y) \\ \nabla ^2f = [f(x+1,y)+f(x-1,y)+f(x,y+1)+f(x,y-1)]-4f(x,y) x22f=f(x+1,y)+f(x1,y)2f(x,y)y22f=f(x,y+1)+f(x,y1)2f(x,y)2f=[f(x+1,y)+f(x1,y)+f(x,y+1)+f(x,y1)]4f(x,y)
拉普拉斯模板:对水平和竖直边缘都有较强响应
L a p l a c e = [ 0 1 0 1 − 4 1 0 1 0 ] 或 [ 0 − 1 0 − 1 4 − 1 0 − 1 0 ] Laplace = \begin{bmatrix} 0&1&0\\ 1&-4&1\\ 0&1&0\\ \end{bmatrix}或 \begin{bmatrix} 0&-1&0\\ -1&4&-1\\ 0&-1&0\\ \end{bmatrix} Laplace=010141010010141010
拉普拉斯锐化的特点:

  1. 对噪声敏感;

  2. 产生双边缘;

拉普拉斯算子的扩展:对角线邻域加入变换中使得对各方向边缘均有较强响应。
[ 0 1 0 1 − 4 1 0 1 0 ] + [ 1 0 1 0 − 4 0 1 0 1 ] = [ 1 1 1 1 − 8 1 1 1 1 ] \begin{bmatrix} 0&1&0\\ 1&-4&1\\ 0&1&0\\ \end{bmatrix}+ \begin{bmatrix} 1&0&1\\ 0&-4&0\\ 1&0&1\\ \end{bmatrix}= \begin{bmatrix} 1&1&1\\ 1&-8&1\\ 1&1&1\\ \end{bmatrix} 010141010+101040101=111181111

拉普拉斯运算对图像增强的基本方法:将原始图像和拉普拉斯图像相叠加
g ( x , y ) = { f ( x , y ) − ∇ 2 f ( x , y ) 模 板 中 心 系 数 为 负 f ( x , y ) + ∇ 2 f ( x , y ) 模 板 中 心 系 数 为 正 g(x,y)=\begin{cases} f(x,y)-\nabla ^2f(x,y)&模板中心系数为负 \\ f(x,y)+\nabla ^2f(x,y)&模板中心系数为正 \\ \end{cases} g(x,y)={f(x,y)2f(x,y)f(x,y)+2f(x,y)
Code

import matplotlib.pyplot as plt
import numpy as np
import cv2 as cv

# Laplace拉普拉斯锐化
def laplace(img):
    cp_img = np.copy(img)
    lap = np.array([[0,1,0],[1,-4,1],[0,1,0]]) # Laplace算子
    m = np.array([[1,0,1],[0,-4,0],[1,0,1]])   # 对角领域
    extend_lap = lap+m  # 扩展Laplace算子
    row, col = extend_lap.shape
    # 边界补零处理
    r = (row-1)//2
    c = (col-1)//2
    rb = np.zeros((r,cp_img.shape[1]))
    temp = np.vstack([rb,cp_img,rb])
    cb = np.zeros((temp.shape[0],c))
    last = np.hstack([cb,temp,cb])
    mem = np.zeros(cp_img.shape)
    # 加权求和
    for i in range(r,last.shape[0]-r):
        for j in range(c,last.shape[1]-c):
            mem[i-r][j-c] = np.sum(extend_lap*last[i-r:i+r+1,j-c:j+c+1])

    return mem

def show(img,title,sub):
    plt.subplot(sub[0],sub[1],sub[2])
    plt.imshow(img,cmap="gray")
    plt.axis('off') # 去掉坐标轴
    plt.title(title)
    
if __name__=="__main__":
    img = cv.imread('16.jpg',0)
    re_img = laplace(img)
    laplacian = cv.Laplacian(img, cv.CV_8U)

    show(re_img, 'laplace', (2, 2, 1))
    show((re_img * 255 / np.max(re_img)).astype(np.uint8)+img, 'normalization laplace', (2, 2, 2))
    show(img, 'img', (2, 2, 3))
    show(laplacian+img, 'cv.Laplacian', (2, 2, 4))
    plt.show()

在这里插入图片描述

在这里插入图片描述

锐化滤波器:高频提升滤波模板:

在这里插入图片描述

高斯—拉普拉斯(Laplace of Gaussian,LoG)算子

锐化在增强边缘和细节的同时也增强了噪声。为了在取得好的锐化效果的同时把噪声的干扰降到最低, 可以先对带噪声的原始图像进行平滑,再进行锐化。

L o G LoG LoG 算子把高斯平滑和拉普拉斯锐化结合起来:

高斯函数:
h ( x , y ) = − e x p ( − x 2 + y 2 2 σ 2 ) = − e x p ( − r 2 2 σ 2 ) ( σ 2 为 标 准 差 ) h(x,y) = -exp(-\frac{x^2+y^2}{2\sigma ^2})=-exp(-\frac{r^2}{2\sigma ^2})(\sigma^2为标准差) h(x,y)=exp(2σ2x2+y2)=exp(2σ2r2)σ2
高斯-拉普拉斯算子:
∇ 2 h = − [ r 2 + σ 2 σ 4 ] e x p ( − x 2 + y 2 2 σ 2 ) \nabla ^2h = -[\frac{r^2+\sigma^2}{\sigma^4}]exp(-\frac{x^2+y^2}{2\sigma^2}) 2h=[σ4r2+σ2]exp(2σ2x2+y2)
常用的 L o G LoG LoG 算子是 5 × 5 5×5 5×5 的模板:
L o G = [ − 2 − 4 − 4 − 4 − 2 − 4 0 8 0 − 4 − 4 8 24 8 − 4 − 4 0 8 0 − 4 − 2 − 4 − 4 − 4 − 2 ] LoG = \begin{bmatrix} -2&-4&-4&-4&-2\\ -4&0&8&0&-4\\ -4&8&24&8&-4\\ -4&0&8&0&-4\\ -2&-4&-4&-4&-2\\ \end{bmatrix} LoG=24442408044824844080424442
在这里插入图片描述


  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值