假设我有一个二值图像,表示为numpy
矩阵,其中一个像素是背景(0)或前景(1)。我正在寻找一种方法,删除前景中没有任何近邻的所有像素
假设图像矩阵为:
a = np.array([[0,0,1,1],[1,0,0,0]])
单像素删除后的结果图像应为
b = np.array([[0,0,1,1],[0,0,0,0]])
到目前为止,我的方法是为所有可能的方向组合开口:
opening1 = ndi.morphology.binary_opening(edge, structure=np.array([[0,1,0],[0,1,0],[0,0,0]]))
opening2 = ndi.morphology.binary_opening(edge, structure=np.array([[0,0,0],[0,1,1],[0,0,0]]))
opening3 = ndi.morphology.binary_opening(edge, structure=np.array([[1,0,0],[0,1,0],[0,0,0]]))
opening4 = ndi.morphology.binary_opening(edge, structure=np.array([[0,0,0],[0,1,0],[0,0,1]]))
opening = opening1 + opening2 + opening3 + opening4
另一种方法是标记连接的组件并通过索引将其删除,但是当涉及到计算复杂性时,这些解决方案感觉是次优的
这个怎么样
其思想是在每个方向上创建一个像素的图像偏移,然后通过查看任何偏移的对应关系来确定是否存在邻居
实际上,给连接的组件贴标签似乎是一条可行之路。至少
skimage
在remove_small_objects
函数here中是这样做的相关问题 更多 >
编程相关推荐