计算机视觉~图像腐蚀和膨胀

使用pythono语言实现以下要求:

任选两种结构对图像进行腐蚀和膨胀。

免费Gpt,低调使用:Sider -- ChatGPT Sidebar, GPT-4, Draw & Web access

实现样例图:

实现代码如下:

import cv2
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
matplotlib.rc("font",family='SimHei')
# 读取图像
image = cv2.imread('lifePhoto02.png', 0)

# 定义矩形和椭圆结构元素
rect_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
ellipse_kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))

# 腐蚀
eroded_image_rect = cv2.erode(image, rect_kernel)
eroded_image_ellipse = cv2.erode(image, ellipse_kernel)

# 膨胀
dilated_image_rect = cv2.dilate(image, rect_kernel)
dilated_image_ellipse = cv2.dilate(image, ellipse_kernel)

# 使用matplotlib库显示图像
plt.imshow(image, cmap='gray')
plt.title('原始图像')
plt.show()

plt.imshow(eroded_image_rect, cmap='gray')
plt.title('矩形结构元素腐蚀后的图像')
plt.show()

plt.imshow(eroded_image_ellipse, cmap='gray')
plt.title('椭圆结构元素腐蚀后的图像')
plt.show()

plt.imshow(dilated_image_rect, cmap='gray')
plt.title('矩形结构元素膨胀后的图像')
plt.show()

plt.imshow(dilated_image_ellipse, cmap='gray')
plt.title('椭圆结构元素膨胀后的图像')
plt.show()

# # 使用OpenCV的GUI显示图像
# cv2.imshow('Original Image', image)
# cv2.imshow('Eroded Image (Rect)', eroded_image_rect)
# cv2.imshow('Eroded Image (Ellipse)', eroded_image_ellipse)
# cv2.imshow('Dilated Image (Rect)', dilated_image_rect)
# cv2.imshow('Dilated Image (Ellipse)', dilated_image_ellipse)
# cv2.waitKey(0)
# cv2.destroyAllWindows()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值