Embedded_game/qianrushi/opecn腐蚀膨胀.py
2025-01-02 12:48:11 +08:00

32 lines
827 B
Python

# -*- coding:utf-8 -*-
# @Author len
# @Create 2023/10/22 15:09
import cv2
import numpy as np
# 读取二维码图像文件
image_path = 'data/red.jpg'
image = cv2.imread(image_path)
# 将图像转换为灰度图像
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 二值化图像(将灰度图像转换为二值图像)
_, threshold = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# 定义腐蚀和膨胀的核(结构元素)
kernel = np.ones((5, 5), np.uint8)
# 腐蚀操作
# eroded = cv2.erode(threshold, kernel, iterations=1)
# 膨胀操作
dilated = cv2.dilate(threshold, kernel, iterations=1)
# 显示原始图像、腐蚀后的图像和膨胀后的图像
cv2.imshow('Original Image', image)
# cv2.imshow('Eroded Image', eroded)
cv2.imshow('Dilated Image', dilated)
cv2.waitKey(0)
cv2.destroyAllWindows()