1、前景物体会变小,整幅图像的白色区域会减少,这对于去除白噪声很有用。
def erode_demo(img): gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) ret, thresh = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) cv.imshow('binary iamge', thresh) kernel = cv.getStructuringElement(cv.MORPH_RECT, (5,5)) dst = cv.erode(thresh, kernel=kernel) cv.imshow('erode_demo', dst)
def dilate_demo(img): gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) ret, thresh = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) cv.imshow('binary iamge', thresh) kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 5)) dst = cv.dilate(thresh, kernel) cv.imshow('dilate image', dst)