我正在使用“分水岭”将我的图像分割成多个区域。生成的区域很好,但是我想删除一些很小的区域。
是否可以在分水岭上添加“最小面积阈值”或进行后处理以合并很小的区域?
示例代码:
import numpy as np
from skimage import measure, segmentation
import matplotlib.pyplot as plt
image = np.zeros((50,50))
# top left
image[8:28, 3:21] = 1
# top right
image[3:12, 30:47] = image[17:20, 30:35] = image[15:18, 39:44] = 1
# bottom left
image[46:48, 3:5] = image[46:48, 13:18] = image[42:45, 2:7] = image[35,25] = 1
# bottom right
image[38:45, 33:38] = image[35:38, 38:45] = image[43:48, 40:45] = image[39,40] = 1
labeled_regions= measure.label(image, connectivity=2)
label_ws = segmentation.watershed(np.ones_like(image), markers=labeled_regions)
fig = plt.imshow(image)
plt.savefig('blobs.png')
plt.imshow(label_ws)
plt.savefig('regions.png')