编程语言
首页 > 编程语言> > 《python+opencv3.3视频教学 基础入门》膨胀与腐蚀 笔记

《python+opencv3.3视频教学 基础入门》膨胀与腐蚀 笔记

作者:互联网

参考:
https://blog.csdn.net/alw_123/article/details/83868878
https://www.jianshu.com/p/fc07d3065cf1

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_INV | cv.THRESH_OTSU)
    cv.imshow("binary image", binary)

    kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))   # 获取核,cv.MORPH_RECT是一个矩形核
    dst = cv.erode(binary, kernel)
    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_INV | cv.THRESH_OTSU)
    cv.imshow("binary image", binary)

    kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))   # 获取核,cv.MORPH_RECT是一个矩形核
    dst = cv.dilate(binary, kernel)
    cv.imshow("dilate_demo", dst)


def dilate_demo_rgb(image):
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))  # 获取核,cv.MORPH_RECT是一个矩形核
    dst = cv.dilate(image, kernel)
    cv.imshow("dilate_demo", dst)


def erode_demo_rgb(image):
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))  # 获取核,cv.MORPH_RECT是一个矩形核
    dst = cv.erode(image, kernel)
    cv.imshow("erode", dst)


print("-----------Python OpenCV Tutorial--------------")
src = cv.imread("C:/cv-samples/data/pic3.png")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)

erode_demo(src)
dilate_demo(src)
dilate_demo(src)   # cv.dilate()也可以直接对彩色图像做膨胀
erode_demo(src)   # cv.erode()也可以直接对彩色图像做腐蚀

cv.waitKey(0)

cv.destroyAllWindows()

标签:kernel,erode,入门,dilate,python,demo,image,cv,opencv3.3
来源: https://blog.csdn.net/rrrrrr89098087/article/details/110437078