共计 866 个字符,预计需要花费 3 分钟才能阅读完成。
实验内容:形态学算法:二值图像进行膨胀、腐蚀、开、闭操作,提取边缘
导入图像
import cv2 as cv
import matplotlib.pyplot as plt
img = cv.imread('LenaRGB.bmp')
img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
plt.imshow(img)
二值化
gray_img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
_, binary_img = cv.threshold(gray_img, 127, 255, cv.THRESH_BINARY)
plt.imshow(binary_img, cmap='gray')
膨胀
kernel1 = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))
dilate_img = cv.dilate(binary_img, kernel1)
plt.imshow(dilate_img, cmap='gray')
腐蚀
erode_img = cv.erode(binary_img, kernel1)
plt.imshow(erode_img, cmap='gray')
开运算
opened3 = cv.morphologyEx(binary_img, cv.MORPH_OPEN, kernel1, iterations=3)
plt.imshow(opened3, cmap='gray')
闭运算
closed3 = cv.morphologyEx(binary_img, cv.MORPH_CLOSE, kernel1, iterations=3)
plt.imshow(closed3, cmap='gray')
提取边缘
absdiff_img = cv.absdiff(dilate_img, erode_img)
plt.imshow(absdiff_img, cmap='gray')
正文完