zoukankan      html  css  js  c++  java
  • 差分滤波器的介绍及用于图像特征检测

    一. 差分滤波器

            差分滤波器对于图像亮度急剧变化的边缘有提取效果,可以获得邻近像素的差值。

    二. 差分滤波器形式


    竖直差分滤波器 ↑
     

    水平差分滤波器 ↑
     

    对角线差分滤波器 ↑

    三. python实现差分滤波器

            实验:实现上述三个差分滤波器,并作用于图像,查看图像各个方向上信息提取效果

    import cv2
    
    import numpy as np
    
    # Gray scale
    
    def BGR2GRAY(img):
    
        b = img[:, :, 0].copy()
    
        g = img[:, :, 1].copy()
    
        r = img[:, :, 2].copy()
    
        # Gray scale
    
        out = 0.2126 * r + 0.7152 * g + 0.0722 * b
    
        out = out.astype(np.uint8)
    
        return out
    
    # different filter
    
    def different_filter(img, K_size=3):
    
        H, W = img.shape
    
        # Zero padding
    
        pad = K_size // 2
    
        out = np.zeros((H + pad * 2, W + pad * 2), dtype=np.float)
    
        out[pad: pad + H, pad: pad + W] = img.copy().astype(np.float)
    
        tmp = out.copy()
    
        out_v = out.copy()
    
        out_h = out.copy()
    
        out_d = out.copy()
    
        # vertical kernel
    
        Kv = [[0., -1., 0.],[0., 1., 0.],[0., 0., 0.]]
    
        # horizontal kernel
    
        Kh = [[0., 0., 0.],[-1., 1., 0.], [0., 0., 0.]]
    
        # diagonal kernel
    
        Kd = [[-1.,0.,0.],[0.,1.,0.],[0.,0.,0.]]
    
        # filtering
    
        for y in range(H):
    
            for x in range(W):
    
                out_v[pad + y, pad + x] = np.sum(Kv * (tmp[y: y + K_size, x: x + K_size]))
    
                out_h[pad + y, pad + x] = np.sum(Kh * (tmp[y: y + K_size, x: x + K_size]))
    
                out_d[pad + y, pad + x] = np.sum(Kd * (tmp[y: y + K_size, x: x + K_size]))
    
        out_v = np.clip(out_v, 0, 255)
    
        out_h = np.clip(out_h, 0, 255)
    
        out_d = np.clip(out_d, 0, 255)
    
        out_v = out_v[pad: pad + H, pad: pad + W].astype(np.uint8)
    
        out_h = out_h[pad: pad + H, pad: pad + W].astype(np.uint8)
    
        out_d = out_d[pad: pad + H, pad: pad + W].astype(np.uint8)
    
     
    
        return out_v, out_h, out_d
    
    # Read image
    
    img = cv2.imread("../gezi.jpg").astype(np.float)
    
    # grayscale
    
    gray = BGR2GRAY(img)
    
    # different filtering
    
    out_v, out_h,out_d = different_filter(gray, K_size=3)
    
    # Save result
    
    cv2.imwrite("out_v.jpg", out_v)
    
    cv2.imshow("result_v", out_v)
    
    cv2.imwrite("out_h.jpg", out_h)
    
    cv2.imshow("result_h", out_h)
    
    cv2.imwrite("out_d.jpg", out_d)
    
    cv2.imshow("result_d", out_d)
    
    cv2.waitKey(0)
    
    cv2.destroyAllWindows()

    四. 实验结果


    原图 ↑
     

    水平差分滤波器作用结果 ↑
     

    竖直差分滤波器作用结果 ↑
     

    对角线(左上>右下)差分滤波器作用结果 ↑

            可以看到,实验结果如我们之前判断的那样,水平差分滤波器检测出了图像中的竖直特征;竖直差分滤波器检测出了图像中的水平特征;对角线(左上>右下)差分滤波器检测出了图像的对角线(左下>右上)特征。


    五. 参考材料:

      https://www.jianshu.com/p/87c32d5085f6

  • 相关阅读:
    单表清除重复数据
    调用webApi封装
    简单写入本地日志,日志文件位置与主程序exe位置相同
    APPConfig.XML获取配置文件(主程序和Dll各自的)
    获取当前运行程序上一级目录指定文件夹,没有就创建文件夹
    shell脚本中的单引号和双引号以及反引号详解
    Linux shell中反引号(`)的应用
    关于网页 硬解 软解 H264 HEVC 和你电脑起飞了那点事
    浏览器支持H.265解码总结
    微软、谷歌、亚马逊、Facebook等硅谷大厂91个开源软件盘点(附下载地址)
  • 原文地址:https://www.cnblogs.com/wojianxin/p/12503415.html
Copyright © 2011-2022 走看看