我试图通过删除所有完全隔离的单个单元格来减少二进制python数组中的噪声,即如果它们完全被其他“0”包围,则将“1”值单元格设置为0。我已经能够通过使用循环删除大小等于 1 的 blob 来获得一个可行的解决方案,但这对于大型数组来说似乎是一个非常低效的解决方案:
import numpy as np
import scipy.ndimage as ndimage
import matplotlib.pyplot as plt
# Generate sample data
square = np.zeros((32, 32))
square[10:-10, 10:-10] = 1
np.random.seed(12)
x, y = (32*np.random.random((2, 20))).astype(np.int)
square[x, y] = 1
# Plot original data with many isolated single cells
plt.imshow(square, cmap=plt.cm.gray, interpolation='nearest')
# Assign unique labels
id_regions, number_of_ids = ndimage.label(square, structure=np.ones((3,3)))
# Set blobs of size 1 to 0
for i in xrange(number_of_ids + 1):
if id_regions[id_regions==i].size == 1:
square[id_regions==i] = 0
# Plot desired output, with all isolated single cells removed
plt.imshow(square, cmap=plt.cm.gray, interpolation='nearest')
在这种情况下,侵蚀和扩张我的数组将不起作用,因为它也会删除宽度为 1 的特征。我觉得解决方案位于scipy.ndimage包中的某个位置,但到目前为止我还无法破解它. 任何帮助将不胜感激!