import cv2 as cv
import matplotlib.pyplot as plt
# 封装图片显示函数
def image_show(image):
if image.ndim == 2:
plt.imshow(image, cmap='gray')
else:
image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
plt.imshow(image)
plt.show()
if __name__ == '__main__':
# 读取原图
img_fish = cv.imread('goldfish.jpg')
# 转换为灰度图
img_gray = cv.cvtColor(img_fish, cv.COLOR_RGB2GRAY)
# 获取图像边缘
img_edge = cv.Canny(img_gray, 5, 20)
# 闭运算填充图像空洞
kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))
img_close = cv.morphologyEx(img_edge, cv.MORPH_CLOSE, kernel, iterations=2)
# 寻找轮廓坐标点
[contours, hierarchy] = cv.findContours(img_close, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
# 绘制轮廓点
cv.drawContours(img_fish, contours, 0, (0, 255, 0), -1)
# 显示图片
image_show(img_fish)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)