python opencv 图像膨胀
代码:
import cv2
import numpy as np
# 图像膨胀
def dilate_img(img,a,iterations):
kernel = np.ones((a,a),np.uint8)
dilate = cv2.dilate(img,kernel,iterations = iterations)
return dilate
# 回调函数,因为只能传一个参数,不方便,所以pass
def nothing(pos):
pass
# 图片路径
path = "2.jpg"
#读取图片
img = cv2.imread(path,1)
# 创建老窗口
cv2.namedWindow('OldImg')
# 绑定老窗口和滑动条(滑动条的数值)
cv2.createTrackbar('a', 'OldImg', 1, 30, nothing)
cv2.createTrackbar('iterations', 'OldImg', 1, 100, nothing)
while True:
# 提取滑动条的数值
a = cv2.getTrackbarPos('a', 'OldImg')
iterations = cv2.getTrackbarPos('iterations', 'OldImg')
# 滑动条数字传入函数img_dilated中,并且调用函数img_dilated
dilated = dilate_img(img,a,iterations)
# 绑定 img 和 dilated
result = np.hstack([img,dilated])
cv2.imshow('OldImg', result)
# 设置推出键
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 关闭窗口
cv2.destroyAllWindows()
效果
转载:https://blog.csdn.net/weixin_44493841/article/details/102487891
查看评论