import cv2
import matplotlib.pyplot as plt
import numpy as np
img=cv2.imread("page_1.jpg",0)
size = 11
kernel = np.ones((size, size), dtype=np.uint8)
img_erosion = cv2.erode(img, kernel, iterations=1)
img_dilation = cv2.dilate(img, kernel, iterations=1)
img_close = cv2.erode(cv2.dilate(img, kernel), kernel)
plt.imshow(img_close)
coding=utf-8
img = cv2.imread('page_1.jpg',0)
#定义结构元素
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5, 5))
#闭运算
closed = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
#显示腐蚀后的图像
cv2.imshow("Close",closed);
#开运算
opened = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
#显示腐蚀后的图像
cv2.imshow("Open", opened);
cv2.waitKey(0)
cv2.destroyAllWindows()