16、OpenCV Python 腐蚀和彭胀

__author__ = "WSX"
import cv2 as cv
import numpy as np

def erode_demo(image):
    print(image.shape)
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
    cv.imshow("binary", binary)
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (15, 15)) #15*15 的 矩形
    dst = cv.erode(binary, kernel)  #15*15 的 矩形  腐蚀
    cv.imshow("erode_demo", dst)


def dilate_demo(image):
    print(image.shape)
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
    cv.imshow("binary", binary)
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 5)) #5*5的 矩形
    dst = cv.dilate(binary, kernel) #膨胀
    cv.imshow("dilate_demo", dst)
原文地址:https://www.cnblogs.com/WSX1994/p/9155541.html