from skimage import measure import cv2 import numpy as np ## ##image:二值图像 ##threshold_point:符合面积条件大小的阈值 def remove_small_points(image,threshold_point): img = cv2.imread(image, 0) #输入的二值图像 img_label, num = measure.label(img, neighbors=8, return_num=True)#输出二值图像中所有的连通域 props = measure.regionprops(img_label)#输出连通域的属性,包括面积等 resMatrix = np.zeros(img_label.shape) for i in range(1, len(props)): if props[i].area > threshold_point: tmp = (img_label == i + 1).astype(np.uint8) resMatrix += tmp #组合所有符合条件的连通域 resMatrix *= 255 return resMatrix res = remove_small_points(image,threshold_point)