• [opencv]图像处理-腐蚀操作与膨胀操作


    1.腐蚀操作:

      类似于化学反应,用酸腐蚀金属等等效果类似,因而被称为腐蚀操作。

    import cv2
    import matplotlib.pyplot as plt
    import numpy as np
    
    # 载入汉字 展示原图
    img = cv2.imread('E:/img/6.jpg')
    
    cv2.imshow('img',img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
    # 腐蚀范围2x2
    kernel = np.ones((2,2),np.uint8)
    # 迭代次数 iterations=1
    erosion = cv2.erode(img,kernel,iterations = 1)
    
    cv2.imshow('erosion',erosion)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    原图如下,带有许多不需要的细线,可以利用腐蚀操作去除细线:

     腐蚀操作后

     2.膨胀操作

      与名称同义,进行一个膨胀。上图明显腐蚀后,线条与原来相比变细了。这时可以采取一个膨胀操作来增宽线条。

      代码为先腐蚀后膨胀的操作:

    import cv2
    import matplotlib.pyplot as plt
    import numpy as np
    
    # 载入汉字 展示原图
    img = cv2.imread('E:/img/6.jpg')
    
    cv2.imshow('img',img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
    # 腐蚀范围2x2
    kernel = np.ones((2,2),np.uint8)
    # 迭代次数 iterations=1
    erosion = cv2.erode(img,kernel,iterations = 1)
    
    cv2.imshow('erosion',erosion)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
    # 膨胀范围
    kernel = np.ones((3,3),np.uint8)
    # 迭代次数
    img_dilate = cv2.dilate(erosion,kernel,iterations = 1)
    
    cv2.imshow('dilate', img_dilate)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    膨胀操作后,线条明显变粗。

  • 相关阅读:
    -bash: fork: Cannot allocate memory 问题的处理
    Docker top 命令
    docker常见问题修复方法
    The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)
    What's the difference between encoding and charset?
    hexcode of é î Latin-1 Supplement
    炉石Advanced rulebook
    炉石bug反馈
    Sidecar pattern
    SQL JOIN
  • 原文地址:https://www.cnblogs.com/zlc364624/p/12733026.html
Copyright © 2020-2023  润新知