6.3.6腐蚀与膨胀
python代码:
# -*- coding: utf-8 -*-
import cv2
g_trackbarNum = 0 # 全局变量,来区分做腐蚀还是膨胀,0为腐蚀,1为膨胀
g_kernelsize = 1 # 初始化内核大小,实际取的是g_kernelsize*2+1,即初始化为3
def dealImage(n):
global g_trackbarNum
kernel = cv2.getStructuringElement(1,(n*2+1,n*2+1))
if g_trackbarNum == 0:
desImage = cv2.erode(srcImage, kernel)
else:
desImage = cv2.dilate(srcImage, kernel)
cv2.imshow("desImage", desImage)
def process(n):
global g_trackbarNum
g_trackbarNum = n
srcImage = cv2.imread("E:/Study/python/OpenCV_study/img/black-white-0.png")
cv2.namedWindow("desImage")
cv2.createTrackbar("erode/dilate", "desImage", 0, 1, process)
cv2.createTrackbar("kernel", "desImage", g_kernelsize, 10, dealImage)
kernel = cv2.getStructuringElement(1 , (g_kernelsize*2+1,g_kernelsize*2+1))
desImage = cv2.erode(srcImage, kernel)
cv2.imshow("desImage", desImage)
cv2.waitKey(0)